Convert WMI call to CIM call

六月ゝ 毕业季﹏ 提交于 2019-12-11 10:45:50

问题


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.


回答1:


Your problem is you're using two incompatible commands to translate.

Invoke-CimMethod == Invoke-WmiMethod

Get-WmiObject is not the above, however. Here's a way to accomplish what you're doing:

$ComputerName = 'Foo'
$cimArgs = @{
    'Namespace'    = 'Root\CCM\ClientSDK'
    'ClassName'    = 'CCM_SoftwareUpdatesManager'
    'MethodName'   = 'InstallUpdates' # returns UInt32 object; 0 = success
    'Arguments'    = @{
        'CCMUpdates' = Get-WmiObject -Namespace Root\CCM\ClientSDK -Class CCM_SoftwareUpdate -Filter 'ComplianceState = "0"'
    }
    'CimSession'   = New-CimSession -ComputerName $ComputerName -SessionOption (New-CimSessionOption -Protocol Dcom)
}
Invoke-CimMethod @cimArgs

The Invoke-CimMethod cmdlet takes a dictionary to pass arguments to the method. I determined the keys/values based on this documentation.

This can alternatively be found by the following:

Get-CimClass -ClassName 'CCM_SoftwareUpdatesManager' -Namespace 'Root\CCM\ClientSDK' |
     ForEach-Object -MemberName CimClassMethods



回答2:


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
                 }


来源:https://stackoverflow.com/questions/51388966/convert-wmi-call-to-cim-call

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!