[英]CIM, Win32_OperatingSystem, and Win32ShutdownTracker
I'm trying to convert some PowerShell code from using WMI to using CIM. 我正在尝试将某些PowerShell代码从使用WMI转换为使用CIM。 I had a function which used Win32ShutdownTracker ( https://msdn.microsoft.com/en-us/windows/desktop/aa394057 ) to reboot a system, with a timeout, comment, and reason for initiating the reboot. 我有一个使用Win32ShutdownTracker( https://msdn.microsoft.com/zh-cn/windows/desktop/aa394057 )来重新启动系统的函数,其中包含超时,注释以及启动重新启动的原因。 Using WMI I had to give the account running the reboot permissions using “psbase.Scope.Options.EnablePrivileges = $true” even though the account was already an administrator on the local system. 使用WMI,即使该帐户已经是本地系统的管理员,我也必须使用“ psbase.Scope.Options.EnablePrivileges = $ true”授予运行该帐户的重启权限。 I have not figured out how to give the same permissions using CIM and it seems without them, I'm unable to reboot the system. 我还没有弄清楚如何使用CIM授予相同的权限,而且如果没有它们,我将无法重新启动系统。 Also, the class binding does not seem to work. 另外,类绑定似乎不起作用。 Any help would be greatly appreciated. 任何帮助将不胜感激。
I've tried many combinations of CIM, including getting the object then invoking it and calling the class directly and none have worked. 我尝试了CIM的许多组合,包括获取对象然后调用它并直接调用该类,但没有一个起作用。
WMI Code (Works without issue): WMI代码(可以正常运行):
$OSObject = Get-WmiObject -Class Win32_OperatingSystem
$OSObject.psbase.Scope.Options.EnablePrivileges = $true
$OSObject.Win32ShutdownTracker(300,"This is a test",2147745794,6)
CIM Code I've tried (Does not work): 我尝试过的CIM代码(无效):
$OSObject = Get-CimInstance -Class Win32_OperatingSystem
$OSObject.psbase.Scope.Options.EnablePrivileges = $true
The property 'EnablePrivileges' cannot be found on this object. 在此对象上找不到属性“ EnablePrivileges”。 Verify that the property exists and can be set. 验证该属性存在并且可以设置。 At line:1 char:1 + $OSObject.psbase.Scope.Options.EnablePrivileges = $true + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : PropertyNotFound 在第1行:char:1 + $ OSObject.psbase.Scope.Options.EnablePrivileges = $ true + ~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~ + + CategoryInfo:InvalidOperation:(:) [],RuntimeException + FullyQualifiedErrorId:PropertyNotFound
Invoke-CimMethod -CimClass $OSObject -MethodName Win32ShutdownTracker –Arguments 300,"This is a test",21477
45794,6
Invoke-CimMethod : Cannot bind parameter 'CimClass'. Invoke-CimMethod:无法绑定参数“ CimClass”。 Cannot convert the "Win32_OperatingSystem: Microsoft Windows 10 Enterprise" value of type "Microsoft.Management.Infrastructure.CimInstance#root/cimv2/Win32_OperatingSystem" to type "Microsoft.Management.Infrastructure.CimClass". 无法将类型“ Microsoft.Management.Infrastructure.CimInstance#root / cimv2 / Win32_OperatingSystem”的“ Win32_OperatingSystem:Microsoft Windows 10 Enterprise”值转换为类型“ Microsoft.Management.Infrastructure.CimClass”。 At line:1 char:28 + Invoke-CimMethod -CimClass $OSObject -MethodName Win32ShutdownTracker ... + ~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Invoke-CimMethod], ParameterBindingException + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.Management.Infrastructure.CimCmdlets.Invok eCimMethodCommand 第1行:28位字符+ Invoke-CimMethod -CimClass $ OSObject -MethodName Win32ShutdownTracker ... + ~~~~~~~~ + + CategoryInfo:InvalidArgument:(:) [Invoke-CimMethod],ParameterBindingException + FullyQualifiedErrorId:CannotConvertArgumentNoMessage ,Microsoft.Management.Infrastructure.CimCmdlets.Invok eCimMethodCommand
Invoke-CimMethod -CimClass "Win32_OperatingSystem" -MethodName Win32ShutdownTracker –Arguments @{300,"This is a test",2147745794,6}
Invoke-CimMethod -CimClass "Win32_OperatingSystem" -MethodName Win32ShutdownTracker –Arguments @{300,"This
is a test",2147745794,6}
Invoke-CimMethod -CimClass "Win32_OperatingSystem" -MethodName "Win32ShutdownTracker" –Arguments 300,"This
is a test",2147745794,6
Invoke-CimMethod : Cannot bind parameter 'CimClass'. Invoke-CimMethod:无法绑定参数“ CimClass”。 Cannot convert the "Win32_OperatingSystem" value of type "System.String" to type "Microsoft.Management.Infrastructure.CimClass". 无法将类型“ System.String”的“ Win32_OperatingSystem”值转换为类型“ Microsoft.Management.Infrastructure.CimClass”。 At line:1 char:28 + Invoke-CimMethod -CimClass "Win32_OperatingSystem" -MethodName "Win32 ... + ~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Invoke-CimMethod], ParameterBindingException + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.Management.Infrastructure.CimCmdlets.Invok eCimMethodCommand 第1行:28位字符+ Invoke-CimMethod -CimClass“ Win32_OperatingSystem” -MethodName“ Win32 ... + ~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo: InvalidArgument:(:) [Invoke-CimMethod],ParameterBindingException + FullyQualifiedErrorId:CannotConvertArgumentNoMessage,Microsoft.Management.Infrastructure.CimCmdlets.Invok eCimMethodCommand
Invoke-CimMethod -CimClass "Win32_OperatingSystem" -MethodName Win32ShutdownTracker –Arguments 300,"This is
a test",2147745794,6
Invoke-CimMethod : Cannot bind parameter 'CimClass'. Invoke-CimMethod:无法绑定参数“ CimClass”。 Cannot convert the "Win32_OperatingSystem" value of type "System.String" to type "Microsoft.Management.Infrastructure.CimClass". 无法将类型“ System.String”的“ Win32_OperatingSystem”值转换为类型“ Microsoft.Management.Infrastructure.CimClass”。 At line:1 char:28 + Invoke-CimMethod -CimClass "Win32_OperatingSystem" -MethodName Win32S ... + ~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Invoke-CimMethod], ParameterBindingException + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.Management.Infrastructure.CimCmdlets.Invok eCimMethodCommand 第1行:28位字符+ Invoke-CimMethod -CimClass“ Win32_OperatingSystem” -MethodName Win32S ... + ~~~~~~~~~~~~~~~~~~~~ + + CategoryInfo:InvalidArgument :(:) [Invoke-CimMethod],ParameterBindingException + FullyQualifiedErrorId:CannotConvertArgumentNoMessage,Microsoft.Management.Infrastructure.CimCmdlets.Invok eCimMethodCommand
(Invoke-CimMethod -CimClass 'Win32_OperatingSystem').Win32ShutdownTracker(300,"This is a test",2147745794,6
)
Invoke-CimMethod : Cannot bind parameter 'CimClass'. Invoke-CimMethod:无法绑定参数“ CimClass”。 Cannot convert the "Win32_OperatingSystem" value of type "System.String" to type "Microsoft.Management.Infrastructure.CimClass". 无法将类型“ System.String”的“ Win32_OperatingSystem”值转换为类型“ Microsoft.Management.Infrastructure.CimClass”。 At line:1 char:29 + (Invoke-CimMethod -CimClass 'Win32_OperatingSystem').Win32ShutdownTra ... + ~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Invoke-CimMethod], ParameterBindingException + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.Management.Infrastructure.CimCmdlets.Invok eCimMethodCommand 在第1行:char:29 +(Invoke-CimMethod -CimClass'Win32_OperatingSystem')。Win32ShutdownTra ... + ~~~~~~~~~~~~~~~~~~~~ + + CategoryInfo: InvalidArgument:(:) [Invoke-CimMethod],ParameterBindingException + FullyQualifiedErrorId:CannotConvertArgumentNoMessage,Microsoft.Management.Infrastructure.CimCmdlets.Invok eCimMethodCommand
(Invoke-CimMethod -CimClass $OSObject).Win32ShutdownTracker(300,"This is a test",2147745794,6)
Invoke-CimMethod : Cannot bind parameter 'CimClass'. Invoke-CimMethod:无法绑定参数“ CimClass”。 Cannot convert the "Win32_OperatingSystem: Microsoft Windows 10 Enterprise" value of type "Microsoft.Management.Infrastructure.CimInstance#root/cimv2/Win32_OperatingSystem" to type "Microsoft.Management.Infrastructure.CimClass". 无法将类型“ Microsoft.Management.Infrastructure.CimInstance#root / cimv2 / Win32_OperatingSystem”的“ Win32_OperatingSystem:Microsoft Windows 10 Enterprise”值转换为类型“ Microsoft.Management.Infrastructure.CimClass”。 At line:1 char:29 + (Invoke-CimMethod -CimClass $OSObject).Win32ShutdownTracker(300,"This ... + ~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Invoke-CimMethod], ParameterBindingException + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.Management.Infrastructure.CimCmdlets.Invok eCimMethodCommand 在第1行:char:29 +(Invoke-CimMethod -CimClass $ OSObject).Win32ShutdownTracker(300,“ This ... + ~~~~~~~~~ + CategoryInfo:InvalidArgument:(:) [Invoke-CimMethod] ,ParameterBindingException + FullyQualifiedErrorId:CannotConvertArgumentNoMessage,Microsoft.Management.Infrastructure.CimCmdlets.Invok eCimMethodCommand
This works for me: 这对我有用:
$arguments = @{
Timeout = [System.UInt32]300
Comment = 'This is a test'
ReasonCode = [System.UInt32]2147745794
Flags = 6
}
Invoke-CimMethod -Query 'SELECT * FROM Win32_OperatingSystem' -MethodName 'Win32ShutdownTracker' –Arguments $arguments
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.