Domain Workstation Shutdown Script – a.k.a My Green Policy

Posted on Posted in Don't Feed the Technician

One of the first things i took on when i became senior network administrator in my current position was to carry out a green policy but my limitations where £0 funding. With that in mind it occurred to me that the only place i could make a saving in electricity would be the make sure all site workstations would be off at night.

I can’t take full credit for the following code bulk has to go to the users over at Edugeek.

 

SHUTDOWN (3.3 KiB, 166 downloads)

 

The Code:

 

I won’t go through all the code and what it does i will only cover the areas that you would need to alter to make this work on your network.

 

Might be simpler to download the *.vbs file from the above download then go through this guide.

 

 

‘***************************************************************************************
‘ Shutdown Script – AWicher



‘***************************************************************************************
Set WSHShell = WScript.CreateObject(“WScript.Shell”)
Set FSO = CreateObject(“Scripting.FileSystemObject”)
Set logFile = FSO.CreateTextFile(“site.txt”, True)
Set objWMIService = GetObject(“winmgmts:\\.\root\cimv2”)
‘***************************************************************************************

‘***************************************************************************************
‘ Point at top level OU to shutdown all machines in an OU and sub OUs
strOU = “ou=Workstation”
‘***************************************************************************************

‘***************************************************************************************
strDomain = “Yourdomain.uk.com”
shutdownMessage=”””(10min Warning) Planned system shut-down of all school computers!! Please save your work and keep the computer turned off (Network Office ext:111)”””

‘***************************************************************************************

‘***************************************************************************************
set objRootDSE = GetObject(“LDAP://” & strDomain & “/RootDSE”)
set objParent = GetObject(“LDAP://” & strOU & “,” & objRootDSE.Get(“defaultNamingContext”))
‘Process computers in first level of OU.
objParent.Filter = Array(“computer”)
Call ouComp(objParent)
Call ouTree(objParent)
logFile.Close

Function ouTree(objParent)
‘***************************************************************************************

‘***************************************************************************************
objParent.Filter = Array(“OrganizationalUnit”)
for each objChild in objParent
‘Wscript.Echo “Processing OU ” & objChild.Get(“Name”) & objChild.Name
logfile.WriteLine (“Processing OU ” & objChild.Get(“Name”) & objChild.Name)
ouComp(objChild)
ouTree (objChild)
next
End Function

Function ouComp(objChild)
For each objUser in objChild
If objUser.class=”computer” then
select case ucase(objUser.Get(“Name”))
‘**************************************************************************************

‘**************************************************************************************
Case “Computer1″,”Computer2″,”Computer3”
‘Wscript.Echo “Will not Shutdown ” & objUser.Get(“Name”)
logfile.WriteLine (“Will not Shutdown ” & objUser.Get(“Name”) & ” ” & Now)
Case Else
Set colItems = objWMIService.ExecQuery _
(“Select * from Win32_PingStatus ” & _
“Where Address = ‘” & objUser.Get(“Name”) & “‘”)
For Each objItem in colItems
If objItem.StatusCode = 0 Then
‘Wscript.Echo “Will now try and Shutdown ” & objUser.Get(“Name”)
logfile.WriteLine (“Will now try and Shutdown ” & objUser.Get(“Name”) & ” ” & Now)
‘**************************************************************************************

‘**************************************************************************************
ShellRun = WSHShell.Run (“cmd /c shutdown /f /s /m \\” & objUser.Get(“Name”) & ” /t 600 /c ” & shutdownMessage,0,True )
End If
Next
end select

End If
next
End Function

 

To make this easy to follow i will be using Noepadd++ and refering to line numbers. Although not necessary to do i would highly recommend installing Notepadd++ to follow this guide.

 

First off when this code is run it will connect to the domain and will check what OU to use.

It will then create a new text file or replace if already exists and start from top sending the shutdown command to each workstation in the first OU then once done will move onto the next OU and start shutting down all workstations in that OU until it can’t find any more OU’s.

 

Now for the changes to make this script work on your network.

 

Make it Work For Me:

 

1

The first area you will need to check and change would be the domain name you are on.

  • Change line 19 – strDomain = “YOUR DOMAIN ADDRESS”

 

2

Now to set the top-level OU to send the shutdown command to. The code above is used to shut down the site but i have other scripts to shut down specific buildings or rooms.

  • Change line 15 – strOU = “ou=***********”
  • For example – strOU = “ou=B1,ou=Bowland,ou=Workstations” – For those who have worked with LDAP… will understand, you have to put the bottom OU first working your way to top OU. So this line would Open the domain, check AD for Workstations, then move into Bowland, then into B1. All workstations in B1 will then be sent the shutdown command. If you was to remove B1 OU it would then send the shutdown command to all workstations within Bowland but would also open each OU under Bowland and send the command to them.

 

3

Another area to alter would be the message to logged on users.

  • Line 20 – shutdownMessage=”””Lorem ipsum”””

Enter a polite message informing of shutdown. I would put how long untill forced shutdown will occur (Shutdown in 10min…) and tell who is performing the shutdown and how to get in contact with them.

  • shutdownMessage=”””(10min Warning) Planned system shut-down of all school computers!! Please save your work and keep the computer turned off (Network Office ext:406)”””

 

4

To set the name of the text file the code will create.

  • On line 9 – Set logFile = FSO.CreateTextFile(“site.txt”, True)

Change the filename (“FILENAME.txt,True) to whatever means something to you or to its location or purpose.

 

5

Don’t shut down specific computers can be done too.

  • On line 53 within the ” ” –  Case “Computer1″,”Computer2″,”Computer3”

These 4 computers will if found not be sent the code to shut down. Simply add the names of the workstations you wish to be omitted from this shutdown. Put each computer within quotation marks and keep separated using a comma.

 

6

The shutdown syntax itself. This is the code that is sent to each workstation found. The code is a normal cmd remote shutdown command, so to alter the delay just alter the number after the /t command (in this case 600 is 10min)

The code could also be altered to not shut down but to reboot, this could done by removing the -s and replace with -r.

 

From Microsoft:

Syntax

shutdown [{-l|-s|-r|-a}] [-f] [-m [\\ComputerName]] [-t xx] [-c message] [-d[u][p]:xx:yy]

Parameters

-l Logs off the current user, this is also the defualt. -m ComputerName takes precedence.

-s Shuts down the local computer.

-r Reboots after shutdown.

-a Aborts shutdown. Ignores other parameters, except -l and ComputerName. You can only use -a during the time-out period.

-f Forces running applications to close.

-m [\\ComputerNameSpecifies the computer that you want to shut down.

-t xx Sets the timer for system shutdown in xx seconds. The default is 20 seconds.

-c message Specifies a message to be displayed in the Message area of the System Shutdown window. You can use a maximum of 127 characters. You must enclose the message in quotation marks.

-d [u][p]:xx:yy Lists the reason code for the shutdown. The following table lists the different values.

Value Description

u

Indicates a user code.

p

Indicates a planned shutdown code.

xx

Specifies the major reason code (0-255).

yy

Specifies the minor reason code (0-65536).

 

 

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *