首页 > 解决方案 > 将本地 PowerShell 命令转换为 Cim-Instance 以进行远程管理

问题描述

以下 PowerShell 代码在本地工作,但我无法将其全部转换为使用 CIM 命令。我无法将前三个查询转换为 CIM。我需要使用 CIM 的原因是因为这就是我的网络当前允许 PowerShell 远程访问的方式。测试计算机安装了最新的 SCCM 客户端。

# Check all locations that would contain a pending reboot flag
$RebootPending = $false
$RebootPending = $RebootPending -or ([wmiclass]'ROOT\ccm\ClientSDK:CCM_ClientUtilities').DetermineIfRebootPending().RebootPending
$RebootPending = $RebootPending -or ([wmiclass]'ROOT\ccm\ClientSDK:CCM_ClientUtilities').DetermineIfRebootPending().IsHardRebootPending
$RebootPending = $RebootPending -or (@(((Get-ItemProperty("HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager")).$("PendingFileRenameOperations")) |
      Where-Object { $_ }
  ).length -ne 0)
$RebootPending = $RebootPending -or (
  @(Get-WmiObject -Query "SELECT * FROM CCM_SoftwareUpdate" -Namespace "ROOT\ccm\ClientSDK" |
      Where-Object {
      $_.EvaluationState -eq 8 -or # patch pending soft reboot
      $_.EvaluationState -eq 9 -or # patch pending hard reboot
      $_.EvaluationState -eq 10 } # reboot needed before installing patch
  ).length -ne 0)

我已设法将最后一个查询转换为 Cim,但我似乎无法确定如何将其他三个查询转换为 Cim

转换后的查询:

$RebootPending = $RebootPending -or (
  @(Get-CimInstance -CimSession $($Computer.CimSession) -Namespace 'ROOT\ccm\ClientSDK' -Query "SELECT * FROM CCM_SoftwareUpdate" |
      Where-Object {
      $_.EvaluationState -eq 8 -or # patch pending soft reboot
      $_.EvaluationState -eq 9 -or # patch pending hard reboot
      $_.EvaluationState -eq 10 } # reboot needed before installing patch
  ).length -ne 0
)

标签: powershellregistrywmi

解决方案


Invoke-CimMethod如果要使用 CIM 会话,可以使用cmdlet。对于示例中的前两项,请按如下方式调用它:

Invoke-CimMethod -ClassName 'CCM_ClientUtilities' `
                 -CimSession $Computer.CimSession `
                 -MethodName 'DetermineIfRebootPending' `
                 -Namespace 'ROOT\ccm\ClientSDK'

你会得到一个像这样的对象:

DisableHideTime     : 01/01/1970 00:00:00
InGracePeriod       : False
IsHardRebootPending : False
NotifyUI            : False
RebootDeadline      : 01/01/1970 00:00:00
RebootPending       : True
ReturnValue         : 0
PSComputerName      : 

例如Get-ItemProperty,调用它略有不同:

Invoke-CimMethod -ClassName 'StdRegProv ' `
                 -CimSession $Computer.CimSession `
                 -MethodName 'GetMultiStringValue' `
                 -Namespace 'ROOT\Cimv2' `
                 -Arguments @{hDefKey=[uint32]2147483650; 
                              sSubKeyName="SYSTEM\CurrentControlSet\Control\Session Manager"; 
                              sValueName="PendingFileRenameOperations"}

输出将是一个字符串数组,如下所示:

sValue
-----
{\??\C:\ProgramData\Microsoft\...

推荐阅读