+ Post New Thread
Page 1 of 3 123 LastLast
Results 1 to 15 of 38
Scripts Thread, Shutdown Script in Coding and Web Development; I wrote this vbscript to help with an agrument between staff at another school about who should shut down PC's! ...
  1. #1
    monkeyx's Avatar
    Join Date
    Nov 2006
    Posts
    364
    Thank Post
    8
    Thanked 52 Times in 41 Posts
    Rep Power
    25

    Shutdown Script

    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 OU Also 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
    Last edited by monkeyx; 6th March 2009 at 08:45 AM. Reason: minor typo in script

  2. Thanks to monkeyx from:

    garethedmondson (26th March 2009)

  3. #2
    monkeyx's Avatar
    Join Date
    Nov 2006
    Posts
    364
    Thank Post
    8
    Thanked 52 Times in 41 Posts
    Rep Power
    25
    Tried to edit old version, but unable todo so. This new version attempts to ping a machine before shutting it down, as it speeds up the process.

    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)
    Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
    '***************************************************************************************
    ' 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"))
    'Process computers in first level of OU.
    objParent.Filter = Array("computer")
    Call ouComp(objParent)
    Call ouTree(objParent)
    logFile.Close
    
    Function ouTree(objParent)
    '***************************************************************************************
    ' Enumerate All Computer Objects in selected OU
    '***************************************************************************************
    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
    		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)
    				'***************************************************************************************
    				' run command prompt options /c closes after run 1,True shows command 0,True Hides command
    				'**************************************************************************************
    				ShellRun = WSHShell.Run ("cmd /c shutdown /f /s /m \\" & objUser.Get("Name") & " /t 240 /c " & shutdownMessage,0,True ) 
    			    End If
    			Next			
    	end select
    
    	End If
    next
    End Function
    Last edited by monkeyx; 27th March 2009 at 08:21 AM. Reason: Add extra functions

  4. 2 Thanks to monkeyx:

    AWicher (9th July 2010), garethedmondson (26th March 2009)

  5. #3
    jsnetman's Avatar
    Join Date
    Oct 2007
    Posts
    887
    Thank Post
    23
    Thanked 134 Times in 126 Posts
    Rep Power
    39
    Tried the script here and it did not work. Interesting script if I could get it to work.

  6. Thanks to jsnetman from:

    garethedmondson (26th March 2009)

  7. #4

    garethedmondson's Avatar
    Join Date
    Oct 2008
    Location
    Gowerton, Swansea
    Posts
    2,260
    Thank Post
    965
    Thanked 324 Times in 192 Posts
    Blog Entries
    11
    Rep Power
    164
    Yes it would be intresting if it worked as at the moment we have a shutdown script that has all the machine names in - so if they have been rebuilt with new names we have to manually edit the script.

    This is a lot better because from what I can see in the posts above it checks the AD for machines.

    GJE

  8. #5

    powdarrmonkey's Avatar
    Join Date
    Feb 2008
    Location
    Alcester, Warwickshire
    Posts
    4,859
    Thank Post
    412
    Thanked 777 Times in 650 Posts
    Rep Power
    182
    (/me plugs Shutdownertron (info) which does all this for you)

  9. Thanks to powdarrmonkey from:

    sharkster (26th March 2009)

  10. #6
    jsnetman's Avatar
    Join Date
    Oct 2007
    Posts
    887
    Thank Post
    23
    Thanked 134 Times in 126 Posts
    Rep Power
    39
    Yes Shtdowatron looks good. I had a quick look at the instructions and wasn't clear about something. If you do not want the local client config settings do you have to install the client. If you do the script method is obviously more efficient as it doesn't require a client residing on the machine.

  11. #7

    powdarrmonkey's Avatar
    Join Date
    Feb 2008
    Location
    Alcester, Warwickshire
    Posts
    4,859
    Thank Post
    412
    Thanked 777 Times in 650 Posts
    Rep Power
    182
    Quote Originally Posted by jsnetman View Post
    Yes Shtdowatron looks good. I had a quick look at the instructions and wasn't clear about something. If you do not want the local client config settings do you have to install the client.
    Yes (because the point of it is decentralisation), just don't include the optional local config tool, or leave its default of install-on-demand.


    If you do the script method is obviously more efficient as it doesn't require a client residing on the machine.
    Depends on how you define 'more efficient'.

  12. #8
    jsnetman's Avatar
    Join Date
    Oct 2007
    Posts
    887
    Thank Post
    23
    Thanked 134 Times in 126 Posts
    Rep Power
    39
    Depends on how you define 'more efficient'.
    I thought networks were all about centralised management. But my view is if you need one less piece of software pushed out and one less service or program running on a machine the better.

    But still shutdownatron does look good and may give it a try. We currently use the scripting method with a list of machines.
    Last edited by jsnetman; 26th March 2009 at 10:12 AM.

  13. #9

    Join Date
    Mar 2007
    Posts
    421
    Thank Post
    14
    Thanked 16 Times in 10 Posts
    Rep Power
    19
    We wrote a multi purpose system here. Every time anyone logs in it gets logged in an SQL server with the PC details/username/time etc. The trick is to store the MAC addresses etc as well for startup scripts.

    Then each of us has a console to view the logins etc. We can use the console to shutdown/restart PCs or simply shutdown all PCs or start them all up using the magic packet method. You can also watch for certain users

    This works really well - especially the morning startup is only a mouse click!

    I'll see if I can package this up in a more generic format. It should work on an Windows server/AD based network but it will need to be tweaked to fit.

  14. #10
    Jon_boy1984's Avatar
    Join Date
    Oct 2007
    Location
    Worcestershire
    Posts
    60
    Thank Post
    10
    Thanked 15 Times in 15 Posts
    Rep Power
    37

    Exclamation

    I had a colleague that created a script like that, it works ok, but when we scheduled it staff weren't happy because it shutdown their PC in the middle of something (why they are still in the school at 7pm, I'll never know).

    So now we use something else, that doesn't use AD to check for PCs, it just shuts down every PC running a M$ OS on the network. 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).

    I didn't create it by the way, I stood on the shoulders of giants to get where I am, I understand how it works though.

  15. #11
    monkeyx's Avatar
    Join Date
    Nov 2006
    Posts
    364
    Thank Post
    8
    Thanked 52 Times in 41 Posts
    Rep Power
    25
    Quote Originally Posted by jsnetman View Post
    Tried the script here and it did not work. Interesting script if I could get it to work.
    What error message did the script give?

    Did you set strOU and strDomain with valid values?

  16. #12
    jsnetman's Avatar
    Join Date
    Oct 2007
    Posts
    887
    Thank Post
    23
    Thanked 134 Times in 126 Posts
    Rep Power
    39
    Yes set the domain and the following ou structure:

    strOU = "ou=D7,ou=Desktops,ou=Resources"

    It does not give any errors.

  17. #13
    lionsl2005's Avatar
    Join Date
    Nov 2008
    Location
    Barnet
    Posts
    547
    Thank Post
    384
    Thanked 23 Times in 22 Posts
    Rep Power
    16
    Quote Originally Posted by GoldenWonder View Post
    We wrote a multi purpose system here. Every time anyone logs in it gets logged in an SQL server with the PC details/username/time etc. The trick is to store the MAC addresses etc as well for startup scripts.

    Then each of us has a console to view the logins etc. We can use the console to shutdown/restart PCs or simply shutdown all PCs or start them all up using the magic packet method. You can also watch for certain users

    This works really well - especially the morning startup is only a mouse click!

    I'll see if I can package this up in a more generic format. It should work on an Windows server/AD based network but it will need to be tweaked to fit.
    Yeh im strugling on morning when turning on pcs. if there is a handy tool it will be gr8. if you can package that, most of us will love it.

  18. #14
    Dom_'s Avatar
    Join Date
    Dec 2008
    Posts
    1,009
    Thank Post
    151
    Thanked 138 Times in 115 Posts
    Rep Power
    56
    I use ABTutor for remote power on computers, its not free, but is very cheap

  19. #15
    monkeyx's Avatar
    Join Date
    Nov 2006
    Posts
    364
    Thank Post
    8
    Thanked 52 Times in 41 Posts
    Rep Power
    25
    Quote Originally Posted by jsnetman View Post
    Yes set the domain and the following ou structure:

    strOU = "ou=D7,ou=Desktops,ou=Resources"

    It does not give any errors.
    The script writes all output to logfile.txt rather than the screen.

    Is that showing anything?

SHARE:
+ Post New Thread
Page 1 of 3 123 LastLast

Similar Threads

  1. Script to shutdown computers.
    By FN-GM in forum Scripts
    Replies: 18
    Last Post: 17th November 2008, 03:43 PM
  2. Shutdown Script
    By andrew_91090 in forum Scripts
    Replies: 3
    Last Post: 1st October 2008, 02:03 PM
  3. Replies: 15
    Last Post: 8th September 2008, 01:43 PM
  4. Shutdown batch script stalls if 1 PC is shut down
    By PrimaryTech in forum Scripts
    Replies: 8
    Last Post: 7th July 2007, 06:04 PM
  5. Replies: 18
    Last Post: 14th October 2005, 09:28 AM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •