powershell - 将本地 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
)
解决方案
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\...
推荐阅读
- python - 你能简化 Python 中不等于或等于的链式比较吗?
- javascript - 将函数传递给子组件反应原生
- ios - Swift 4.0 代码、Xcode 10.0 的 ImplicitlyUnwrappedOptional 错误
- html - HTML5 输入类型仅显示年份
- javascript - 全局变量在开关内不起作用 - javascript
- php - 无法从套接字读取数据,无法绑定到 php 中的地址
- d3.js - 带有对数刻度的d3 js箱线图
- multithreading - MVVM Light DispatcherHelper,MVVM Light 中多线程的正确方法
- regex - 正则表达式,字符之间最多 1 个空格,没有前导或尾随空格
- ios - 连接到蜂窝数据提供商时 iOS 请求超时错误