Hello @everyone
A member of our community recently asked if there were a way to set the Embedded Script of a PowerShell Session programatically.
Before even trying to solve this challenge, he came back up to us with solution;
We thought it would be interesting to share his solution with our beloved community.
So here it goes:
function Get-CompressedByteArray
# This function actually does all the encrytion necessay to populate
{
[CmdletBinding()]
Param
(
[Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)][byte[]] $byteArray = $(Throw("-byteArray is required"))
)
Process
{
# Write-Verbose "Get-CompressedByteArray"
[System.IO.MemoryStream] $output = New-Object System.IO.MemoryStream
$gzipStream = New-Object System.IO.Compression.DeflateStream $output, ([IO.Compression.CompressionMode]::Compress)
$gzipStream.Write( $byteArray, 0, $byteArray.Length )
$gzipStream.Close()
$output.Close()
$tmp = $output.ToArray()
# Write-Output $tmp
}
}
$InlineScript = '<paste the scripot you want to input in the embedded script section>'
$bytes = [System.Text.Encoding]::ASCII.GetBytes($InlineScript)
$CompressedBytes = Get-CompressedByteArray $bytes
at this point, we get a fully functional compressed string that will be easily set to the EmbeddedScriptCompressed property of a powershell session, such as :
# if applicable, Import the RDM Powershell Module
$s = Get-RDMSession -Name "name of the session you want to set the embedded script into"
$s.PowerShell.EmbeddedScriptCompressed = $CompressedBytes
Set-RDMSession $s
Regards,
Alex Belisle
Hello @everyone
Additionally, we can apply this the other way around to read the content of an already set PowerShell Session :
function Get-DecompressedByteArray {
[CmdletBinding()]
Param
(
[Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)]
[byte[]] $byteArray = $(Throw("-byteArray is required"))
)
Process
{
# Write-Verbose "Get-DecompressedByteArray"
$input = New-Object System.IO.MemoryStream( , $byteArray )
$output = New-Object System.IO.MemoryStream
$gzipStream = New-Object System.IO.Compression.DeflateStream $input, ([IO.Compression.CompressionMode]::Decompress)
$gzipStream.CopyTo( $output )
$gzipStream.Close()
$input.Close()
[byte[]] $byteOutArray = $output.ToArray()
# Write-Output $byteOutArray
}
}
$s = Get-RDMSession -Name "name of the session you want to get the embedded script from"
$DecompressedBytes = Get-DecompressedByteArray ($s.PowerShell.EmbeddedScriptCompressed)
# display the text
Write-Host ( $enc.GetString( $decompressedByteArray ) | Out-String )
Regards,
Alex Belisle
Very helpful!
For anyone trying to update an XML file from an RDM Session Export (File > Export > Selection: myexport.rdm), you can convert the bytearray from the above Get-CompressedBytes function to base64 and save it in the XML.
ex:
$CompressedBytes = Get-CompressedByteArray -byteArray $Bytes
$XMLNode.PowerShell.EmbeddedScriptCompressed = [string]([Convert]::ToBase64String($CompressedBytes))
Just perfect. Thanks for the effort and sharing this information.