This one has been confusing me or the last couple of days. Unable to pass param to ConvertTo-SecureString, only script defined variable.
When I run a script I pass a number of params. On of the tasks it needs to do is connect to a remote machine via PSSession, i.e.
./myscript.ps1 -VMPass "12345@!" -VMuser abc
In my script I have the following which will be passed to New-PSSession:
[CmdletBinding()]
Param(
$VMuser,
$VMPass
)
$PWord = ConvertTo-SecureString -AsPlainText -String "$VMPass" -Force
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $VMuser, $PWord
$NewVMSession = (New-PSSession -ComputerName $NewVMAddress -Credential $cred -ErrorAction Stop)
When I run this I get the following error:
New-PSSession : [WIN-V2BK0KCPC7H] Connecting to remote server WIN-V2BK0KCPC7H
failed with the following error message : Access is denied. For more information,
see the about_Remote_Troubleshooting Help topic.
At C:\Users\ChildsC\Documents\Git\BAIC\Controller.ps1:85 char:26
+ ... MSession = (New-PSSession -ComputerName $NewVMAddress -Credential $vm ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OpenError: (System.Manageme....RemoteRunspace:RemoteRunspace) [New-PSSession], PSRemotingTransportException
+ FullyQualifiedErrorId : AccessDenied,PSSessionOpenFailed
However, if I were to explicitly define $VMPass = "12345@!" in the script OR if I were to call Get-Credential it works fine.
So there is a small issue in the way the param is being passed.
PSCredentialobject instead. - Bill_Stewart