Submitted By: Bruce Walton

This script logs off, shuts down, reboots, or powers off (depending on the selection) the computers listed in the Server array. The script prompts the user for the action they want to take, and demonstrates how to use WMI to remotely manage computers.

Visual Basic
Edit|Remove
'This script logs off, shuts down, reboots, or powers off (depending on the selection) 
'the computers listed in the Server array
'The user excuting the script must have administrative rights to the target machines
'Target computers running Windows Firewall need to follow the information in KB article _
'875605 on how to allow remote administration
'
'Submitted by Bruce Walton

'Prompt the user for the desired action
msg="Please select one of the following:" & vbcrlf & "0 - Log off" & vbcrlf & _
    "1 - Shutdown" & vbcrlf & "2 - Reboot" & vbcrlf & "8 - Power Off"

flag=inputbox(msg)
'Make sure the section is appropriate
if flag<>0 and flag<>1 and flag<>2 and flag<>8 then
    msgbox "Incorrect selection"
    wscript.quit
end if

'The computers to send the command to.  This list can be changed to allow for user input
Servers=array("computer1", "computer2", "computer3")

for x=lbound(servers) to ubound(servers)
    'Create the WMI object
    for each OS in getobject ("winmgmts:{(Shutdown)}//" & servers(x) & _
        "/root/cimv2").InstancesOf("Win32_OperatingSystem")
        'Change the WMI security to allow for shut downs, reboots, etc
        set Security = OS.Security_
        set PrivObj = Security.Privileges
        PrivObj.Add(18)
    
        'Send the command
        RetVal = OS.Win32ShutDown(flag,0)
    next
    if RetVal = 0 then
        wscript.echo "Shutdown " & servers(x)
    else
        wscript.echo "Unable to shutdown " & servers(x)
    end if
next