<

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x

Scripting XP from Afar

Published on
8,780 Points
2,580 Views
2 Endorsements
Last Modified:
Approved
Community Pick
There was a question recently on EE about "How do you shutdown a computer remotely and prompt the end user with the option to wait or proceed so that they might have time to close their applications before the computer shutdown?" . So I proceeded to offer this tidbit of code:

' Shutdown.vbs
' Example VBScript to shutdown computers w/ user input
' Author ou_dober
' Version 1.1 - July 2009
' --------------------------------------
Dim objShell, strComputer, strInput
Dim strShutdown
 
' This parameter is for when the user clicks on the yes box.(adjust -t as needed - default 0 min.)
strShutdown1 = "shutdown -s -t 0 -f -m \\" & strComputer
' This parameter is for when the user clicks on the no box. (adjust -t as needed - default 5 min.)
strShutdown2 = "shutdown -s -t 300 -f -m \\" & strComputer
' This parameter is for when the user does nothing. (adjust -t as needed - default 0 min.)
' If this is not needed, REM out the line with "        objShell.Run strShutdown3"
strShutdown3 = "shutdown -s -t 0 -f -m \\" & strComputer
 
Const wshYes = 6
Const wshNo = 7
Const wshYesNoDialog = 4
Const wshQuestionMark = 32
 
Set objShell = CreateObject("Wscript.Shell")
 
intReturn = objShell.Popup("Your computer needs to be shutdown. Do you wish to proceed right now?", _
    30, "***** SHUTDOWN REQUIRED *****", wshYesNoDialog   wshQuestionMark)
 
If intReturn = wshYes Then
        Wscript.Echo "You clicked the Yes to shutdown your computer now."
        objShell.Run strShutdown1
ElseIf intReturn = wshNo Then
        Wscript.Echo "You clicked the No button. Your computer will shutdown in 5 minutes. Please close all applications."
        objShell.Run strShutdown2
Else
        Wscript.Echo "The popup timed out. Computer will shutdown now."
        objShell.Run strShutdown3
End If

Open in new window


But then I thought "What if they only wanted to reboot the computer rather than a full shutdown?". So I offered another tidbit:

' Reboot.vbs
' Example VBScript to Reboot computers w/ user input
' Author ou_dober
' Version 1.1 - July 2009
' --------------------------------------
Dim objShell, strComputer, strInput
Dim strReboot
 
' This parameter is for when the user clicks on the yes box.(adjust -t as needed - default 0 min.)
strReboot1 = "Shutdown -r -t 0 -f -m \\" & strComputer
' This parameter is for when the user clicks on the no box. (adjust -t as needed - default 5 min.)
strReboot2 = "Shutdown -r -t 300 -f -m \\" & strComputer
' This parameter is for when the user does nothing. (adjust -t as needed - default 0 min.)
' If this is not needed, REM out the line with "        objShell.Run strReboot3"
strReboot3 = "Shutdown -r -t 0 -f -m \\" & strComputer
 
Const wshYes = 6
Const wshNo = 7
Const wshYesNoDialog = 4
Const wshQuestionMark = 32
 
Set objShell = CreateObject("Wscript.Shell")
 
intReturn = objShell.Popup("Your computer needs to be rebooted. Do you wish to proceed right now?", _
    10, "***** Reboot REQUIRED *****", wshYesNoDialog   wshQuestionMark)
 
If intReturn = wshYes Then
        Wscript.Echo "You clicked the Yes to Reboot your computer now."
        objShell.Run strReboot1
ElseIf intReturn = wshNo Then
        Wscript.Echo "You clicked the No button. Your computer will Reboot in 5 minutes. Please close all applications."
        objShell.Run strReboot2
Else
        Wscript.Echo "The popup timed out. Computer will Reboot now."
        objShell.Run strReboot3
End If

Open in new window


Anyways, the shutdown command is a really great command line utility when systems need to be shutdown or rebooted.  There are also many variations on how this command line can be used depending on your needs or purpose. This can be reviewed simply by going to your local command prompt and typing "shutdown /?"

So once you have created a great script and tested it thoroughly, you're not done yet. You still need to get it on the remote machine(s), place it a common area for other administrators to use, secure it from improper use, and then be able to use it remotely as planned. Here are common questions I have been asked that may help out.

What is the easiest way to access a file system of a remote machine?

Go to the Start Menu > Run > type "\\MACHINENAME\C$" and hit Enter. This will open the Explorer window showing the root folder of the remote machine so that you may copy the script to its appropriate place.  You can also do "\\MACHINENAME\ADMIN$\" to go directly to the C:\Windows folder of the remote machine as well. Make sure that you have proper rights to the remote machine before proceeding this way.

So where is the best place to put my script?

A common place for administrators to put their script is in a custom folder on the root (i.e. C:\Scripts or C:\ITTools) so that it can be easily found later on. A much better option to consider that would prevent end users from easily deleting the folder or script itself is in the C:\Windows\System32 folder.  This has two advantages: One advantage is that it makes it much less easier for the common end user to find the script should they try to explore the hard drive. Secondly, it puts the script in a default path of the OS so that it can be easily executed in any directory or sub-directory within a command prompt window or run line within the Start Menu.

You could even take this a step further if you like and create a sub-folder under C:\Windows\System32 called "ITTools" and add it the existing OS path:

PATH = %PATH%;C:\Windows\System32\ITTools

This will allow you to nicely keep your scripts separated and sorted from the other Windows utilities.

NOTE: Should you go on the path of putting the shutdown.vbs script in the C:\Windows\System32\ directory, please make sure to rename the shutdown.vbs to something else to prevent conflicts with the shutdown executable itself.

So what if that end user is a touch more techie than a common end user and finds it in the C:\Windows\System32 directory?

Another good step that can be taken by an administrator to protect their script is to change the attributes of the script so that it is hidden from the end user.  This can be done with the ATTRIB command via command prompt:

ATTRIB +H C:\windows\system32\shutdown.vbs

For more details on the ATTRIB command (use ATTRIB /?) or go to:
http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/attrib.mspx?mfr=true

So what if the end user is a power user with elevated privileges and found the hidden script?

I would suggest that you change the permissions of the script so that it can be selectively accessed, executed, and/or modified. This can be done with the CACLS command:

CACLS C:\Windows\System32\ /E /C /D enduser

For more details on the CACLS command (CACLS /?) or go to:
http://technet.microsoft.com/en-us/library/bb490872.aspx

So I just got the script copied onto the necessary machines. How do I execute it from my computer remotely now?

There is many ways that this can be done once the script is in place, but for the purpose of this article I will only do one that is free and fairly simple to do:

PSTools - From the toolkit, you will need to use PSEXEC.  The toolkit will need to be installed on the local and remote machine to work properly. [b]Note: Some anti-virus scanners report that one or more of the tools are infected with a "remote admin" virus. You may be to create an exemption rule in your AV client if you decide to use this tool.[/b] Once the toolkit is installed properly on the machines, go to the command prompt on your computer and type:

PSEXEC \\machinename shutdown.vbs

This will run the script remotely for you.

For more information on PSEXEC:
http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx

To download PSTools:
http://download.sysinternals.com/Files/PsTools.zip

For recurring scripts, consider using the task scheduler.  For more information:
http://technet.microsoft.com/en-us/library/bb742545.aspx


2
Author:ou_dober
2 Comments
LVL 6

Expert Comment

by:manicsquirrel
In your scripts, where are you assigning a value to strComputer?
0
LVL 6

Author Comment

by:ou_dober
I'm not, therefore it defaults to the local machine that it is being ran on.  
0

Featured Post

Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Check How effective MS Exchange Expert thinks Exchange Mailbox Recovery by SysTools IS. Visit the Official site to get detailed information:- https://www.systoolsgroup.com/exchange-recovery.html (https://www.systoolsgroup.com/exchange-recovery.h…

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month