I have been experimenting with two different approaches to finalizing a script. I need to offer different ways to complete the script, depending on user needs, like Logoff, Restart and Shutdown. I have tried two approaches, a Shutdown.exe based approach, and a Get-WmiObject approach. Like this...
Shutdown.exe -r -t 0
or
(Get-WmiObject win32_operatingsystem -nameSpace:'root\CIMV2' -computerName:'.').Win32Shutdown(2) > $null
The problem is that in different situations each works or doesn't. The former approach works on my Windows Home machine while the latter fails with a "Privilege not held" error. On the other hand, on a series of Windows 7 machines last week the former did nothing and the latter worked fine. I am hoping someone can verify if this is indeed a Home vs Pro thing, or if there might be something else at play? To add complexity to the issue, I also need to be able to run the script as a remote job, and ideally would rather not need an alternative approach. Is there some global solution here? Or do I need to perhaps go the ugly kludge route of using both, trusting one of the two will work in any condition?
EDIT: Per Tony's comment I am now using variations on this in a switch.
try {
(Get-WmiObject win32_operatingsystem -nameSpace:'root\CIMV2' -computerName:'.').Win32Shutdown(1) > $null
} catch {
Shutdown.exe /s /t 0
}