Hi,
I am trying to update my PowerShell script entries (Embedded script) which have their username and password passed as plain text variables in the arguments field. I am trying to move these values to the dedicated username and password fields on the entry, but I am having trouble moving the password.
Import-Module Devolutions.PowerShell
$RDPSessions = Get-RDMSession | Where-Object {$_.ConnectionType -eq "PowerShell"}
foreach ($s in $RDPSessions)
{
$words = $s.PowerShell.Arguments -split ' '
if ($words.Length -eq 4) {
$username = $words[2]
} elseif ($words.Length -eq 5) {
$username = $words[2]
$password = $words[4]
} else {
continue
}
if ($password -ne "") {
$safePassword=ConvertTo-securestring $password -asplaintext -force
Set-RDMSessionPassword $s.ID -Password $safePassword -Refresh
$test =Get-RDMSessionPassword $s.ID -AsPlainText
$test
}
$s.PowerShell.UserName = $username
Set-RDMSession $s
$s.Name,$username, $password -join ' - '
}When the $test variable is printed, the RDMSessionPassword returnes the newly set value. If I try to add another same check after Set-RDMSession it returns an empty value, as if the newly reloaded session does not contain the value anymore. Even after commenting out the Set-RDMSession call, the password is not applied to the entry. It is not visible on the entry and the export also doesn't contain the PowerShell.SafePassword value.
I would try to set the password via the $s.PowerShell.SafePassword variable, but I do not know how to generate the correct Base64 string that is used in the field. Setting the field this way with a known Base64 value from an existing "SafePassword" field does seem to work though.
Kind regards,
Ambrož Tičar
Hello,
For the Set-RDMSessionPassword cmdlet, could you please try to either not specify the .ID property or use the -Session switch like this?
Set-RDMSessionPassword $s -Password $safePassword -Refresh or Set-RDMSessionPassword -Session $s -Password $safePassword -Refresh
About the password encryption, the best method is still using the Set-RDMSessionPassword cmdlet with a secure string value. The method we use to encrypt the password is not publicly available.
Best regards,
Érica Poirier
Hi,
thank you, this seems to work!
Kind regards,
Ambrož Tičar
Hello,
Thank you for your feedback and glad the provided solution is working.
Best regards,
Érica Poirier