0
votes

I’m trying to convert some PowerShell code from using WMI to using 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. 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. 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. 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.

WMI Code (Works without issue):

$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):

$OSObject  = Get-CimInstance -Class Win32_OperatingSystem
$OSObject.psbase.Scope.Options.EnablePrivileges = $true

The property 'EnablePrivileges' cannot be found on this object. 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

Invoke-CimMethod -CimClass $OSObject -MethodName Win32ShutdownTracker –Arguments 300,"This is a test",21477
45794,6

Invoke-CimMethod : Cannot bind parameter '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". At line:1 char: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'. Cannot convert the "Win32_OperatingSystem" value of type "System.String" to type "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

Invoke-CimMethod -CimClass "Win32_OperatingSystem" -MethodName Win32ShutdownTracker –Arguments 300,"This is
 a test",2147745794,6

Invoke-CimMethod : Cannot bind parameter 'CimClass'. Cannot convert the "Win32_OperatingSystem" value of type "System.String" to type "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

(Invoke-CimMethod -CimClass 'Win32_OperatingSystem').Win32ShutdownTracker(300,"This is a test",2147745794,6
)

Invoke-CimMethod : Cannot bind parameter 'CimClass'. Cannot convert the "Win32_OperatingSystem" value of type "System.String" to type "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

(Invoke-CimMethod -CimClass $OSObject).Win32ShutdownTracker(300,"This is a test",2147745794,6)

Invoke-CimMethod : Cannot bind parameter '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". 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
-Arguments is a hashtable. I'm not sure how you set EnablePrivileges with CIM.js2010

1 Answers

0
votes

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