Hi all I'm trying to pass server login credentials to my 'createSQLServer function but keep hitting the error 'Cannot process argument transformation on parameter 'creds'.userName'' I've tried a lot of different, even tried with a 'param block' but stull stuck. A push in the right direction would be appreciated, cheers.
###### SQL SERVER LOGIN CREDENTIALS
$userName = "aaron"
$password = "Password_1234"
$securePassword = ConvertTo-SecureString -String $password -AsPlainText -Force
$creds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $userName, $securePassword
### SQL server names with resource group name appeneded
$server1 = "sqlserver1" + $resGroup
$serVerion = "12.0"
function createSQLserver ([string]$server1,[string]$server2, [string]$server3, [System.Management.Automation.PSCredential]$creds,[string]$server1Location, [string]$server2Location, [string]$server3Location, [string]$resGroup, [string]$serVerion, [string]$userName, [string]$password, [SecureString]$securePassword)
{
#Create server 1(Server A)
<#check to see if server exists - It exists, $continue is created and passed to
if statement to append two random characters to name#>
Write-Host "Creating First SQL Server"
$sqlServer = New-AzureRmSqlServer -ServerName $server1 -SqlAdministratorCredentials $creds -Location $server1Location -ResourceGroupName $resGroup -ServerVersion $serVerion -ErrorVariable continue -ErrorAction SilentlyContinue
if ($continue)
{
do {
$server1 = $server1 + (rand)
$sqlServer = New-AzureRmSqlServer -ServerName $server1 `
-SqlAdministratorCredentials $creds -Location $server1Location `
-ResourceGroupName $resGroup -ServerVersion "12.0" -ErrorVariable continue -ErrorAction SilentlyContinue
}
until(!$continue)
Write-Host 'exists creating new' $server1 'Created'
}else{
Write-Host $server1 ' Created'
}
Start-Sleep -s 2
}
createSQLserver $server1 $username $password $securePassword $creds $server1Location $resGroup $serVerion
You need to use your named parameters!
Here's a snippet of your first few parameters:
...
[string]$server1
,
[string]$server2
,
[string]$server3
,
[System.Management.Automation.PSCredential]$creds
...
And then the ones you're passing in to the function call
createSQLserver $server1 $username $password $securePassword ...
So because you're not using the names of your parameters, they are using their relative ordinal position i.e.
param | value
---------+----------------
$server1 | $server1
$server2 | $username
$server3 | $password
$creds | $securePassword
So what have we learned?
Always use named parameters!
createSQLserver -server1 $server1 -username $username -password $password -securePassword $securePassword
That should sort you out :-)
Related
Dears,
In my Powershell script, I would like to use a specific credential on a remote computer. This login exists on the remote computer and is local.
Hence, my code looks like this:
[Windows.Security.Credentials.PasswordVault,Windows.Security.Credentials,ContentType=WindowsRuntime]
$Vault = New-Object Windows.Security.Credentials.PasswordVault
$RESOURCE = "Resource"
$USERNAME = "MyLogin"
try {
$credentials = $Vault.Retrieve($RESOURCE,$USERNAME)
$pwd = $credentials.Password | ConvertTo-SecureString -Key (1..16)
}
catch {
$pwd = Read-Host "please enter your password:" -AsSecureString
$Encrypted = ConvertFrom-SecureString -SecureString $pwd -Key (1..16)
$credentials = new-object -Type Windows.Security.Credentials.PasswordCredential -ArgumentList $RESOURCE,"REMOTE_COMPUTER\"+$USERNAME,$Encrypted
$Vault.Add($credentials)
}
$cred = New-Object System.Management.Automation.PsCredential($USERNAME,$pwd)
$ComputerMemory = Get-WmiObject -ComputerName "REMOTE_COMPUTER" -Class win32_operatingsystem -ErrorAction Stop -Credential $cred
$Memory = ([math]::round(((($ComputerMemory.TotalVisibleMemorySize - $ComputerMemory.FreePhysicalMemory)*100)/ $ComputerMemory.TotalVisibleMemorySize), 2))
echo $Memory
Problem of course is when executing GetWmiObject, I got an Access denied error...
Is there something wrong with this code?
Thanks in advance for your feedback,
Kind regards,
Good morning :S.
I can enter into a PSSession and execute cmdlets just fine, however, as soon as I specify an account to use, it just throws back an access is denied error. I've even tested with the same account and password that established the PSSession. This works locally just fine.
I am trying to integrate this into an SCCM application, so there isn't a whole lot of wiggle room.
EDIT: I put an easier code that doesn't work either below:
$username = 'DOMAIN\Username'
$password = 'P#ssword'
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential $username, $securePassword
Start-Process Notepad.exe -Credential $credential
#Execute variable
$myCommand = "'C:\Program Files (x86)\PGP Corporation\PGP Desktop\pgpwde' --status --disk 0"
#Credential Variables
$username = 'DOMAIN\USERNAME'
$Password = ConvertTo-SecureString -String 'P#ssword' -Force -AsPlainText
$credential = New-Object System.Management.Automation.PsCredential -ArgumentList $username, $Password
#Expression Variable
$expression = #"
try
{
& $myCommand | Out-File `C:\test.txt -Force
}
catch
{
`$_.Exception.Message | Out-File `C:\ERROR.txt -Force
}
"#
#Execute
Start-Process powershell.exe -ArgumentList "-c $expression" -Credential $credential
I am new to Powershell and I want to try and automate server decommission procedures because the current task is too manual. So I have two functions. Function 1 stores the domain admins credentials and function 2 executes the decom procedures.
Function decom{
Set-Variable domain,password -Scope Global
function user {
$user =Read-Host -Prompt "Enter user name"
$domain = "domain\"+$user
$password = Read-Host -Prompt "Enter domain password" -AsSecureString | ConvertFrom-String | Set-Content C:\test\password.txt
}
user
$remove_AD = Uninstall-ADDSDomainController -RemoveDnsDelegation -NoRebootOnCompletion New-Object System.Management.Automation.PSCredential $domain, $password -WhatIf
$remove_Domain = Remove-Computer -UnjoinDomainCredential New-Object System.Management.Automation.PSCredential $domain, $password -WhatIf
$remove_NetAdapter = Disable-NetAdapter -Name * -WhatIf
}decom
In the below code, $Result variable has the following information. I need to iterate below each line in $Result variable and get the <APPPOOL NAME> that is, "DefaultAppPool","Classic .NET AppPool" & ".NET v2.0 Classic" as an input to the second Invoke-Command saved in $Result2. Please advise how this can be accomplished.
$Result output:
APPPOOL "DefaultAppPool" (MgdVersion:v4.0,MgdMode:Integrated,state:Started)
APPPOOL "Classic .NET AppPool" (MgdVersion:v2.0,MgdMode:Classic,state:Started)
APPPOOL ".NET v2.0 Classic" (MgdVersion:v2.0,MgdMode:Classic,state:Started)
$Username = '<username>'
$Password = '<Password>'
$pass = ConvertTo-SecureString -AsPlainText $Password -Force
$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username, $pass
$input_file_path = "servers.txt"
$output_path = "result.txt"
foreach ($server in Get-Content $input_file_path) {
$Result = Invoke-Command -ComputerName $server -Credential $Cred -ScriptBlock {
C:\Windows\system32\inetsrv\appcmd.exe list apppools
}
$Result | Add-Content $output_path
$Result2 = Invoke-Command -ComputerName #server -Credential $Cred -ScriptBlock {
C:\Windows\system32\inetsrv\appcmd.exe list apppools <APPPOOL NAME> /text:processmodel.username
}
}
I have the script below that lets me switch between the different elements and runs the functions in them one by one.
But what I need to do now is make it so I can select multiple ones and have them run and pause between them to verifiy if things were loaded correctly. So that way I don't run into the issue having to re rerun the full script again and redo the same one over.
Can anybody show me how to do this? I am lost as to how to get this completed and working properly.
write-host "Sets up location you want to run staging"
$ElementDistro = Read-Host -Prompt "Which Element do you want to run? (TV30/TV30BP/TV30LM/TV30PV/LT101/XR2/MU11/SAP)"
while ($ElementDistro -notmatch "^(TV30|TV30BP|TV30LM|TV30PV|LT101|XR2|MU11|SAP)$")
{
write-host "you have enterd an error" -ForegroundColor Red
write-host "You must type TV30 or TV30BP or TV30LM or TV30PV or LT101 or XR2 or MU11 or SAP"
write-host "you typed $ElementDistro"
write-host "set location you want to run staging"
$ElementDistro = Read-Host -Prompt "Which Element do you want to run? (TV30/TV30BP/TV30LM/TV30PV/LT101/XR2/MU11/SAP)"
}
switch ($ElementDistro)
{
'TV30'
{
# Do TV30 Stuff
write-host "you have entered TC TV30"
$passwd = convertto-securestring -AsPlainText -Force -String ''
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist "",$passwd
$session = enter-pssession -computername '' -credential $cred
$source = Select-TC
$destination = 'Desktop'
"Calling Copy-Item with parameters source: '$source', destination: '$destination'."
Copy-Item -Path $source -Destination $destination
exit-pssession
break
}
'TV30BP'
{
# Do TV30BP Stuff
$passwd = convertto-securestring -AsPlainText -Force -String ''
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist "",$passwd
$session = enter-pssession -computername '' -credential $cred
$source = Select-TC
$destination = 'Desktop'
"Calling Copy-Item with parameters source: '$source', destination: '$destination'."
Copy-Item -Path $source -Destination $destination
# exit-pssession
break
}
'TV30LM'
{
# Do TV30LM stuff
$passwd = convertto-securestring -AsPlainText -Force -String ''
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist "",$passwd
$session = enter-pssession -computername '' -credential $cred
$source = Select-TC
$destination = 'Desktop'
"Calling Copy-Item with parameters source: '$source', destination: '$destination'."
Copy-Item -Path $source -Destination $destination
exit-pssession
break
}
'TV30PV'
{
# Do TV30PV stuff
$passwd = convertto-securestring -AsPlainText -Force -String ''
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist "",$passwd
$session = enter-pssession -computername '' -credential $cred
$source = Select-TC
$destination = 'Desktop'
"Calling Copy-Item with parameters source: '$source', destination: '$destination'."
Copy-Item -Path $source -Destination $destination
exit-pssession
break
}
'LT101'
{
# Do LT101 stuff
$passwd = convertto-securestring -AsPlainText -Force -String ''
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist "",$passwd
$session = enter-pssession -computername '' -credential $cred
break
}
'XR2'
{
# Do XR2 stuff
$passwd = convertto-securestring -AsPlainText -Force -String ''
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist "",$passwd
$session = enter-pssession -computername '' -credential $cred
break
}
'MU11'
{
# Do TF10 stuff
$passwd = convertto-securestring -AsPlainText -Force -String ''
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist "",$passwd
$session = enter-pssession -computername '' -credential $cred
break
}
'SAP'
{
# Do SAP stuff
$passwd = convertto-securestring -AsPlainText -Force -String ''
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist "",$passwd
$session = enter-pssession -computername '' -credential $cred
break
}
}
break
}
If you got at least V3, you can use Out-GridView with -OutPutMode Multiple as a menu to select multiple items from:
$Menu = 'TV30','TV30BP','TV30LM','TV30PV','LT101','XR2','MU11','SAP','ALL'
$Choices = $Menu | Out-GridView -OutputMode Multiple -Title 'Select Locations you want to run staging, and click OK.'
Switch ($Choices)
{
.....
The quick answer is that Powershell's switch statement accepts an array for input. If you leave out the break statement at the end of each switch case it will execute each case that is a match. Enter your choices as a comma-separated list and put them into an array using the split statement.
Each choice in you $Choices array will be executed. If you put a Pause statement where your break statements are you can pause at the completion of each step.
$Choices = #('TV30','MU11')
switch ($Choices)
{
'TV30' {some code}
'TV30BP' {some code}
'MU11' {some code}
}