问题
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