I wrote this vbscript to help with an agrument between staff at another school about who should shut down PC's!
Also helps with your green credentials, can be useful to run via scheduled task to shutdown at a certain time.
Once you point the script at an OU it will enumerate all OUs and shutdown all machines, so make sure you point at write OUAlso an example of a script that can run against all computers in OU and sub OUs.
This script works on a couple of domains I have tested it on, but had to remove references and make generic for public release. If you are interested could you runthe script and let me know how it works or doesn't
Code:'*************************************************************************************** ' Shutdown Script written by tim@monkeyx.net ' Before running, set strDomain to your domain and strOU ' Create Reference to Wshell object for running command line programs ' Also creates a text file for logging in same folder script is run. '*************************************************************************************** Set WSHShell = WScript.CreateObject("WScript.Shell") Set FSO = CreateObject("Scripting.FileSystemObject") Set logFile = FSO.CreateTextFile("logfile.txt", True) '*************************************************************************************** ' Create Reference for OU to be enumertated '*************************************************************************************** ' Point at top level OU to shutdown all machines in an OU and sub OUs strOU = "ou=Level 1,ou=toplevel" '*************************************************************************************** ' Create Reference to OU Domain '*************************************************************************************** strDomain = "yourdomain.local" shutdownMessage="""End of day power down. Please save your work within 5 minutes to prevent data loss""" '*************************************************************************************** ' Connect to Domain using default Naming Contect '*************************************************************************************** set objRootDSE = GetObject("LDAP://" & strDomain & "/RootDSE") set objParent = GetObject("LDAP://" & strOU & "," & objRootDSE.Get("defaultNamingContext")) Call ouTree(objParent) logFile.Close Function ouTree(objParent) '*************************************************************************************** ' Enumerate All Computer Objects in selected OU '*************************************************************************************** 'objParent.Filter = Array("computer") 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")) '*************************************************************************************** ' List Machines not to shutdown when find in OUs '************************************************************************************** Case "COMP1","COMP2","COMP3","COMP4","ETC" 'Wscript.Echo "Will not Shutdown " & objUser.Get("Name") logfile.WriteLine ("Will not Shutdown " & objUser.Get("Name") & " " & Now) Case Else '*************************************************************************************** ' run command prompt options /c closes after run 1,True shows command 0,True Hides command '************************************************************************************** 'Wscript.Echo "Will now try and Shutdown " & objUser.Get("Name") logfile.WriteLine ("Will now try and Shutdown " & objUser.Get("Name") & " " & Now) '*************************************************************************************** ' Shutdown Options /f force log off /t 1200 is 20 minute warning '************************************************************************************** ' ShellRun = WSHShell.Run ("cmd /c shutdown /f /s /m \\" & objUser.Get("Name") & " /t 240 /c " & shutdownMessage,0,True ) end select End If next End Function



LinkBack URL
About LinkBacks
Also an example of a script that can run against all computers in OU and sub OUs.
Reply With Quote




Don't worry though, it checks the PCs name against a Skiplist txt file, so for things like servers, and admin workstations, we just get it to skip those, so they keep on running (They do have to be in UPPERCASE for some reason, I guessed it was because its using netbios). It also use's a different version of the shutdown.exe, so that the end user has an option to cancel the shutdown if they are still working. Not bad going for 3 files all freely available. I can't remember where I got them from, but if you PM me I can send them to you, or if I get to many requests I'll just attach it to a post. Its all free stuff too (my favourite price).

