I have a PowerShell script that encrypts and stores a password in an XML file - this runs fine. Another script imports that XML and decrypts the password to be passed to another process. This scripts work perfectly fine on one server (server 1) but fails on another (server 2).
Both servers I am running on run are Windows Server 2016 and run PowerShell version 5.1.14393.3053 - yet the script fails on server 2 but not on server 1. I have tried the process using another administrator user but got the same error.
Here is what my scripts look like:
encryptor.ps1 (generates the xml):
$cred = Get-Credential
$cred | Export-CliXml -Path 'D:\cred.xml'
decryptor.ps1 (this is the script that fails):
$credential = Import-CliXml -Path 'D:\cred.xml'
$PGUser=$credential.UserName
$PGPwd=$credential.GetNetworkCredential().Password #This causes error shown below
On both servers, running the encryptor.ps1 works fine but running decryptor.ps1 has an issue on server 2.
On server 1, decryptor.ps1 runs fine and I am able to access the variable $PGPwd
On server 2, when decryptor.ps1 is ran I see this error:
Method invocation failed because [Deserialized.System.Management.Automation.PSCredential] does not contain a method named 'GetNetworkCredential'.
At D:\infa_shared\DGDR\RunDump.ps1:3 char:1
+ $PGPwd=$credential.GetNetworkCredential().Password
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (GetNetworkCredential:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound