Search code examples
powershellsccm

Filter and replace results from Invoke-CIMMethod


Wondering if you could advise on how I filter these results and provide something more readable.

Input:

Invoke-CimMethod -Namespace ROOT\ccm\ClientSDK -ClassName CCM_Application -ComputerName $computer -MethodName Install -Arguments $ccmArgs | ft -AutoSize

Output

JobId                                  ReturnValue PSComputerName 
-----                                  ----------- -------------- 
{010101010101010101010101010101010101}           0 CLIENT_NAME

0 = success, and so I'd like it to just return the Computer Name and 'Success' or something similar.

Any help or pointers would be much appreciated.Thanks.


Solution

  • As Olaf already suggested, you can limit/rename the properties you want returned with Select-Object like this:

    Invoke-CimMethod -Namespace ROOT\ccm\ClientSDK -ClassName CCM_Application -ComputerName $computer -MethodName Install -Arguments $ccmArgs |
    Select-Object @{Name = 'ComputerName'; Expression = {$_.PSComputerName}},
                  @{Name = 'Result'; Expression = { if($_.ReturnValue -eq 0) {'Success'} else {'Error'}}}
    

    Output would be:

    ComputerName Result
    ------------ ------
    CLIENT_NAME  Success