by etolleson at 2013-02-14 06:57:39
I am new to PS finished the book PV3 MOL and felt pretty good until now, I am letting myself get stuck on the "‘Switching to WMI’ section of 16.2 in the book ToolMaking MOL page 164" for some reason I am not able to getby etolleson at 2013-02-14 09:51:39Invoke-WmiMethod -Class Win32_OperatingSystem -Name REBOOT -ComputerName localhost
to work I have tried several different things. I can get the Invoke-WmiMethod command to work with the help EXAMPLE 2 (notepad) in PS. I am having trouble understanding the error. I tried replacing the code in "listing 16.1" with this code and still no luck. Can you give me sugestions about Invoke-WmiMethod command and ways to troubleshoot the Error that I am receiving and having trouble deciffering fo rsome reason it is:
Invoke-WmiMethod : Invalid method Parameter(s)
At line:1 char:1
+ Invoke-WmiMethod -Class Win32_OperatingSystem -Name REBOOT -ComputerName localho …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: ( [Invoke-WmiMethod], ManagementException
+ FullyQualifiedErrorId : InvokeWMIManagementException,Microsoft.PowerShell.Commands.InvokeWmiMethod
I found this to work but not really sure why and if it was what the author was suggestingby MattG at 2013-02-14 16:36:43Get-WmiObject -Class win32_operatingSystem -ComputerName server | Invoke-WmiMethod -name REBOOT
if so great if not what should I have done. Trying to gain a better understanding of how to work with PS and WMI for older version of OS.
Great questions. The reason your first example doesn’t work is because the Reboot method is an instance method meaning that you must invoke it from an instance of a WMI class - Win32_OperatingSystem in your case. As to why this method is not static I don’t know considering you will only ever get a single instance of a Win32_OperatingSystem class as far as I know. For your first example to work properly, you must call a static method. A good example of a static WMI method is Create in Win32_Process. To illustrate this, observe the following examples:by etolleson at 2013-02-15 06:14:42Invoke-WmiMethod -Class Win32_Process -Name Create -ArgumentList calc.exe -ComputerName localhost
and the CIM cmdlet PSv3 equivalent:Invoke-CimMethod -Class Win32_Process -Name Create -Arguments @{CommandLine='calc.exe'}
To more clearly illustrate that Reboot requires an instance of the Win32_OperatingSystem class observe the following:$OperatingSystemInstance = Get-WmiObject -Class Win32_OperatingSystem
I hope this helps answer your questions.
$OperatingSystemInstance.Reboot()
Matt thanks for the response. The calc works great and I think I understand the instance that needs to be created. When I first tried the reboot it gave me an error which I found out you could not run it on the localhost or it appears that way anyway as long as I specified a different computer name in the instance it work like a charm. Thanks again for you help.
Exception calling "Reboot" : "Privilege not held. "
At line:1 char:1
+ $OperatingSystemInstance.Reboot()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: ( , MethodInvocationException
+ FullyQualifiedErrorId : WMIMethodException