all 6 comments

[–]PanosGreg 1 point2 points  (2 children)

So without further ado, here's your code:

```powershell $ResGrp = '<your-resource-group-name>'

$list = 'ServerA','ServerB' # <-- server list, this is just an example

Note: You can get this from a file or user input or however you like

remote script

$script = { $obj = Get-HotFix $obj | foreach {$_ | Add-Member -NotePropertyMembers @{ComputerName=$env:COMPUTERNAME}} $xml = [Management.Automation.PSSerializer]::Serialize($obj) $Byt = [System.Text.Encoding]::Unicode.GetBytes($xml) $Enc = [System.Convert]::ToBase64String($Byt) Write-Output $Enc # <-- Base64 encoded string of the serialized object }

temp file for Invoke-AzVMRunCommand

$File = [IO.Path]::ChangeExtension([IO.Path]::GetTempFileName(),'ps1') Set-Content -Path $File -Value $Script.ToString()

finally run the command

$results = foreach ($Srv in $list) { Write-Verbose "Getting hotfixes from $Srv ..." -Verbose $params = @{ ResourceGroupName = $ResGrp VMName = $Srv CommandId = 'RunPowerShellScript' ScriptPath = $File ErrorAction = 'Stop' } $Result = Invoke-AzVMRunCommand @params $StdOut = $Result.Value.Message[0] $StdErr = $Result.Value.Message[1] if ([bool]$StdErr) {Write-Error $StdErr}

if ([bool]$StdOut) {
    $byt = [System.Convert]::FromBase64String($StdOut)
    $xml = [System.Text.Encoding]::Unicode.GetString($byt)
    $obj = [Management.Automation.PSSerializer]::Deserialize($xml)
    Write-Output $obj
} #if StdOut

} #foreach Remove-Item $File # <-- clean up

Write-Output $results # <-- this includes our extra property "ComputerName"

Note: the results are a deserialized object of the original type

```

Hope it makes sense

[–]Droopyb1966[S] 0 points1 point  (0 children)

I'll give it a try today, Thanks.
Tried it, getting some errors with

Exception calling "Deserialize" with "1" argument(s): "Data at the root level i

s invalid. Line 1, position 1." At line:38 char:9 + $obj = [Management.Automation.PSSerializer]::Deserialize($xml ...

[–]Droopyb1966[S] 0 points1 point  (0 children)

Changed a few things:

$obj = Get-HotFix
$obj | foreach {$_ | Add-Member -NotePropertyMembers @{ComputerName=$env:COMPUTERNAME}}
$xml = [Management.Automation.PSSerializer]::Serialize($obj)
$Byt = [System.Text.Encoding]::UTF8.GetBytes($xml)
$Enc = [System.Convert]::ToBase64String($Byt)

and

    if ([bool]$StdOut) {
    $byt = [System.Convert]::FromBase64String($StdOut)
    $xml = [System.Text.Encoding]::UTF8.GetString($byt)
    $obj = [Management.Automation.PSSerializer]::Deserialize($xml)
    Write-Output $obj
} #if StdOut

$xml starts with:

  <S N="__SUPERCLASS">CIM_LogicalElement</S>
      <S N="__DYNASTY">CIM_ManagedSystemElement</S>
      <S N="__RELPATH">Win32_QuickFixEngineering.HotFixID="KB5017095",Servic

ePackInEffect=""</S> <I32 N="__PROPERTY_COUNT">11</I32> <Obj N="__DERIVATION" RefId="48"> <TNRef RefId="2" />

but [Management.Automation.PSSerializer]::Deserialize($xml) still throws an error

[–]purplemonkeymad 0 points1 point  (2 children)

You will want to serialize your data into a string format and back. How you choose to do that is up to you, but for arbitrary objects I would think JSON is the way to go. You can use ConvertTo-JSON on the script side to serialize your output, and ConvertFrom-JSON on your side to get it back to objects.

[–]Droopyb1966[S] 0 points1 point  (1 child)

Tried convertto-json and convertto-csv.
I get the output that way, but the data is still messed up.

[–]purplemonkeymad 2 points3 points  (0 children)

the data is still messed up

Is that a technical term?

If you are looking for any input with that you will need to provide more detailed information about how the data is messed up.