credentials not stored in password field

I have this code
[pre]

echo “Connecting to myserver”

$Server=“myserver”

$User=“Administrator”

ConvertTo-SecureString “mypassword” -AsPlainText -Force |ConvertFrom-SecureString |Out-File “c:\temp\pass.txt”

$Password= Get-Content “c:\temp\pass.txt” | ConvertTo-SecureString

$credential = New-Object System.Management.Automation.PSCredential($User,$Password)

$credential.GetNetworkCredential().Password

cmdkey /generic:TERMSRV/$Server /user:$User /pass:"$Password"

mstsc /v:$Server
[/pre]

but when I want to use this the password is not entered correcectly in the password field what am I doing wrong here

What are you trying to here ? You are converting password to secure string, saving to a file and then converting it back in the same script. Will cmdkey accept secure string ? if not you just need to use the hardcoded password directly with the cmdkey.

Hi

the reason the password creation is in the same script is only to show how I created the password file since you can also use an AES key.
so line 8 should dissapear from the script
the problem at this point was that cmdkey does not like “System.Security.SecureString” and as such fails to connect because the password is incorrect.
I just found out if you change line 14 by adding $pass = $credential.getNetworkCredential().Password and change line 16 $password into $pass then it is working and you can now start an RDP session without having to enter your password hard coded in the script
and that’s what I was trying to figure out.