Search code examples
powershellwmicim

Convert WMI call to CIM call


The code I am writing is suppose to kick off any patches currently available to a server using CIM. And I have to use CIM due to the required DCOM protocol for my network.

I'm using ` for easier viewing

The following wmi code works:

$ComputerName = 'Foo'
[System.Management.ManagementObject[]] $CMMissingUpdates = @(`
    Get-WmiObject -ComputerName $ComputerName `
                  -Query "SELECT * FROM CCM_SoftwareUpdate WHERE ComplianceState = '0'" `
                  -Namespace "ROOT\ccm\ClientSDK" `
                  -ErrorAction Stop)
$null = (Get-WmiObject -ComputerName $ComputerName `
                       -Namespace "root\ccm\ClientSDK" `
                       -Class "CCM_SoftwareUpdatesManager" `
                       -List).InstallUpdates($CMMissingUpdates)

What I've made using CIM that doesn't work:

$null = (Invoke-CimMethod -CimSession $Computer.CimSession `
                          -Namespace 'ROOT\ccm\ClientSDK' `
                          -ClassName 'CCM_SoftwareUpdatesManager' `
                          -MethodName 'InstallUpdates').InstallUpdates($CMMissingUpdates)

Not only am I interested in a solution to my Invoke-CimMethod but how it was solved. I can't seem to determine how to view and implement the methods of classes in CIM.


Solution

  • Turns out it was a casting issue. Link to solution: https://www.reddit.com/r/PowerShell/comments/8zvsd8/kick_off_a_sccm_clients_install_all_available/

    The final solution:

    $CMMissingUpdates = @( `
      Get-CimInstance -Query "SELECT * FROM CCM_SoftwareUpdate WHERE ComplianceState = '0'" `
                      -Namespace "ROOT\ccm\ClientSDK"
    )
    Invoke-CimMethod -Namespace 'ROOT\ccm\ClientSDK' `
                     -ClassName 'CCM_SoftwareUpdatesManager' `
                     -MethodName 'InstallUpdates' `
                     -Arguments @{ 
                        CCMUpdates = [cminstance[]]$CMMissingUpdates
                     }