PowerShell for Windows Admins

Dec 21 2009   7:23AM GMT

Working with the OS

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

We’ve seen what information we can derive on the Operating System from Win32_OperatingSystem but is there anything we can actually do to our systems?

There are a handful of methods available to us – mainly to do with shutting down the system

PS> Get-WmiObject -Class Win32_OperatingSystem | Get-Member -MemberType Method | Format-Table Name, Definition -Wrap

Name                                                  Definition
—-                                                        ———-
Reboot                                                System.Management.ManagementBaseObject Reboot()
SetDateTime                                     System.Management.ManagementBaseObject SetDateTime(System.S
                                                             tring LocalDateTime)
Shutdown                                           System.Management.ManagementBaseObject Shutdown()
Win32Shutdown                               System.Management.ManagementBaseObject Win32Shutdown(System
                                                             .Int32 Flags, System.Int32 Reserved)
Win32ShutdownTracker                 System.Management.ManagementBaseObject Win32ShutdownTracker
                                                             (System.UInt32 Timeout, System.String Comment, System.UInt3
                                                             2 ReasonCode, System.Int32 Flags)

Details from http://msdn.microsoft.com/en-us/library/aa394239(VS.85).aspx but

Reboot will shutdown and restart the system. SetDatetime ssts computer date and time (shouldn’t be needed with Time synchronisation in the domain)

Shutdown unloads programs and dlls so computer can be turned off

Win32Shutdown gives us options

0 (0x0) = Log Off
4 (0x4) = Forced Log Off (0 + 4)
1 (0x1) = Shutdown
5 (0x5) = Forced Shutdown (1 + 4)
2 (0x2) = Reboot
6 (0x6) = Forced Reboot (2 + 4)
8 (0x8) = Power Off
12 (0xC) = Forced Power Off (8 + 4)

On Vista\Win2008 and above we can use

Win32ShutdownTracker   to add a comment into the event log and and shutdown dialog box

We can use Win32Shutdown like this

$computer = Get-WmiObject -Class Win32_OperatingSystem -ComputerName .

If we put this into a loop and feed a set of computer names into it we can shut down our whole environment. Not a frequent occurrence – but one I needed to do recently when moving a datacentre.

 Comment on this Post

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: