Most Power Shell variables are a "String, char Object[] array, or Byte[] array". Example:
$string = "hello world"
$string.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
# OR
$char = 1,2,3,4,5,6
$char.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
But doing things like Encryption with Power Shell such as RSA returns the Base Type of
$rsa = New-Object System.Security.Cryptography.RSACryptoServiceProvider
$rsa.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True False RSACryptoServiceProvider System.Security.Cryptography.RSA
Trying to extract or save the "System.Security.Cryptography.RSA" $rsa BaseType and restore it from a variable breaks the Base Type. Only way that seems to work is to export the $rsa to a xml file and import it later as
Export-Clixml -InputObject $rsa -Path .\rsa.xml
$imported_rsa = Import-Clixml -Path ".\rsa.xml"
However you can't do it with a variable if the rsa.xml is stored as a string. Getting this error.
$xml_string = @"
<xml-data>
"@
$xml_string.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
$imported_rsa = Import-Clixml $xml_string
Import-Clixml: Cannot find drive. A drive with the name '<Objs Version="1.1.0.1" xmlns="http' does not exist.
Doing it with PSSerializer looks correct but is converted to the wrong type of System.Object
$rsa
CspKeyContainerInfo :
KeyExchangeAlgorithm : RSA
KeySize : 1024
LegalKeySizes : {System.Security.Cryptography.KeySizes}
PersistKeyInCsp : False
PublicOnly : False
SignatureAlgorithm : http://www.w3.org/2000/09/xmldsig#rsa-sha1
$rsa.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True False RSACryptoServiceProvider System.Security.Cryptography.RSA
$xml_string = @"
<xml-data>
"@
[Management.Automation.PSSerializer]::Deserialize($xml_string)
KeyExchangeAlgorithm : RSA
KeySize : 1024
LegalKeySizes : {System.Security.Cryptography.KeySizes}
PersistKeyInCsp : False
PublicOnly : False
SignatureAlgorithm : http://www.w3.org/2000/09/xmldsig#rsa-sha1
[Management.Automation.PSSerializer]::Deserialize($xml_string).GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True PSObject System.Object
Attempting to save System.Security.Cryptography.RSA BaseType by converting it to a format that can be move around. Breaks once you try to move it back its System.Security.Cryptography.RSA BaseType. The only thing that seems to work is to export the $rsa variable as a XML file and Import it. However try to do it completely by storing data in variables which leads to the wrong GetType. Breaking the $rsa and not able to decrypt or encrypt anything after the fact.