<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-3376060701639754039</id><updated>2012-02-16T18:57:38.197-08:00</updated><title type='text'>Book1</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://booksrthere.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3376060701639754039/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://booksrthere.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>jerin</name><uri>http://www.blogger.com/profile/10103476469626499932</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>10</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-3376060701639754039.post-2191776867912406974</id><published>2008-03-26T09:31:00.000-07:00</published><updated>2008-03-26T09:32:53.820-07:00</updated><title type='text'>Hack 9 Add, Remove, or Retrieve Environment Variables</title><content type='html'>Environment variables can easily be added, removed, or retrieved using the script in this hack.&lt;br /&gt;&lt;br /&gt;Using VBScript to work with the Windows system environment can be pretty simple. This hack shows how to use a script to read variables, add new variables, remove variables, and recurse through all of them. Just take a look through the script and read the comments to see how to perform each task. Note that there are four types of values in the Windows Script Host (WSH) environment—System, User, Volatile, and Process—and the script uses all of them.&lt;br /&gt;&lt;br /&gt;By the way, this script is provided by Dudeworks (http://www.dudeworks.net). For additional resources on Windows scripting and working with the environment, see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/wsProEnvironment.asp.&lt;br /&gt;&lt;br /&gt;The Code&lt;br /&gt;Type the following script into Notepad (with Word Wrap disabled) and save it with a .vbs extension as GetEnvVars.vbs:&lt;br /&gt;&lt;br /&gt;'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ &lt;br /&gt;&lt;br /&gt;'Created by: Rob Olson - Dudeworks &lt;br /&gt;&lt;br /&gt;'Created on: 10/17/2001 &lt;br /&gt;&lt;br /&gt;'Purpose: Get Environment Variables. &lt;br /&gt;&lt;br /&gt;'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;wscript.echo "Working with the Environment: Provided by www.dudeworks.net"&amp;vbcrlf&amp;vbcrlf&amp;strval &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;'// Create an instance of the wshShell object&lt;br /&gt;&lt;br /&gt;set WshShell = CreateObject("WScript.Shell")&lt;br /&gt;&lt;br /&gt;'Use the methods of the object&lt;br /&gt;&lt;br /&gt;wscript.echo "Environment.item: "&amp; WshShell.Environment.item("WINDIR")&lt;br /&gt;&lt;br /&gt;wscript.echo "ExpandEnvironmentStrings: "&amp; WshShell.ExpandEnvironmentStrings("%windir%")&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;'// add and remove environment variables&lt;br /&gt;&lt;br /&gt;'// Specify the environment type ( System, User, Volatile, or Process )&lt;br /&gt;&lt;br /&gt;set oEnv=WshShell.Environment("System")&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;wscript.echo "Adding ( TestVar=Windows Script Host ) to the System " _&lt;br /&gt;&lt;br /&gt;&amp; "type environment"&lt;br /&gt;&lt;br /&gt;' add a var&lt;br /&gt;&lt;br /&gt;oEnv("TestVar") = "Windows Script Host"&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;wscript.echo "removing ( TestVar=Windows Script Host ) from the System " _&lt;br /&gt;&lt;br /&gt;&amp; "type environment"&lt;br /&gt;&lt;br /&gt;' remove a var&lt;br /&gt;&lt;br /&gt;oEnv.Remove "TestVar"&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;'// List all vars in all environment types&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;'//System Type&lt;br /&gt;&lt;br /&gt;set oEnv=WshShell.Environment("System")&lt;br /&gt;&lt;br /&gt;for each sitem in oEnv &lt;br /&gt;&lt;br /&gt;strval=strval &amp; sItem &amp;vbcrlf &lt;br /&gt;&lt;br /&gt;next&lt;br /&gt;&lt;br /&gt;wscript.echo "System Environment:"&amp;vbcrlf&amp;vbcrlf&amp;strval &lt;br /&gt;&lt;br /&gt;strval=""&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;'//Process Type&lt;br /&gt;&lt;br /&gt;set oEnv=WshShell.Environment("Process")&lt;br /&gt;&lt;br /&gt;for each sitem in oEnv &lt;br /&gt;&lt;br /&gt;strval=strval &amp; sItem &amp;vbcrlf &lt;br /&gt;&lt;br /&gt;next&lt;br /&gt;&lt;br /&gt;wscript.echo "Process Environment:"&amp;vbcrlf&amp;vbcrlf&amp;strval &lt;br /&gt;&lt;br /&gt;strval=""&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;'//User Type&lt;br /&gt;&lt;br /&gt;set oEnv=WshShell.Environment("User")&lt;br /&gt;&lt;br /&gt;for each sitem in oEnv &lt;br /&gt;&lt;br /&gt;strval=strval &amp; sItem &amp;vbcrlf &lt;br /&gt;&lt;br /&gt;next&lt;br /&gt;&lt;br /&gt;wscript.echo "User Environment:"&amp;vbcrlf&amp;vbcrlf&amp;strval &lt;br /&gt;&lt;br /&gt;strval=""&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;'//Volatile Type&lt;br /&gt;&lt;br /&gt;set oEnv=WshShell.Environment("Volatile")&lt;br /&gt;&lt;br /&gt;for each sitem in oEnv &lt;br /&gt;&lt;br /&gt;strval=strval &amp; sItem &amp;vbcrlf &lt;br /&gt;&lt;br /&gt;next&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;wscript.echo "Volatile Environment:"&amp;vbcrlf&amp;vbcrlf&amp;strval &lt;br /&gt;&lt;br /&gt;strval=""&lt;br /&gt;Running the Hack&lt;br /&gt;To run the script, open a command prompt, change to the directory where the script is saved, and type cscript.exe GetEnvVars.vbs. Here is an example of typical output from the script on a Windows 2000 machine:&lt;br /&gt;&lt;br /&gt;Microsoft (R) Windows Script Host Version 5.6&lt;br /&gt;&lt;br /&gt;Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Working with the Environment: Provided by www.dudeworks.net&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Environment.item: %SystemRoot%&lt;br /&gt;&lt;br /&gt;ExpandEnvironmentStrings: C:\WINNT&lt;br /&gt;&lt;br /&gt;Adding ( TestVar=Windows Script Host ) to the System type environment&lt;br /&gt;&lt;br /&gt;removing ( TestVar=Windows Script Host ) from the System type environment&lt;br /&gt;&lt;br /&gt;System Environment:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;ComSpec=%SystemRoot%\system32\cmd.exe&lt;br /&gt;&lt;br /&gt;Os2LibPath=%SystemRoot%\system32\os2\dll;&lt;br /&gt;&lt;br /&gt;Path=%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem&lt;br /&gt;&lt;br /&gt;windir=%SystemRoot%&lt;br /&gt;&lt;br /&gt;OS=Windows_NT&lt;br /&gt;&lt;br /&gt;PROCESSOR_ARCHITECTURE=x86&lt;br /&gt;&lt;br /&gt;PROCESSOR_LEVEL=6&lt;br /&gt;&lt;br /&gt;PROCESSOR_IDENTIFIER=x86 Family 6 Model 5 Stepping 2, GenuineIntel&lt;br /&gt;&lt;br /&gt;PROCESSOR_REVISION=0502&lt;br /&gt;&lt;br /&gt;NUMBER_OF_PROCESSORS=1&lt;br /&gt;&lt;br /&gt;PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH&lt;br /&gt;&lt;br /&gt;TEMP=%SystemRoot%\TEMP&lt;br /&gt;&lt;br /&gt;TMP=%SystemRoot%\TEMP&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Process Environment:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;=C:=C:\&lt;br /&gt;&lt;br /&gt;=ExitCode=00000000&lt;br /&gt;&lt;br /&gt;ALLUSERSPROFILE=C:\Documents and Settings\All Users&lt;br /&gt;&lt;br /&gt;APPDATA=C:\Documents and Settings\Administrator\Application Data&lt;br /&gt;&lt;br /&gt;CommonProgramFiles=C:\Program Files\Common Files&lt;br /&gt;&lt;br /&gt;COMPUTERNAME=SNOOPY&lt;br /&gt;&lt;br /&gt;ComSpec=C:\WINNT\system32\cmd.exe&lt;br /&gt;&lt;br /&gt;HOMEDRIVE=C:&lt;br /&gt;&lt;br /&gt;HOMEPATH=\Documents and Settings\Administrator&lt;br /&gt;&lt;br /&gt;LOGONSERVER=\\SNOOPY&lt;br /&gt;&lt;br /&gt;NUMBER_OF_PROCESSORS=1&lt;br /&gt;&lt;br /&gt;OS=Windows_NT&lt;br /&gt;&lt;br /&gt;Os2LibPath=C:\WINNT\system32\os2\dll;&lt;br /&gt;&lt;br /&gt;Path=C:\WINNT\system32;C:\WINNT;C:\WINNT\System32\Wbem&lt;br /&gt;&lt;br /&gt;PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH&lt;br /&gt;&lt;br /&gt;PROCESSOR_ARCHITECTURE=x86&lt;br /&gt;&lt;br /&gt;PROCESSOR_IDENTIFIER=x86 Family 6 Model 5 Stepping 2, GenuineIntel&lt;br /&gt;&lt;br /&gt;PROCESSOR_LEVEL=6&lt;br /&gt;&lt;br /&gt;PROCESSOR_REVISION=0502&lt;br /&gt;&lt;br /&gt;ProgramFiles=C:\Program Files&lt;br /&gt;&lt;br /&gt;PROMPT=$P$G&lt;br /&gt;&lt;br /&gt;SystemDrive=C:&lt;br /&gt;&lt;br /&gt;SystemRoot=C:\WINNT&lt;br /&gt;&lt;br /&gt;TEMP=C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp&lt;br /&gt;&lt;br /&gt;TMP=C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp&lt;br /&gt;&lt;br /&gt;USERDOMAIN=SNOOPY&lt;br /&gt;&lt;br /&gt;USERNAME=Administrator&lt;br /&gt;&lt;br /&gt;USERPROFILE=C:\Documents and Settings\Administrator&lt;br /&gt;&lt;br /&gt;windir=C:\WINNT&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;User Environment:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;TEMP=%USERPROFILE%\Local Settings\Temp&lt;br /&gt;&lt;br /&gt;TMP=%USERPROFILE%\Local Settings\Temp&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Volatile Environment:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;LOGONSERVER=\\SNOOPY&lt;br /&gt;&lt;br /&gt;APPDATA=C:\Documents and Settings\Administrator\Application Data&lt;br /&gt;By the way, if you add a new variable via the command prompt, you will not see it when you try to read it via the script. You can read only the new values created via the same scripting type you used to create them. Although I've tested this only to a limited extent, it seems to be true. Try it for yourself; just open a command prompt, type Set DUDE=Dudeworks, and press Enter to set the new environment variable. Now, when you execute GetEnvVars.vbs, and you'll notice that it does not list that new variable. However, if you type SET at the command prompt, you will see it.&lt;br /&gt;&lt;br /&gt;—Rob Olson&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3376060701639754039-2191776867912406974?l=booksrthere.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://booksrthere.blogspot.com/feeds/2191776867912406974/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3376060701639754039&amp;postID=2191776867912406974' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3376060701639754039/posts/default/2191776867912406974'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3376060701639754039/posts/default/2191776867912406974'/><link rel='alternate' type='text/html' href='http://booksrthere.blogspot.com/2008/03/hack-9-add-remove-or-retrieve.html' title='Hack 9 Add, Remove, or Retrieve Environment Variables'/><author><name>jerin</name><uri>http://www.blogger.com/profile/10103476469626499932</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3376060701639754039.post-2708118105393033182</id><published>2008-03-26T09:30:00.000-07:00</published><updated>2008-03-26T09:31:26.817-07:00</updated><title type='text'>Hack 8 Execute a Command on Each Computer in a Domain</title><content type='html'>This handy script lets you easily run any command on a specified subset of computers in your domain.&lt;br /&gt;&lt;br /&gt;Running the same command on multiple computers in your domain can be tedious indeed, but such a scenario is common in an administrator's life. I've written this hack to make this chore easier. The script traverses member systems of a domain, executing a command against each system that has a name that matches a particular specification you specify in the command line. Note that regular expressions are legal in this script, which makes it a powerful and flexible addition to the administrator's toolkit.&lt;br /&gt;&lt;br /&gt;The Code&lt;br /&gt;To use this script, type it into a text editor such as Notepad (make sure Word Wrap is disabled) and save it with a .vbs extension as ExecuteAll.vbs. Alternatively, if you don't want to wear your fingers out, you can download the script from the O'Reilly web site.&lt;br /&gt;&lt;br /&gt;'Script Name: ExecuteAll.vbs&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Option Explicit&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Dim oDomain, oService, oItem, oShell&lt;br /&gt;&lt;br /&gt;Dim strDomain, strSpec, strCommand, intButton&lt;br /&gt;&lt;br /&gt;Dim oArgs, strFinalCommand, oRegEx, boolConfirm&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;' Prepare to execute commands &amp; do popups&lt;br /&gt;&lt;br /&gt;Set oShell = CreateObject("WScript.Shell")&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;GetArguments&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;' Access the domain so we can traverse objects&lt;br /&gt;&lt;br /&gt;WScript.Echo "Accessing NT Domain " &amp; strDomain&lt;br /&gt;&lt;br /&gt;Set oDomain = GetObject("WinNT://" &amp; strDomain)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;' Initiate our regular expression support&lt;br /&gt;&lt;br /&gt;Set oRegEx = New RegExp&lt;br /&gt;&lt;br /&gt;oRegEx.Pattern = strSpec&lt;br /&gt;&lt;br /&gt;oRegEx.IgnoreCase = True&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;' Traverse each computer (WinNT) object in the domain&lt;br /&gt;&lt;br /&gt;WScript.Echo "Searching for " &amp; strSpec&lt;br /&gt;&lt;br /&gt;oDomain.Filter = Array("Computer") ' only look at computers&lt;br /&gt;&lt;br /&gt;For Each oItem In oDomain&lt;br /&gt;&lt;br /&gt;If oRegEx.Test(oItem.Name) Then&lt;br /&gt;&lt;br /&gt;WScript.Echo " Matched " &amp; oItem.Name&lt;br /&gt;&lt;br /&gt;strFinalCommand = Replace(strCommand, "$n", oItem.Name)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;intButton = vbNo&lt;br /&gt;&lt;br /&gt;If boolConfirm Then&lt;br /&gt;&lt;br /&gt;intButton = oShell.Popup("Execute " &amp; strFinalCommand &amp; "?",,_&lt;br /&gt;&lt;br /&gt;"System " &amp; oItem.Name, vbYesno + vbQuestion)&lt;br /&gt;&lt;br /&gt;End If&lt;br /&gt;&lt;br /&gt;If (boolConfirm = False) Or (intButton = vbYes) Then&lt;br /&gt;&lt;br /&gt;WScript.Echo " Executing: " &amp; strFinalCommand&lt;br /&gt;&lt;br /&gt;execute strFinalCommand&lt;br /&gt;&lt;br /&gt;End If&lt;br /&gt;&lt;br /&gt;End If&lt;br /&gt;&lt;br /&gt;Next&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;' All done; clean up&lt;br /&gt;&lt;br /&gt;Set oItem = Nothing&lt;br /&gt;&lt;br /&gt;Set oRegEx = Nothing&lt;br /&gt;&lt;br /&gt;Set oDomain = Nothing&lt;br /&gt;&lt;br /&gt;Set oShell = Nothing&lt;br /&gt;&lt;br /&gt;Set oArgs = Nothing&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;'&lt;br /&gt;&lt;br /&gt;' Glean the arguments for our run from the command line, if provided.&lt;br /&gt;&lt;br /&gt;' If any are missing, prompt for input. A blank input signals an abort.&lt;br /&gt;&lt;br /&gt;'&lt;br /&gt;&lt;br /&gt;' /Y is an optional last argument&lt;br /&gt;&lt;br /&gt;Sub GetArguments&lt;br /&gt;&lt;br /&gt;Dim i, strConfirm, intButton&lt;br /&gt;&lt;br /&gt;Set oArgs = WScript.Arguments&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;boolConfirm = True ' assume always confirm&lt;br /&gt;&lt;br /&gt;strDomain = "" ' domain to be traversed&lt;br /&gt;&lt;br /&gt;strSpec = "" ' name specification to be matched&lt;br /&gt;&lt;br /&gt;strCommand = "" ' command to be executed on each match&lt;br /&gt;&lt;br /&gt;strConfirm = "" ' track prompting for confirmation setting&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;' Look for our optional 4th argument&lt;br /&gt;&lt;br /&gt;If oArgs.Length = 4 Then&lt;br /&gt;&lt;br /&gt;If UCase(oArgs.Item(3)) = "/Y" Then&lt;br /&gt;&lt;br /&gt;boolConfirm = False&lt;br /&gt;&lt;br /&gt;strConfirm = "/Y" ' don't prompt below&lt;br /&gt;&lt;br /&gt;End If&lt;br /&gt;&lt;br /&gt;End If&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;' Look for any specified arguments, in order&lt;br /&gt;&lt;br /&gt;If oArgs.Length &gt;= 1 Then strDomain = oArgs(0)&lt;br /&gt;&lt;br /&gt;If oArgs.Length &gt;= 2 Then strSpec = oArgs(1)&lt;br /&gt;&lt;br /&gt;If oArgs.Length &gt;= 3 Then strCommand = oArgs(2)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;' Prompt for any arguments not specified on the command line&lt;br /&gt;&lt;br /&gt;If strDomain = "" Then&lt;br /&gt;&lt;br /&gt;strDomain = InputBox _&lt;br /&gt;&lt;br /&gt;("Enter the name of the NT Domain to be traversed", _&lt;br /&gt;&lt;br /&gt;"NT Domain")&lt;br /&gt;&lt;br /&gt;End If&lt;br /&gt;&lt;br /&gt;If strDomain = "" Then WScript.Quit&lt;br /&gt;&lt;br /&gt;strDomain = UCase(strDomain)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;If strSpec = "" Then&lt;br /&gt;&lt;br /&gt;strSpec = InputBox _&lt;br /&gt;&lt;br /&gt;("Enter your name specification for the computer(s) " &amp; _&lt;br /&gt;&lt;br /&gt;"that will be matched within the " &amp; strDomain &amp; " Domain." &amp; _&lt;br /&gt;&lt;br /&gt;vbCrlf &amp; "Regular Expressions are acceptable.", _&lt;br /&gt;&lt;br /&gt;"Name Specification")&lt;br /&gt;&lt;br /&gt;End If&lt;br /&gt;&lt;br /&gt;If strSpec = "" Then WScript.Quit&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;If strCommand = "" Then&lt;br /&gt;&lt;br /&gt;strCommand = InputBox _&lt;br /&gt;&lt;br /&gt;("Enter the command to be executed on each computer matching " &amp; _&lt;br /&gt;&lt;br /&gt;strSpec &amp; " within the " &amp; strDomain &amp; " Domain." &amp; _&lt;br /&gt;&lt;br /&gt;vbCrlf &amp; "$n will be substituted for the computer name.", _&lt;br /&gt;&lt;br /&gt;"Command to Execute")&lt;br /&gt;&lt;br /&gt;End If&lt;br /&gt;&lt;br /&gt;If strCommand = "" Then WScript.Quit&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;If strConfirm = "" Then&lt;br /&gt;&lt;br /&gt;intButton = oShell.Popup("Confirm each command prior to execution?",,_&lt;br /&gt;&lt;br /&gt;"Confirm?", vbYesNo + vbQuestion)&lt;br /&gt;&lt;br /&gt;If intButton = vbNo Then&lt;br /&gt;&lt;br /&gt;boolConfirm = False&lt;br /&gt;&lt;br /&gt;End If&lt;br /&gt;&lt;br /&gt;End If&lt;br /&gt;&lt;br /&gt;End Sub&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;' Execute a command. Each is always run under a new instance of the command&lt;br /&gt;&lt;br /&gt;' processor. This allows the use of built-in commands and I/O redirection.&lt;br /&gt;&lt;br /&gt;'&lt;br /&gt;&lt;br /&gt;' We won't wait for command completion.&lt;br /&gt;&lt;br /&gt;Sub Execute(strCommand)&lt;br /&gt;&lt;br /&gt;Dim RetVal&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;strCommand = "%COMSPEC% /c " &amp; strCommand&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;RetVal = oShell.Run(strCommand, 1, False)&lt;br /&gt;&lt;br /&gt;End Sub&lt;br /&gt;Running the Hack&lt;br /&gt;Here is the syntax for running the script:&lt;br /&gt;&lt;br /&gt;ExexcuteAll.vbs &lt;DomainToTraverse&gt; &lt;ComputerSpecification&gt; &lt;Command&gt; [/Y]&lt;br /&gt;When the script runs, the matched system's name will be substituted for the occurrence of $n in the command to be performed. By default, each command instance is confirmed before it is executed, but you can specify /Y to always answer Yes instead.&lt;br /&gt;&lt;br /&gt;Here's an example of how to run the script:&lt;br /&gt;&lt;br /&gt;ExexcuteAll.vbs MYDOMAIN WKSATL* "del \\$n\admin$\activitylog.txt"&lt;br /&gt;This example traverses the MYDOMAIN domain, looking for computer names that start with WKSATL* (note the wildcard) and deletes the activitylog.txt file from the C:\Winnt folder.&lt;br /&gt;&lt;br /&gt;—Hans Schefske&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3376060701639754039-2708118105393033182?l=booksrthere.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://booksrthere.blogspot.com/feeds/2708118105393033182/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3376060701639754039&amp;postID=2708118105393033182' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3376060701639754039/posts/default/2708118105393033182'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3376060701639754039/posts/default/2708118105393033182'/><link rel='alternate' type='text/html' href='http://booksrthere.blogspot.com/2008/03/hack-8-execute-command-on-each-computer.html' title='Hack 8 Execute a Command on Each Computer in a Domain'/><author><name>jerin</name><uri>http://www.blogger.com/profile/10103476469626499932</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3376060701639754039.post-3723570914587084434</id><published>2008-03-26T09:29:00.000-07:00</published><updated>2008-03-26T09:30:35.579-07:00</updated><title type='text'>Hack 7 Rename Mapped Drives</title><content type='html'>Renaming drive mappings can be done in several ways, but automating the process is most efficient using a script.&lt;br /&gt;&lt;br /&gt;Occasionally, an administrator might need to change drive-mapping names to hide share paths or to make the drive name user-friendly. This is an easy operation when done manually through a console, but when you try to automate this task, it becomes a little more difficult. Because mapped drives are not partitions on the local hard disk, common DOS commands, such as label, can't be used. Most drive-mapping commands, such as net use, don't have a way to customize the name of the mapped drive either.&lt;br /&gt;&lt;br /&gt;One common way to perform this task is to hack the following Registry key and add the _LabelFromReg string value:&lt;br /&gt;&lt;br /&gt;HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2\%key%&lt;br /&gt;Here, the %key% variable is the drive letter to be changed.&lt;br /&gt;&lt;br /&gt;There is a whole host of ways to make this method work, either by editing the Registry directly, via script, or by importing a .reg file using regedit /c. All of these methods require many steps and some require external files, so they might not fit into every administrative scheme. But there's an easier approach.&lt;br /&gt;&lt;br /&gt;The Code&lt;br /&gt;As it turns out, our old friend VBScript can be used to make this task a little more seamless. This simple script can be used on mapped drives as well as local partitions:&lt;br /&gt;&lt;br /&gt;mDrive = "drive letter"&lt;br /&gt;&lt;br /&gt;Set oShell = CreateObject("Shell.Application")&lt;br /&gt;&lt;br /&gt;oShell.NameSpace(mDrive).Self.Name = "AnyName"&lt;br /&gt;Running the Hack&lt;br /&gt;To use this hack, simply edit the script to change the drive letter and drive name as desired. For example, if E: is a mapped drive that has the label Budgets on 172.16.33.14, and you want to change the label on the mapped drive to simply Budgets, change this line:&lt;br /&gt;&lt;br /&gt;mDrive = "drive letter"&lt;br /&gt;to this:&lt;br /&gt;&lt;br /&gt;mDrive = "e:\"&lt;br /&gt;Then, change this line:&lt;br /&gt;&lt;br /&gt;oShell.NameSpace(mDrive).Self.Name = "AnyName"&lt;br /&gt;to this:&lt;br /&gt;&lt;br /&gt;oShell.NameSpace(mDrive).Self.Name = "Budgets"&lt;br /&gt;Finally, run the script by creating a shortcut to it and double-clicking on the shortcut, by calling it from a logon script, or by any other method suitable for your environment.&lt;br /&gt;&lt;br /&gt;—Michael Brainard&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3376060701639754039-3723570914587084434?l=booksrthere.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://booksrthere.blogspot.com/feeds/3723570914587084434/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3376060701639754039&amp;postID=3723570914587084434' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3376060701639754039/posts/default/3723570914587084434'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3376060701639754039/posts/default/3723570914587084434'/><link rel='alternate' type='text/html' href='http://booksrthere.blogspot.com/2008/03/hack-7-rename-mapped-drives_26.html' title='Hack 7 Rename Mapped Drives'/><author><name>jerin</name><uri>http://www.blogger.com/profile/10103476469626499932</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3376060701639754039.post-8623573222881001591</id><published>2008-03-26T09:26:00.000-07:00</published><updated>2008-03-26T09:27:46.078-07:00</updated><title type='text'>Hack 6 Shut Down a Remote Computer</title><content type='html'>Here's a nifty way to use a script to shut down remote machines.&lt;br /&gt;&lt;br /&gt;Sometimes, you need to be able to shut down a server remotely. This script pings the computer in question prior to sending the Win32Shutdown method. It operates on remote PCs and has been tested on systems running Windows 2000. It will probably work on NT4 systems with the proper WHS/WMI/VB scripting, though it has not been tested on such systems.&lt;br /&gt;&lt;br /&gt;Using the Win32Shutdown method, the script provides you with the option of logging off the current user of the machine, powering the machine down, or rebooting it. In addition, each of these options can be forced so that the action occurs even if applications are running. Use this option carefully, though, because it might cause the logged-on user to lose his work if he has open files. Note that forced log off/power down/reboot will not work if the screen saver is password-protected and is currently active.&lt;br /&gt;&lt;br /&gt;The Code&lt;br /&gt;Make sure you have the latest scripting engines on the workstation you run this script from. You can download the latest scripting engines at the Microsoft Scripting home page (http://msdn.microsoft.com/library/default.asp?url=/nhp/default.asp?contentid=28001169). Note that, when working with the Active Directory Services Interface (ADSI), you must have the same applicable rights as you need to use the built-in administrative tools. Also, for VB scripts that interact with Windows Management Instrumentation (WMI), apply the most current version of the WMI agents.&lt;br /&gt;&lt;br /&gt;Type the following code into a text editor such as Notepad (making sure to have Word Wrap disabled) and save it with a .vbs extension. Alternatively, you can download the RemoteShutdown.vbs script from the O'Reilly web site at http://www.oreilly.com/catalog/winsvrhks/.&lt;br /&gt;&lt;br /&gt;'/'|| RemoteShutdown.vbs&lt;br /&gt;&lt;br /&gt;'||&lt;br /&gt;&lt;br /&gt;'|| Created by Harvey Hendricks, MCP, MCSE, A+&lt;br /&gt;&lt;br /&gt;'|| March 2001&lt;br /&gt;&lt;br /&gt;'|| email: Harvey.Hendricks@aramcoservices.com&lt;br /&gt;&lt;br /&gt;'||&lt;br /&gt;&lt;br /&gt;'||&lt;br /&gt;&lt;br /&gt;'|| Based on techniques and ideas from:&lt;br /&gt;&lt;br /&gt;'|| SMS admin, SMS Installer, &amp; WMI forums -&gt;&lt;br /&gt;&lt;br /&gt;'|| http://www.myITforum.com/forums&lt;br /&gt;&lt;br /&gt;'|| Win32 Scripting -&gt; http://cwashington.netreach.net/&lt;br /&gt;&lt;br /&gt;'|| Microsoft Windows Script Technologies -&gt;&lt;br /&gt;&lt;br /&gt;'|| http://msdn.microsoft.com/scripting&lt;br /&gt;&lt;br /&gt;'|| Microsoft Online Library -&gt; &lt;br /&gt;&lt;br /&gt;'|| http://msdn.microsoft.com/library/default.asp&lt;br /&gt;&lt;br /&gt;'|| Microsoft VBScript 5.5 documentation and Microsoft WMI SDK&lt;br /&gt;&lt;br /&gt;'||&lt;br /&gt;&lt;br /&gt;'||~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~&lt;br /&gt;&lt;br /&gt;'|| SCRIPT LOGIC FLOW:&lt;br /&gt;&lt;br /&gt;'|| Collects computername from user, calls function to ping the computername&lt;br /&gt;&lt;br /&gt;'|| to determine if it is accessible, if not then display message and exit&lt;br /&gt;&lt;br /&gt;'|| otherwise continue.&lt;br /&gt;&lt;br /&gt;'|| Collects desired action to perform from the user, does error checking on&lt;br /&gt;&lt;br /&gt;'|| the input to determine if it is acceptable, if not then display message&lt;br /&gt;&lt;br /&gt;'|| and exit otherwise continue.&lt;br /&gt;&lt;br /&gt;'|| Set variables and output messages based on the action chosen. Calls&lt;br /&gt;&lt;br /&gt;'|| Win32Shutdown with the appropriate variable. Displays success message&lt;br /&gt;&lt;br /&gt;'|| and exits&lt;br /&gt;&lt;br /&gt;'||&lt;br /&gt;&lt;br /&gt;'|| Uses WMI Win32Shutdown method from the Win32_OperatingSystem class&lt;br /&gt;&lt;br /&gt;'|| to perform different logoff / powerdown / reboot functions&lt;br /&gt;&lt;br /&gt;'||&lt;br /&gt;&lt;br /&gt;'|| Testing found the following values to be effective on Win32Shutdown:&lt;br /&gt;&lt;br /&gt;'|| Action decimal binary&lt;br /&gt;&lt;br /&gt;'|| Logoff 0 0000&lt;br /&gt;&lt;br /&gt;'|| Force Logoff 4 0100&lt;br /&gt;&lt;br /&gt;'|| Reboot 2 0010&lt;br /&gt;&lt;br /&gt;'|| Force Reboot 6 0110&lt;br /&gt;&lt;br /&gt;'|| Powerdown 8 1000&lt;br /&gt;&lt;br /&gt;'|| Force Powerdown 12 1100&lt;br /&gt;&lt;br /&gt;'||&lt;br /&gt;&lt;br /&gt;'|| Notice that the third bit from the right appears to be the "FORCE" bit.&lt;br /&gt;&lt;br /&gt;'||&lt;br /&gt;&lt;br /&gt;'|| A value of 1 will do a shutdown, ending at the "It is safe to turn&lt;br /&gt;&lt;br /&gt;'|| off your computer" screen. I have no use for this and did not test it.&lt;br /&gt;&lt;br /&gt;'||&lt;br /&gt;&lt;br /&gt;'||&lt;br /&gt;&lt;br /&gt;'||NOTES: - tested under Windows 2000 Pro. with ACPI compliant systems -&lt;br /&gt;&lt;br /&gt;'|| SHOULD work under Windows NT4 without modification IF the&lt;br /&gt;&lt;br /&gt;'|| system has compatible versions of WSH / WMI / VBscripting&lt;br /&gt;&lt;br /&gt;'||&lt;br /&gt;&lt;br /&gt;'||Logoff / Powerdown / Reboot:&lt;br /&gt;&lt;br /&gt;'|| Does not work if a password protected screen saver is active or&lt;br /&gt;&lt;br /&gt;'|| there is data to save. Either way the system waits for user input.&lt;br /&gt;&lt;br /&gt;'||&lt;br /&gt;&lt;br /&gt;'||Force Logoff / Force Powerdown / Force Reboot:&lt;br /&gt;&lt;br /&gt;'|| Does not work if a password protected screen saver is active, will wait&lt;br /&gt;&lt;br /&gt;'|| for user input. Otherwise will close open applications without saving&lt;br /&gt;&lt;br /&gt;'|| data.&lt;br /&gt;&lt;br /&gt;'||&lt;br /&gt;&lt;br /&gt;'\/~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;'/\/\/\/\/\/\/\/\/\/\/\/\/\/\ start function &lt;br /&gt;&lt;br /&gt;function Ping(byval strName)&lt;br /&gt;&lt;br /&gt;dim objFSO, objShell, objTempFile, objTS&lt;br /&gt;&lt;br /&gt;dim sCommand, sReadLine&lt;br /&gt;&lt;br /&gt;dim bReturn&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;set objShell = WScript.CreateObject("Wscript.Shell")&lt;br /&gt;&lt;br /&gt;set objFSO = CreateObject("Scripting.FileSystemObject")&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;'Set default return value&lt;br /&gt;&lt;br /&gt;bReturn = false&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;'Create command line to ping and save results to a temp file&lt;br /&gt;&lt;br /&gt;sCommand = "cmd /c ping.exe -n 3 -w 1000 " &amp; strName &amp; " &gt; C:\temp.txt"&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;'Execute the command&lt;br /&gt;&lt;br /&gt;objShell.run sCommand, 0, true&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;'Get the temp file&lt;br /&gt;&lt;br /&gt;set objTempFile = objFSO.GetFile("C:\temp.txt")&lt;br /&gt;&lt;br /&gt;set objTS = objTempFile.OpenAsTextStream(1)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;'Loop through the temp file to see if "reply from" is found,&lt;br /&gt;&lt;br /&gt;'if it is then the ping was successful&lt;br /&gt;&lt;br /&gt;do while objTs.AtEndOfStream &lt;&gt; true&lt;br /&gt;&lt;br /&gt;sReadLine = objTs.ReadLine&lt;br /&gt;&lt;br /&gt;if instr(lcase(sReadLine), "reply from") &gt; 0 then&lt;br /&gt;&lt;br /&gt;bReturn = true&lt;br /&gt;&lt;br /&gt;exit do&lt;br /&gt;&lt;br /&gt;end if&lt;br /&gt;&lt;br /&gt;loop&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;'Close temp file and release objects&lt;br /&gt;&lt;br /&gt;objTS.close&lt;br /&gt;&lt;br /&gt;objTempFile.delete&lt;br /&gt;&lt;br /&gt;set objTS = nothing&lt;br /&gt;&lt;br /&gt;set objTempFile = nothing&lt;br /&gt;&lt;br /&gt;set objShell = nothing&lt;br /&gt;&lt;br /&gt;set objFSO = nothing&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;'Return value&lt;br /&gt;&lt;br /&gt;Ping = bReturn&lt;br /&gt;&lt;br /&gt;end function&lt;br /&gt;&lt;br /&gt;'/\/\/\/\/\/\/\/\/\/\/\/\/\/\ end function&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;'/\/\/\/\/\/\/\/\/\/\/\ Start Main body of script &lt;br /&gt;&lt;br /&gt;'Get computer name to operate on&lt;br /&gt;&lt;br /&gt;ComputerName=InputBox("Enter the Machine name of the computer" &amp; vbCRLF _&lt;br /&gt;&lt;br /&gt;&amp; "you wish to Shutdown / Reboot / Logoff", _&lt;br /&gt;&lt;br /&gt;"Remote Shutdown / Reboot / Logoff", _&lt;br /&gt;&lt;br /&gt;"ComputerName")&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;'if Cancel selected - exit &lt;br /&gt;&lt;br /&gt;If (ComputerName = "") Then Wscript.Quit&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;'change the name to uppercase&lt;br /&gt;&lt;br /&gt;ComputerName=UCase(ComputerName)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;'ping the computername to see if it is accessible&lt;br /&gt;&lt;br /&gt;bPingtest = ping(Computername)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;If bPingtest = FALSE Then&lt;br /&gt;&lt;br /&gt;y = msgbox ("'" &amp; ComputerName &amp; "' is not accessible!" &amp; vbCRLF _&lt;br /&gt;&lt;br /&gt;&amp; "It may be offline or turned off." &amp; vbCRLF _&lt;br /&gt;&lt;br /&gt;&amp; "Check the name for a typo." &amp; vbCRLF, _&lt;br /&gt;&lt;br /&gt;vbCritical, ComputerName &amp; " NOT RESPONDING")&lt;br /&gt;&lt;br /&gt;Wscript.Quit&lt;br /&gt;&lt;br /&gt;end IF&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;'Get the action desired&lt;br /&gt;&lt;br /&gt;Action=InputBox( _&lt;br /&gt;&lt;br /&gt;"Select Action to perform on " &amp; ComputerName &amp; vbCRLF &amp; vbCRLF _&lt;br /&gt;&lt;br /&gt;&amp; " 1 - Logoff" &amp; vbCRLF _&lt;br /&gt;&lt;br /&gt;&amp; " 2 - Force Logoff ( NO SAVE )" &amp; vbCRLF _&lt;br /&gt;&lt;br /&gt;&amp; " 3 - Powerdown" &amp; vbCRLF _&lt;br /&gt;&lt;br /&gt;&amp; " 4 - Force Powerdown ( NO SAVE )" &amp; vbCRLF _&lt;br /&gt;&lt;br /&gt;&amp; " 5 - Reboot" &amp; vbCRLF _&lt;br /&gt;&lt;br /&gt;&amp; " 6 - Force Reboot ( NO SAVE )" &amp; vbCRLF &amp; vbCRLF _&lt;br /&gt;&lt;br /&gt;&amp; "NOTE:" &amp; vbCRLF _&lt;br /&gt;&lt;br /&gt;&amp; " Using Force will close windows" &amp; vbCRLF _&lt;br /&gt;&lt;br /&gt;&amp; " without saving changes!", _&lt;br /&gt;&lt;br /&gt;"Select action to perform on " &amp; ComputerName, "")&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;'if Cancel selected - exit &lt;br /&gt;&lt;br /&gt;If (Action = "") Then Wscript.Quit&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;'error check input&lt;br /&gt;&lt;br /&gt;If (INSTR("1234567",Action)=0) OR (Len(Action)&gt;1) then&lt;br /&gt;&lt;br /&gt;y = msgbox("Unacceptable input passed -- '" &amp; Action &amp; "'", _&lt;br /&gt;&lt;br /&gt;vbOKOnly + vbCritical, "That was SOME bad input!")&lt;br /&gt;&lt;br /&gt;Wscript.Quit&lt;br /&gt;&lt;br /&gt;end if&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;'set flag to disallow action unless proper input achieved, 1 =&gt; go 0 =&gt; nogo&lt;br /&gt;&lt;br /&gt;flag = 0&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;'set variables according to computername and action&lt;br /&gt;&lt;br /&gt;Select Case Action&lt;br /&gt;&lt;br /&gt;Case 1 'Logoff&lt;br /&gt;&lt;br /&gt;x = 0&lt;br /&gt;&lt;br /&gt;strAction = "Logoff sent to " &amp; ComputerName&lt;br /&gt;&lt;br /&gt;flag = 1&lt;br /&gt;&lt;br /&gt;Case 2 'Force Logoff &lt;br /&gt;&lt;br /&gt;x = 4&lt;br /&gt;&lt;br /&gt;strAction = "Force Logoff sent to " &amp; ComputerName&lt;br /&gt;&lt;br /&gt;flag = 1&lt;br /&gt;&lt;br /&gt;Case 3 'Powerdown&lt;br /&gt;&lt;br /&gt;x = 8&lt;br /&gt;&lt;br /&gt;strAction = "Powerdown sent to " &amp; ComputerName&lt;br /&gt;&lt;br /&gt;flag = 1&lt;br /&gt;&lt;br /&gt;Case 4 'Force Powerdown&lt;br /&gt;&lt;br /&gt;x = 12&lt;br /&gt;&lt;br /&gt;strAction = "Force Powerdown sent to " &amp; ComputerName&lt;br /&gt;&lt;br /&gt;flag = 1&lt;br /&gt;&lt;br /&gt;Case 5 'Reboot&lt;br /&gt;&lt;br /&gt;x = 2&lt;br /&gt;&lt;br /&gt;strAction = "Reboot sent to " &amp; ComputerName&lt;br /&gt;&lt;br /&gt;flag = 1&lt;br /&gt;&lt;br /&gt;Case 6 'Force Reboot&lt;br /&gt;&lt;br /&gt;x = 6&lt;br /&gt;&lt;br /&gt;strAction = "Force Reboot sent to " &amp; ComputerName&lt;br /&gt;&lt;br /&gt;flag = 1&lt;br /&gt;&lt;br /&gt;Case 7 'Test dialog boxes&lt;br /&gt;&lt;br /&gt;y = msgbox("Test complete", vbOKOnly + vbInformation, "Dialog Box Test Complete")&lt;br /&gt;&lt;br /&gt;flag = 0&lt;br /&gt;&lt;br /&gt;Case Else 'Default -- should never happen&lt;br /&gt;&lt;br /&gt;y = msgbox("Error occurred in passing parameters." _&lt;br /&gt;&lt;br /&gt;&amp; vbCRLF &amp; " Passed '" &amp; Action &amp; "'", _&lt;br /&gt;&lt;br /&gt;vbOKOnly + vbCritical, "PARAMETER ERROR")&lt;br /&gt;&lt;br /&gt;flag = 0&lt;br /&gt;&lt;br /&gt;End Select&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;'check flag&lt;br /&gt;&lt;br /&gt;' if equal 1 (TRUE) then perform Win32Shutdown action on remote PC&lt;br /&gt;&lt;br /&gt;' and display a confirmation message&lt;br /&gt;&lt;br /&gt;' if not equal 1 (FALSE) then skip the action and script ends&lt;br /&gt;&lt;br /&gt;if flag then&lt;br /&gt;&lt;br /&gt;Set OpSysSet=GetObject("winmgmts:{(Debug,RemoteShutdown)}//" _&lt;br /&gt;&lt;br /&gt;&amp; ComputerName &amp; "/root/cimv2").ExecQuery( _&lt;br /&gt;&lt;br /&gt;"Select * from Win32_OperatingSystem where Primary=true")&lt;br /&gt;&lt;br /&gt;for each OpSys in OpSysSet&lt;br /&gt;&lt;br /&gt;OpSys.Win32Shutdown(x)&lt;br /&gt;&lt;br /&gt;y = msgbox(strAction,vbOKOnly + vbInformation,"Mission Accomplished")&lt;br /&gt;&lt;br /&gt;next&lt;br /&gt;&lt;br /&gt;end If&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;'Release objects&lt;br /&gt;&lt;br /&gt;set OpSys = nothing&lt;br /&gt;&lt;br /&gt;set OpSysSet = nothing&lt;br /&gt;Running the Hack&lt;br /&gt;To run the hack, simply double-click on the RemoteShutdown.vbs file in Windows Explorer (or a shortcut to this file on your desktop) and type the name of the remote computer you want to log off from, power down, or reboot. This name can be the NetBIOS name, DNS name, or IP address of the remote machine. You will then be presented with an input box that displays a menu of options:&lt;br /&gt;&lt;br /&gt;1 - Logoff&lt;br /&gt;2 - Force Logoff&lt;br /&gt;3 - Powerdown&lt;br /&gt;4 - Force Powerdown&lt;br /&gt;5 - Reboot&lt;br /&gt;6 - Force Reboot&lt;br /&gt;Simply type the number for the action you want to perform and press Enter.&lt;br /&gt;&lt;br /&gt;—Harvey Hendricks&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3376060701639754039-8623573222881001591?l=booksrthere.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://booksrthere.blogspot.com/feeds/8623573222881001591/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3376060701639754039&amp;postID=8623573222881001591' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3376060701639754039/posts/default/8623573222881001591'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3376060701639754039/posts/default/8623573222881001591'/><link rel='alternate' type='text/html' href='http://booksrthere.blogspot.com/2008/03/hack-6-shut-down-remote-computer.html' title='Hack 6 Shut Down a Remote Computer'/><author><name>jerin</name><uri>http://www.blogger.com/profile/10103476469626499932</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3376060701639754039.post-5983495484088163973</id><published>2008-03-26T09:25:00.000-07:00</published><updated>2008-03-26T09:26:23.462-07:00</updated><title type='text'>Hack 5 Wait for and Optionally Terminate a Process</title><content type='html'>If you've wondered how to write code that waits for a process to finish before terminating it, here's the answer.&lt;br /&gt;&lt;br /&gt;I have seen a number of discussions regarding the need for a VB script that waits for a process to finish. The script in this hack does this and more: it waits for a process to finish and optionally terminates the process if it has not finished within a specified amount of time.&lt;br /&gt;&lt;br /&gt;This code is a modified form of what I use to control my software deployments, and it has two purposes. First, the code is designed to be certain that the deployment script waits until the initiated software setup executable is fully finished before proceeding. Even though the majority of recent software releases do not require this functionality when being deployed, it is still required for some legacy installations. Second, the code can perform a forceful termination of an application if this functionality is required.&lt;br /&gt;&lt;br /&gt;This script accepts three arguments: the name of the executable to wait for or terminate, the amount of time to wait before terminating the specified executable, and (optionally) a switch specifying that the script should run silently. Note that the script uses Windows Management Instrumentation (WMI) for the process-management tasks, so make sure you're running the latest WMI version on your machine.&lt;br /&gt;&lt;br /&gt;The Code&lt;br /&gt;The script consists of several sections, which are described inline in the following sections.&lt;br /&gt;&lt;br /&gt;Main routine&lt;br /&gt;First, command-line switches are read in the main body area:&lt;br /&gt;&lt;br /&gt;Option Explicit&lt;br /&gt;&lt;br /&gt;'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''&lt;br /&gt;&lt;br /&gt;'&lt;br /&gt;&lt;br /&gt;' File:     vbsWaitForProcess.vbs&lt;br /&gt;&lt;br /&gt;' Updated:  Nov 2002&lt;br /&gt;&lt;br /&gt;' Version:  1.0&lt;br /&gt;&lt;br /&gt;' Author:   Dan Thomson, myITforum.com columnist&lt;br /&gt;&lt;br /&gt;'           I can be contacted at dethomson@hotmail.com&lt;br /&gt;&lt;br /&gt;'&lt;br /&gt;&lt;br /&gt;' Usage:    The command processor version must be run using cscript&lt;br /&gt;&lt;br /&gt;'           cscript vbsWaitForProcess.vbs notepad.exe 60 S&lt;br /&gt;&lt;br /&gt;'           or&lt;br /&gt;&lt;br /&gt;'           The IE and Popup versions can be run with cscript or wscript&lt;br /&gt;&lt;br /&gt;'           wscript vbsWaitForProcess.vbs notepad.exe -1&lt;br /&gt;&lt;br /&gt;'&lt;br /&gt;&lt;br /&gt;' Input:    Name of executable  (ex: notepad.exe)&lt;br /&gt;&lt;br /&gt;'           Time to wait in seconds before terminating the executable&lt;br /&gt;&lt;br /&gt;'               -1 waits indefinitely for the process to finish&lt;br /&gt;&lt;br /&gt;'               0 terminates the process imediately&lt;br /&gt;&lt;br /&gt;'               Any value &gt; 0 will cause the script to wait the specified&lt;br /&gt;&lt;br /&gt;'               amount of time in seconds before terminating the process&lt;br /&gt;&lt;br /&gt;'           Silent mode  (S)&lt;br /&gt;&lt;br /&gt;'&lt;br /&gt;&lt;br /&gt;' Notes:&lt;br /&gt;&lt;br /&gt;'&lt;br /&gt;&lt;br /&gt;''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;On Error Resume Next&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;'Define some variables&lt;br /&gt;&lt;br /&gt;Dim strProcess&lt;br /&gt;&lt;br /&gt;Dim intWaitTime&lt;br /&gt;&lt;br /&gt;Dim strSilent&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;'Get the command line arguments&lt;br /&gt;&lt;br /&gt;strProcess = Wscript.Arguments.Item(0)&lt;br /&gt;&lt;br /&gt;intWaitTime = CInt(Wscript.Arguments.Item(1))&lt;br /&gt;&lt;br /&gt;strSilent = Wscript.Arguments.Item(2)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Call WaitForProcess (strProcess, intWaitTime, strSilent)&lt;br /&gt;Check if process is running&lt;br /&gt;Next, the ProcessIsRunning function determines if a process is running:&lt;br /&gt;&lt;br /&gt;'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''&lt;br /&gt;&lt;br /&gt;'&lt;br /&gt;&lt;br /&gt;' Function: ProcessIsRunning&lt;br /&gt;&lt;br /&gt;'&lt;br /&gt;&lt;br /&gt;' Purpose:  Determine if a process is running&lt;br /&gt;&lt;br /&gt;'&lt;br /&gt;&lt;br /&gt;' Input:    Name of process&lt;br /&gt;&lt;br /&gt;'&lt;br /&gt;&lt;br /&gt;' Output:   True or False depending on if the process is running&lt;br /&gt;&lt;br /&gt;'&lt;br /&gt;&lt;br /&gt;''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''&lt;br /&gt;&lt;br /&gt;Private Function ProcessIsRunning( strProcess )&lt;br /&gt;&lt;br /&gt;    Dim colProcessList&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;    Set colProcessList = Getobject("Winmgmts:").Execquery _&lt;br /&gt;&lt;br /&gt;        ("Select * from Win32_Process Where Name ='" &amp; strProcess &amp; "'")&lt;br /&gt;&lt;br /&gt;    If colProcessList.Count &gt; 0 Then&lt;br /&gt;&lt;br /&gt;        ProcessIsRunning = True&lt;br /&gt;&lt;br /&gt;    Else&lt;br /&gt;&lt;br /&gt;        ProcessIsRunning = False&lt;br /&gt;&lt;br /&gt;    End If&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;    Set colProcessList = Nothing&lt;br /&gt;&lt;br /&gt;End Function&lt;br /&gt;Terminate the process&lt;br /&gt;In the next section, the ProcessTerminate function terminates a process:&lt;br /&gt;&lt;br /&gt;'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''&lt;br /&gt;&lt;br /&gt;'&lt;br /&gt;&lt;br /&gt;' Function: TerminateProcess&lt;br /&gt;&lt;br /&gt;'&lt;br /&gt;&lt;br /&gt;' Purpose:  Terminates a process&lt;br /&gt;&lt;br /&gt;'&lt;br /&gt;&lt;br /&gt;' Input:      Name of process&lt;br /&gt;&lt;br /&gt;'&lt;br /&gt;&lt;br /&gt;''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''&lt;br /&gt;&lt;br /&gt;Private Function ProcessTerminate( strProcess )&lt;br /&gt;&lt;br /&gt;    Dim colProcessList, objProcess&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;    Set colProcessList = GetObject("Winmgmts:").ExecQuery _&lt;br /&gt;&lt;br /&gt;        ("Select * from Win32_Process Where Name ='" &amp; strProcess &amp; "'")&lt;br /&gt;&lt;br /&gt;    For Each objProcess in colProcessList&lt;br /&gt;&lt;br /&gt;        objProcess.Terminate( )&lt;br /&gt;&lt;br /&gt;    Next&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;    Set colProcessList = Nothing&lt;br /&gt;&lt;br /&gt;End Function&lt;br /&gt;Wait for process to terminate&lt;br /&gt;Finally, in the WaitForProcess subroutine, the user interface is set up, the script waits while the process is active, and the process termination is initiated. I created three versions of the subroutine in an effort to demonstrate a few methods for displaying status messages. For example, here's how to display these messages using the command console:&lt;br /&gt;&lt;br /&gt;'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''&lt;br /&gt;&lt;br /&gt;'&lt;br /&gt;&lt;br /&gt;' Sub: WaitForProcess&lt;br /&gt;&lt;br /&gt;'&lt;br /&gt;&lt;br /&gt;' Purpose:  Waits for a process&lt;br /&gt;&lt;br /&gt;'&lt;br /&gt;&lt;br /&gt;' Input:    Name of process&lt;br /&gt;&lt;br /&gt;'           Wait time in seconds before termination.&lt;br /&gt;&lt;br /&gt;'             -1 will cause the script to wait indefinitely&lt;br /&gt;&lt;br /&gt;'             0 terminates the process imediately&lt;br /&gt;&lt;br /&gt;'             Any value &gt; 0 will cause the script to wait the specified&lt;br /&gt;&lt;br /&gt;'             amount of time in seconds before terminating the process&lt;br /&gt;&lt;br /&gt;'           Display mode.&lt;br /&gt;&lt;br /&gt;'             Passing S will run the script silent and not show any prompts&lt;br /&gt;&lt;br /&gt;'&lt;br /&gt;&lt;br /&gt;' Output:   On screen status&lt;br /&gt;&lt;br /&gt;'&lt;br /&gt;&lt;br /&gt;' Notes:    The version echos user messages in the command window via StdOut&lt;br /&gt;&lt;br /&gt;'&lt;br /&gt;&lt;br /&gt;''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''&lt;br /&gt;&lt;br /&gt;Private Sub WaitForProcess( strProcess, intWaitTime, strMode )&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;  If ProcessIsRunning(strProcess) Then&lt;br /&gt;&lt;br /&gt;    Dim StdOut&lt;br /&gt;&lt;br /&gt;    Dim w : w = 0&lt;br /&gt;&lt;br /&gt;    Dim strPrompt&lt;br /&gt;&lt;br /&gt;    Dim intPause : intPause = 1&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;    If UCase(strMode) &lt;&gt; "S" Then&lt;br /&gt;&lt;br /&gt;      strPrompt = "Waiting for " &amp; strProcess &amp; " to finish."&lt;br /&gt;&lt;br /&gt;      Set StdOut = WScript.StdOut&lt;br /&gt;&lt;br /&gt;      StdOut.WriteLine ""&lt;br /&gt;&lt;br /&gt;      StdOut.Write strPrompt&lt;br /&gt;&lt;br /&gt;    End If&lt;br /&gt;&lt;br /&gt;    'Loop while the process is running&lt;br /&gt;&lt;br /&gt;    Do While ProcessIsRunning(strProcess)&lt;br /&gt;&lt;br /&gt;      'Check to see if specified # of seconds have passed before terminating&lt;br /&gt;&lt;br /&gt;      'the process. If yes, then terminate the process&lt;br /&gt;&lt;br /&gt;      If w &gt;= intWaitTime AND intWaitTime &gt;= 0 Then&lt;br /&gt;&lt;br /&gt;        Call ProcessTerminate(strProcess)&lt;br /&gt;&lt;br /&gt;        Exit Do&lt;br /&gt;&lt;br /&gt;      End If&lt;br /&gt;&lt;br /&gt;      'If not running silent, post user messages&lt;br /&gt;&lt;br /&gt;      If UCase(strMode) &lt;&gt; "S" Then _&lt;br /&gt;&lt;br /&gt;        StdOut.Write "."&lt;br /&gt;&lt;br /&gt;      'Increment the seconds counter&lt;br /&gt;&lt;br /&gt;      w = w + intPause&lt;br /&gt;&lt;br /&gt;      'Pause&lt;br /&gt;&lt;br /&gt;      Wscript.Sleep(intPause * 1000)&lt;br /&gt;&lt;br /&gt;    Loop&lt;br /&gt;&lt;br /&gt;    If UCase(strMode) &lt;&gt; "S" Then&lt;br /&gt;&lt;br /&gt;      StdOut.WriteLine ""&lt;br /&gt;&lt;br /&gt;      Set StdOut = Nothing&lt;br /&gt;&lt;br /&gt;    End If&lt;br /&gt;&lt;br /&gt;  End If&lt;br /&gt;&lt;br /&gt;End Sub&lt;br /&gt;The result is shown in Figure 1-8.&lt;br /&gt;&lt;br /&gt;Figure 1-8. Status message displayed in command console&lt;br /&gt;&lt;br /&gt;Alternatively, here's some code for displaying status messages in Internet Explorer:&lt;br /&gt;&lt;br /&gt;'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''&lt;br /&gt;&lt;br /&gt;'&lt;br /&gt;&lt;br /&gt;' Sub: WaitForProcess&lt;br /&gt;&lt;br /&gt;'&lt;br /&gt;&lt;br /&gt;' Purpose:  Waits for a process&lt;br /&gt;&lt;br /&gt;'&lt;br /&gt;&lt;br /&gt;' Input:    Name of process&lt;br /&gt;&lt;br /&gt;'           Wait time in seconds before termination.&lt;br /&gt;&lt;br /&gt;'             -1 will cause the script to wait indefinitely&lt;br /&gt;&lt;br /&gt;'             0 terminates the process imediately&lt;br /&gt;&lt;br /&gt;'             Any value &gt; 0 will cause the script to wait the specified&lt;br /&gt;&lt;br /&gt;'             amount of time in seconds before terminating the process&lt;br /&gt;&lt;br /&gt;'             Display mode.&lt;br /&gt;&lt;br /&gt;'             Passing S will run the script silent and not show any prompts&lt;br /&gt;&lt;br /&gt;'&lt;br /&gt;&lt;br /&gt;' Output:   On screen status&lt;br /&gt;&lt;br /&gt;'&lt;br /&gt;&lt;br /&gt;' Notes:    This version uses Internet Explorer for user messages&lt;br /&gt;&lt;br /&gt;'&lt;br /&gt;&lt;br /&gt;''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''&lt;br /&gt;&lt;br /&gt;Private Sub WaitForProcess( strProcess, intWaitTime, strMode )&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;  If ProcessIsRunning(strProcess) Then&lt;br /&gt;&lt;br /&gt;    Dim objIntExplorer&lt;br /&gt;&lt;br /&gt;    Dim c : c = 0&lt;br /&gt;&lt;br /&gt;    Dim w : w = 0&lt;br /&gt;&lt;br /&gt;    Dim strPrompt&lt;br /&gt;&lt;br /&gt;    Dim intPause : intPause = 1&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;    strPrompt = "Waiting for " &amp; strProcess &amp; " to finish."&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;    'If not running silent, create reference to objIntExplorer&lt;br /&gt;&lt;br /&gt;    'This will be used for the user messages. Also set IE display attributes&lt;br /&gt;&lt;br /&gt;    If UCase(strMode) &lt;&gt; "S" Then&lt;br /&gt;&lt;br /&gt;      Set objIntExplorer = Wscript._&lt;br /&gt;&lt;br /&gt;      CreateObject("InternetExplorer.Application")&lt;br /&gt;&lt;br /&gt;      With objIntExplorer&lt;br /&gt;&lt;br /&gt;        .Navigate "about:blank"&lt;br /&gt;&lt;br /&gt;        .ToolBar = 0&lt;br /&gt;&lt;br /&gt;        .Menubar = 0         ' no menu&lt;br /&gt;&lt;br /&gt;        .StatusBar = 0&lt;br /&gt;&lt;br /&gt;        .Width=400&lt;br /&gt;&lt;br /&gt;        .Height = 80&lt;br /&gt;&lt;br /&gt;        .Left = 100&lt;br /&gt;&lt;br /&gt;        .Top = 100&lt;br /&gt;&lt;br /&gt;        .Document.Title = "WaitForProcess"&lt;br /&gt;&lt;br /&gt;      End With&lt;br /&gt;&lt;br /&gt;      'Wait for IE to finish&lt;br /&gt;&lt;br /&gt;      Do While (objIntExplorer.Busy)&lt;br /&gt;&lt;br /&gt;          Wscript.Sleep 200&lt;br /&gt;&lt;br /&gt;      Loop&lt;br /&gt;&lt;br /&gt;      'Show IE&lt;br /&gt;&lt;br /&gt;      objIntExplorer.Visible = 1&lt;br /&gt;&lt;br /&gt;    End If&lt;br /&gt;&lt;br /&gt;    Do While ProcessIsRunning(strProcess)&lt;br /&gt;&lt;br /&gt;      'Check to see if specified # of seconds have passed before terminating&lt;br /&gt;&lt;br /&gt;      'the process. If yes, then terminate the process&lt;br /&gt;&lt;br /&gt;      If w &gt;= intWaitTime AND intWaitTime &gt;= 0 Then&lt;br /&gt;&lt;br /&gt;        Call ProcessTerminate(strProcess)&lt;br /&gt;&lt;br /&gt;        Exit Do&lt;br /&gt;&lt;br /&gt;      End If&lt;br /&gt;&lt;br /&gt;      If UCase(strMode) &lt;&gt; "S" Then&lt;br /&gt;&lt;br /&gt;        objIntExplorer.Document.Body.InnerHTML = strPrompt &amp; String(c, ".")&lt;br /&gt;&lt;br /&gt;        'Increment the counter.&lt;br /&gt;&lt;br /&gt;        'Reset the counter indicator if it's &gt; 25 because&lt;br /&gt;&lt;br /&gt;        'we don't want it taking up a lot of screen space.&lt;br /&gt;&lt;br /&gt;        If c &gt; 25 Then c = 1 Else c = c + 1&lt;br /&gt;&lt;br /&gt;        'Increment the seconds counter&lt;br /&gt;&lt;br /&gt;        w = w + intPause&lt;br /&gt;&lt;br /&gt;      End If&lt;br /&gt;&lt;br /&gt;      'Pause&lt;br /&gt;&lt;br /&gt;      Wscript.Sleep(intPause * 1000)&lt;br /&gt;&lt;br /&gt;    Loop&lt;br /&gt;&lt;br /&gt;    objIntExplorer.Quit( )             ' close Internet Explorer&lt;br /&gt;&lt;br /&gt;    Set objIntExplorer = Nothing      ' release object reference&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;  End If&lt;br /&gt;&lt;br /&gt;End Sub&lt;br /&gt;The resulting status message is shown in Figure 1-9.&lt;br /&gt;&lt;br /&gt;Figure 1-9. Displaying status messages in Internet Explorer&lt;br /&gt;&lt;br /&gt;Finally, here's code that uses the Popup method of Windows Scripting Host for displaying status messages:&lt;br /&gt;&lt;br /&gt;''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''&lt;br /&gt;&lt;br /&gt;'&lt;br /&gt;&lt;br /&gt;' Sub: WaitForProcess&lt;br /&gt;&lt;br /&gt;'&lt;br /&gt;&lt;br /&gt;' Purpose:  Waits for a process&lt;br /&gt;&lt;br /&gt;'&lt;br /&gt;&lt;br /&gt;' Input:    Name of process&lt;br /&gt;&lt;br /&gt;'           Wait time in seconds before termination.&lt;br /&gt;&lt;br /&gt;'             -1 will cause the script to wait indefinitely&lt;br /&gt;&lt;br /&gt;'             0 terminates the process imediately&lt;br /&gt;&lt;br /&gt;'             Any value &gt; 0 will cause the script to wait the specified ' &lt;br /&gt;&lt;br /&gt;'             amount of time in seconds before terminating the process&lt;br /&gt;&lt;br /&gt;'           Display mode.&lt;br /&gt;&lt;br /&gt;'             Passing S will run the script silent and not show any prompts&lt;br /&gt;&lt;br /&gt;'&lt;br /&gt;&lt;br /&gt;' Output:   On screen status&lt;br /&gt;&lt;br /&gt;'&lt;br /&gt;&lt;br /&gt;' Notes:    This version uses WshShell.Popup for user messages&lt;br /&gt;&lt;br /&gt;'&lt;br /&gt;&lt;br /&gt;''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''&lt;br /&gt;&lt;br /&gt;Private Sub WaitForProcess( strProcess, intWaitTime, strMode )&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;  If ProcessIsRunning(strProcess) Then&lt;br /&gt;&lt;br /&gt;    Dim objWshShell&lt;br /&gt;&lt;br /&gt;    Dim c : c = 0&lt;br /&gt;&lt;br /&gt;    Dim w : w = 0&lt;br /&gt;&lt;br /&gt;    Dim strPrompt&lt;br /&gt;&lt;br /&gt;    Dim intPopupTimer : intPopupTimer = 2&lt;br /&gt;&lt;br /&gt;    Dim intPause : intPause = 1&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;    strPrompt = "Waiting for " &amp; strProcess &amp; " to finish."&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;    'If not running silent, create reference to objWshShell&lt;br /&gt;&lt;br /&gt;    'This will be used for the user messages&lt;br /&gt;&lt;br /&gt;    If UCase(strMode) &lt;&gt; "S" Then _&lt;br /&gt;&lt;br /&gt;      Set objWshShell = CreateObject("WScript.Shell")&lt;br /&gt;&lt;br /&gt;    'Loop while the process is running&lt;br /&gt;&lt;br /&gt;    Do While ProcessIsRunning(strProcess)&lt;br /&gt;&lt;br /&gt;      'Check to see if specified # of seconds have passed before terminating&lt;br /&gt;&lt;br /&gt;      'the process. If yes, then terminate the process&lt;br /&gt;&lt;br /&gt;      If w &gt;= intWaitTime AND intWaitTime &gt;= 0 Then&lt;br /&gt;&lt;br /&gt;        Call ProcessTerminate(strProcess)&lt;br /&gt;&lt;br /&gt;        Exit Do&lt;br /&gt;&lt;br /&gt;      End If&lt;br /&gt;&lt;br /&gt;      'If not running silent, post user prompt&lt;br /&gt;&lt;br /&gt;      If UCase(strMode) &lt;&gt; "S" Then&lt;br /&gt;&lt;br /&gt;        objWshShell.Popup strPrompt &amp; String(c, "."), intPopupTimer, _&lt;br /&gt;&lt;br /&gt;        "WaitForProcess", 64&lt;br /&gt;&lt;br /&gt;        'Increment the counter.&lt;br /&gt;&lt;br /&gt;        'Reset the counter indicator if it's &gt; 25 because&lt;br /&gt;&lt;br /&gt;        'we don't want it taking up a lot of screen space.&lt;br /&gt;&lt;br /&gt;        If c &gt; 25 Then c = 1 Else c = c + 1&lt;br /&gt;&lt;br /&gt;      End If&lt;br /&gt;&lt;br /&gt;      'Increment the seconds counter&lt;br /&gt;&lt;br /&gt;      w = w + intPause + intPopupTimer&lt;br /&gt;&lt;br /&gt;      'Pause&lt;br /&gt;&lt;br /&gt;      Wscript.Sleep(intPause * 1000)&lt;br /&gt;&lt;br /&gt;    Loop&lt;br /&gt;&lt;br /&gt;    Set objWshShell = Nothing&lt;br /&gt;&lt;br /&gt;  End If&lt;br /&gt;&lt;br /&gt;End Sub&lt;br /&gt;The resulting dialog box is shown in Figure 1-10.&lt;br /&gt;&lt;br /&gt;Figure 1-10. Displaying status messages in a dialog box&lt;br /&gt;&lt;br /&gt;Note that if you are assembling a standalone script, it should contain sections 1, 2, 3, and one option from section 4. If you would rather incorporate this code into your existing script, you need only sections 2, 3, and one option from section 4. You'll also need to add the call statement that is at the end of the main routine section. All the code sections are self-contained, which makes them easy to import into existing scripts.&lt;br /&gt;&lt;br /&gt;Running the Hack&lt;br /&gt;To use this hack, type the code into Notepad (with Word Wrap disabled) and save it with a .vbs extension as WaitForProcess.vbs. Or, if you don't want to tire your fingers out, download it from the O'Reilly web site instead.&lt;br /&gt;&lt;br /&gt;Here are a few sample command-line examples. This will wait indefinitely until Notepad is closed:&lt;br /&gt;&lt;br /&gt;cscript WaitForProcess.vbs notepad.exe -1&lt;br /&gt;This will wait silently and indefinitely until Notepad is closed:&lt;br /&gt;&lt;br /&gt;cscript WaitForProcess.vbs notepad.exe -1 S&lt;br /&gt;And this will wait 10 seconds before Notepad is forcefully closed:&lt;br /&gt;&lt;br /&gt;cscript WaitForProcess.vbs notepad.exe 10&lt;br /&gt;—Dan Thomson&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3376060701639754039-5983495484088163973?l=booksrthere.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://booksrthere.blogspot.com/feeds/5983495484088163973/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3376060701639754039&amp;postID=5983495484088163973' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3376060701639754039/posts/default/5983495484088163973'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3376060701639754039/posts/default/5983495484088163973'/><link rel='alternate' type='text/html' href='http://booksrthere.blogspot.com/2008/03/hack-5-wait-for-and-optionally.html' title='Hack 5 Wait for and Optionally Terminate a Process'/><author><name>jerin</name><uri>http://www.blogger.com/profile/10103476469626499932</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3376060701639754039.post-4297371088973483810</id><published>2008-03-26T09:23:00.000-07:00</published><updated>2008-03-26T09:25:30.878-07:00</updated><title type='text'>Hack 4 Automatically Log On After Booting</title><content type='html'>It's sometimes convenient to configure machines to log on automatically when booted. Here are three ways to do this.&lt;br /&gt;&lt;br /&gt;In all versions of Windows that are based on Windows NT (including Windows 2000, Windows XP, and Windows Server 2003), a user is required to log on before he can use the system interactively. This is usually done by pressing Ctrl-Alt-Del and typing the user's credentials. Automatic logon is an option you can set to enable Windows to log on automatically using credentials that are stored in the Registry. To invoke automatic logon, you set Registry entries that define the user ID, the password, and the domain to be used to log on. Why use this feature? There are a number of reasons. As an IT professional, I have several of my home systems set up to do this, and it makes life simpler. Test systems in a lab might be another place to use this feature. I also use it all the time on virtual machine images I have running on my laptop.&lt;br /&gt;&lt;br /&gt;Automatic login makes things simpler, but it creates a security hole. First, the credentials are stored in clear text in the Registry. Thus, anyone with remote Registry privileges can see the clear text user ID and password. Also, if you have automatic logon set on a laptop, anyone who turns on the laptop is automatically logged in as you. So use this feature carefully!&lt;br /&gt;&lt;br /&gt;Manual Configuration&lt;br /&gt;You can configure automatic logon manually by adding the following four key Registry entries: AutoAdminLogon, DefaultDomainName, DefaultUserName, and DefaultPassword. These entries inform Windows whether to attempt automatic logon and provide the credentials (username, password, and domain).&lt;br /&gt;&lt;br /&gt;Start Registry Editor (StartRunregedit) and find the Registry key HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon, which is where the Registry values you set to control automatic logon are located. Two of these values, DefaultDomainName and DefaultUserName, already exist. DefaultDomainName is a string that holds the domain (or workstation) name where the user ID exists, and DefaultUserName is the user ID that Winlogon will attempt to use to log on. This username is authenticated against the domain (or workstation) name set in the DefaultDomainName setting.&lt;br /&gt;&lt;br /&gt;Now, create two new values by right-clicking on Winlogon and selecting NewString Value, which will create new values of type REG_SZ. Name the first value AutoAdminLogon, and specify a value data of 1 to instruct Winlogon to attempt to use automatic logon. Name the second value DefaultPassword; this value specifies the password for the user set in the DefaultUserName setting.&lt;br /&gt;&lt;br /&gt;The result will looking like Figure 1-7.&lt;br /&gt;&lt;br /&gt;Figure 1-7. Enabling automatic logon by editing the Registry&lt;br /&gt;&lt;br /&gt;Script Method&lt;br /&gt;An easier way to configure automatic logon on your machines is to use two VBScript scripts, one to enable automatic logon and the other to disable it. Here's the script for enabling it:&lt;br /&gt;&lt;br /&gt;' Script to turn on automatic logon&lt;br /&gt;&lt;br /&gt;' (c) Thomas Lee 2002&lt;br /&gt;&lt;br /&gt;' Freely distributed!&lt;br /&gt;&lt;br /&gt;Dim Prompt, oWSH,UserName, UserPass, UserDomain&lt;br /&gt;&lt;br /&gt;set oWSH = WScript.CreateObject("WScript.Shell")&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;' get user name&lt;br /&gt;&lt;br /&gt;Prompt = "Enter the autologon user name"&lt;br /&gt;&lt;br /&gt;UserName = InputBox(Prompt, Title, "")&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;' get password&lt;br /&gt;&lt;br /&gt;Prompt = "Enter the autologon user password for " &amp; UserName&lt;br /&gt;&lt;br /&gt;UserPass = InputBox(Prompt, Title, "")&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;' get domain&lt;br /&gt;&lt;br /&gt;Prompt = "Enter the autologon user domain for " &amp; UserName&lt;br /&gt;&lt;br /&gt;Userdomain = InputBox(Prompt, Title, "")&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;' now set these in the Registry&lt;br /&gt;&lt;br /&gt;oWSH.RegWrite "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\AutoAdminLogon",&lt;br /&gt;&lt;br /&gt;"1","REG_SZ"&lt;br /&gt;&lt;br /&gt;oWSH.RegWrite "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\&lt;br /&gt;&lt;br /&gt;DefaultDomainName", UserDomain, "REG_SZ" &lt;br /&gt;&lt;br /&gt;oWSH.RegWrite "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\&lt;br /&gt;&lt;br /&gt;DefaultUserName", UserName, "REG_SZ" &lt;br /&gt;&lt;br /&gt;oWSH.RegWrite "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\&lt;br /&gt;&lt;br /&gt;DefaultPassword", UserPass, "REG_SZ"&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;' ensure the change is persistent!&lt;br /&gt;&lt;br /&gt;oWSH.RegWrite "HKLM\SOFTWARE\Microsoft\WindowsNT\CurrentVersion\Winlogon\ForceAutoLogon", &lt;br /&gt;&lt;br /&gt;"1", "REG_SZ" &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;' All done&lt;br /&gt;And here's the script for disabling automatic logon:&lt;br /&gt;&lt;br /&gt;' Script to remove autoadmin logon&lt;br /&gt;&lt;br /&gt;' (c) Thomas Lee 2002&lt;br /&gt;&lt;br /&gt;' Freely distributed!&lt;br /&gt;&lt;br /&gt;Option Explicit&lt;br /&gt;&lt;br /&gt;On Error Resume Next&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;'Declare variables&lt;br /&gt;&lt;br /&gt;Dim Prompt, oWSH&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;'Set the Windows Script Host Shell &lt;br /&gt;&lt;br /&gt;set oWSH = WScript.CreateObject("WScript.Shell")&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;' delete the relevant keys&lt;br /&gt;&lt;br /&gt;oWSH.RegDelete "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\&lt;br /&gt;&lt;br /&gt;AutoAdminLogon"&lt;br /&gt;&lt;br /&gt;oWSH.RegDelete "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\&lt;br /&gt;&lt;br /&gt;DefaultDomainName"&lt;br /&gt;&lt;br /&gt;oWSH.RegDelete "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\&lt;br /&gt;&lt;br /&gt;DefaultUserName"&lt;br /&gt;&lt;br /&gt;oWSH.RegDelete "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\&lt;br /&gt;&lt;br /&gt;DefaultPassword"&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;' All done - say goodbye!&lt;br /&gt;&lt;br /&gt;Legend = "Autoadmin removed - have a nice day!"&lt;br /&gt;&lt;br /&gt;MyBox = MsgBox (legend, 4096, "We're Done")&lt;br /&gt;You can use Notepad to type these scripts and save them with a .vbs file extension, or download autoadminlogon.vbs and noautoadminlogon.vbs from http://www.oreilly.com/catalog/winsvrhks/.&lt;br /&gt;&lt;br /&gt;Sysinternals Tool&lt;br /&gt;Finally, here's one more way to configure automatic logon on machines. Mark Russinovich, of Sysinternals fame, also wrote a simple program to do this. You can download the program and the source from http://www.sysinternals.com/ntw2k/source/misc.shtml#AutoLogon, where you can find lots of other great tools.&lt;br /&gt;&lt;br /&gt;—Thomas Lee&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3376060701639754039-4297371088973483810?l=booksrthere.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://booksrthere.blogspot.com/feeds/4297371088973483810/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3376060701639754039&amp;postID=4297371088973483810' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3376060701639754039/posts/default/4297371088973483810'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3376060701639754039/posts/default/4297371088973483810'/><link rel='alternate' type='text/html' href='http://booksrthere.blogspot.com/2008/03/hack-4-automatically-log-on-after.html' title='Hack 4 Automatically Log On After Booting'/><author><name>jerin</name><uri>http://www.blogger.com/profile/10103476469626499932</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3376060701639754039.post-9004208708169557787</id><published>2008-03-26T09:22:00.001-07:00</published><updated>2008-03-26T09:22:56.551-07:00</updated><title type='text'>Hack 3 Find and Replace Registry Keys from a Command Line</title><content type='html'>Using the Regfind utility, you can easily search the Registry for a value, regardless of the key, and replace it.&lt;br /&gt;&lt;br /&gt;Regfind (from the Windows 2000 Server Resource Kit) can be an invaluable tool when you need change a Registry key that you know the value for but when do not necessarily know the full path. Recently tasked with changing the hardcoded DNS server IP on all the servers in our organization, I was pleasantly surprised when I located this gem. The problem with trying to change the DNS server entry in the Registry is that all the IP parameters are broken up by a hashed ID. The ID references several things, but most of them have to do with the network card. Regfind allows you to search a set of subkeys in the Registry for a specific value and, when found, replace it. Another real beauty of this program is that it will work remotely; all you need to do is supply it with a list of machines and let it go. Using a list of computer names (generated from SMS, Server Manager, or AD Users and Computers), combined with two batch files, you can make sweeping changes in a dynamic environment.&lt;br /&gt;&lt;br /&gt;The Code&lt;br /&gt;Here's an example of how to change the DNS server entry on all servers in your organization. First, create a batch file called Regchange2.bat with the following syntax:&lt;br /&gt;&lt;br /&gt;regfind -m \\%1 -p HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\parameters "OLDIP" -r "NEWIP"&lt;br /&gt;You will obviously want to replace OLDIP with the old DNS server IP and replace NEWIP with the new DNS server IP.&lt;br /&gt;&lt;br /&gt;Now, create a second batch file called regchange1.bat with the following syntax:&lt;br /&gt;&lt;br /&gt;for /F %%A in (servers.txt) do (call regchange2.bat %%A)&lt;br /&gt;This searches the servers.txt file for computer names and passes them to the regchange2.bat file as a command-line argument.&lt;br /&gt;&lt;br /&gt;Now you need to create a list file for your batch files to use. Create a listing of servers that need to have their DNS IP's changed and save that list as servers.txt. An SMS report or a copy/paste from the server manager will suffice, or you can create the file manually if you like.&lt;br /&gt;&lt;br /&gt;Running the Hack&lt;br /&gt;Now, simply run the regchange1.bat batch file by calling it from a logon script and watch all your servers have their IP settings changed!&lt;br /&gt;&lt;br /&gt;This is just one simple example of how to use Regfind. There are many command-line arguments, so please examine those to meet your needs.&lt;br /&gt;&lt;br /&gt;—Donnie Taylor&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3376060701639754039-9004208708169557787?l=booksrthere.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://booksrthere.blogspot.com/feeds/9004208708169557787/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3376060701639754039&amp;postID=9004208708169557787' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3376060701639754039/posts/default/9004208708169557787'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3376060701639754039/posts/default/9004208708169557787'/><link rel='alternate' type='text/html' href='http://booksrthere.blogspot.com/2008/03/hack-3-find-and-replace-registry-keys.html' title='Hack 3 Find and Replace Registry Keys from a Command Line'/><author><name>jerin</name><uri>http://www.blogger.com/profile/10103476469626499932</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3376060701639754039.post-3677862718379107154</id><published>2008-03-26T09:21:00.001-07:00</published><updated>2008-03-26T09:21:34.726-07:00</updated><title type='text'>Hack 2 Drag and Drop to the Run Menu</title><content type='html'>Hack 2 Drag and Drop to the Run Menu&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;If you're tired of having to drop out to a command prompt and navigate through folders to run an executable that requires switches, try this.&lt;br /&gt;&lt;br /&gt;The following easy-to-use steps can be used to run the program of your choice from the Run menu with any command-line switches you need to include. This is much handier than opening a command prompt and changing to the directory where the executable is located, especially if long filenames are involved, which requires you to enclose your path in double quotes.&lt;br /&gt;&lt;br /&gt;First, navigate in Windows Explorer to the executable you want to launch (Figure 1-2).&lt;br /&gt;&lt;br /&gt;Figure 1-2. Selecting the executable to run in Explorer&lt;br /&gt;&lt;br /&gt;Next, use StartRun to invoke the Run menu (Figure 1-3).&lt;br /&gt;&lt;br /&gt;Figure 1-3. Opening the Run menu&lt;br /&gt;&lt;br /&gt;Then, drag and drop your executable to the Run menu (Figure 1-4). Make sure the Open box is empty before you perform this step, or unexpected results might occur.&lt;br /&gt;&lt;br /&gt;Figure 1-4. Dragging and dropping the executable into the Run menu&lt;br /&gt;&lt;br /&gt;Now, simply add your switches and click OK to launch your application (Figure 1-5).&lt;br /&gt;&lt;br /&gt;Figure 1-5. Adding switches as needed&lt;br /&gt;&lt;br /&gt;You'll want to keep in mind that any filenames or paths that don't follow the old 8.3 naming convention should be within quotation marks to run properly (Figure 1-6).&lt;br /&gt;&lt;br /&gt;Figure 1-6. Using quotation marks for long filenames/paths&lt;br /&gt;&lt;br /&gt;Note that your switches and arguments can reside outside of the quotation marks.&lt;br /&gt;&lt;br /&gt;—Sean Ademy&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3376060701639754039-3677862718379107154?l=booksrthere.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://booksrthere.blogspot.com/feeds/3677862718379107154/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3376060701639754039&amp;postID=3677862718379107154' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3376060701639754039/posts/default/3677862718379107154'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3376060701639754039/posts/default/3677862718379107154'/><link rel='alternate' type='text/html' href='http://booksrthere.blogspot.com/2008/03/hack-2-drag-and-drop-to-run-menu.html' title='Hack 2 Drag and Drop to the Run Menu'/><author><name>jerin</name><uri>http://www.blogger.com/profile/10103476469626499932</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3376060701639754039.post-4332244648436559029</id><published>2008-03-26T09:17:00.000-07:00</published><updated>2008-03-26T09:20:03.271-07:00</updated><title type='text'>Hack 1 Use Run As to Perform Administrative Tasks</title><content type='html'>&lt;em&gt;Use Run As to protect your administrator workstation from Trojans and other nasties.&lt;br /&gt;&lt;br /&gt;If you're lazy, like I am, you probably use the default administrator account on your desktop workstation for browsing the Web, checking your email, and managing the servers on your company's network.&lt;br /&gt;&lt;br /&gt;Not a good idea.&lt;br /&gt;&lt;br /&gt;What if you unknowingly visited a web page that executed a script that downloaded a Trojan to your machine? Your administrator account would be compromised, and the attacker would have total access to your workstation and possibly to your whole network! To avoid such dangers, administrators should always have two user accounts: a regular (user-level) account for ordinary activities, such as web browsing and messaging, and an administrator-level account, used only for performing administrative tasks. This way, when you are reading your email and suddenly remember you have to reschedule a backup, you can simply log off, log back on using your administrator account, perform the task, log off again, and log on again as a regular user.&lt;br /&gt;&lt;br /&gt;Who am I kidding? That's too much to expect of a lazy system administrator.&lt;br /&gt;&lt;br /&gt;How Run As Works&lt;br /&gt;The Run As service (called Secondary Logon service in Windows Server 2003 and Windows XP) is a hack designed to enable you to run programs by using alternate credentials while you're logged on using another account. For example, if you are an administrator and are logged on to your desktop using your regular user account, you won't be able to run administrative tools such as Computer Management, because they require administrator credentials to run properly. (Actually, you can open Computer Management as an ordinary user; you just can't do much with it.) Using Run As, however, you can run Computer Management as an administrator while remaining logged on as an ordinary user.&lt;br /&gt;&lt;br /&gt;There are two ways to use Run As: using the GUI or from the command line. To use the GUI method, first find the program you want to run in Windows Explorer or My Computer. Then, for executables (*.exe files), hold down the Shift key, right-click the program's icon, and select Run to open the Run As Other User dialog box shown in Figure 1-1. For MMC consoles (*.msc files) and Control Panel utilities (*.cpl files), you do the same thing but don't need to hold down the Shift key.&lt;br /&gt;&lt;br /&gt;Figure 1-1. Using Run As to run a program using administrator credentials&lt;br /&gt;&lt;br /&gt;Once you specify the appropriate alternate credentials and click OK, the program you selected runs in the security context of those alternate credentials until you close or terminate the program. If you prefer, the alternative credentials can also be entered as domain\user or user@domain, which in Figure 1-1 would be MTIT\Administrator or Administrator@mtit.com for an example domain named mtit.com (replace these credentials with the name of your own domain). The advantage of doing it the way shown in Figure 1-1 is that, if your computer is a member server, you can specify a local user account by entering the name of the computer in the Domain field.&lt;br /&gt;&lt;br /&gt;Using Run As from the command line is just as easy, but you need to know the path to the program (unless the program file is located within the system path). For example, the Computer Management console file compmgmt.msc is located in the \system32 directory. To run it as Administrator in the MTIT domain, simply type the following at a command prompt:&lt;br /&gt;&lt;br /&gt;runas /user:MTIT\Administrator "mmc %windir\system32\compmgnt.msc"&lt;br /&gt;You'll be prompted for a password for the account, after which Computer Management will open. Note that you can also type this command directly into the Run box (accessed by StartRun).&lt;br /&gt;&lt;br /&gt;Limitations of Run As&lt;br /&gt;While Run As is useful, it has some limitations. First, the alternate credentials you specify must have the Log On Locally user right on the computer. Since Run As is usually used with administrator credentials (which have that right by default), this is usually an issue only in certain circumstances. For example, say you grant a few knowledgeable users a second user account that belongs to the Power Users group, to allow them to update device drivers and perform other minor maintenance on their desktop computers. If you try to reduce the attack surface of your network by removing the right to Log On Locally from the Power Users group using Group Policy, then these users won't be able to perform such tasks.&lt;br /&gt;&lt;br /&gt;Also, there are certain tasks you can't perform directly using Run As, such as opening the Printers folder to administer a printer that is connected to your machine. The reason for this is that the special folders such as Printers and Network and Dial-up Connections are opened indirectly by the operating system, not by a command. You also can't use Run As to open Windows Explorer and access the filesystem on your computer as administrator, because the Windows shell explorer.exe is already running as your current desktop environment and Windows allows only one GUI shell to run at a time.&lt;br /&gt;&lt;br /&gt;Finally, Run As also might not work if the program you are trying to run is located on a network share, because the credentials used to access the share might be different than the credentials used to run the program.&lt;br /&gt;&lt;br /&gt;Most limitations have workarounds of some sort, if you try hard enough to find them. So, let's see if we can figure out ways to get around these limitations (except for the Log On Locally limitation, which is absolute).&lt;br /&gt;&lt;br /&gt;Running programs without an executable&lt;br /&gt;Say you want to change some settings for the Local Area Connection in the Network and Dial-up Connections folder. If you try doing this as an ordinary user, you'll get a message saying "The controls on this properties sheet are disabled because you do not have sufficient privileges to access them." Here's how to access these settings as an administrator without logging out of your regular account. Right-click on the task bar and open Task Manager. Then, switch to the Processes tab, select explorer.exe, and click End Process to kill the desktop but leave Task Manager running. Now, switch to the Applications tab, click New Task, type runas /user:MTIT\Administrator explorer.exe to run the Windows Explorer shell in an administrator context, and click OK. Finally, move Task Manager out of the way and type your password into the command-prompt window.&lt;br /&gt;&lt;br /&gt;A new desktop will now appear, running in the security context of your administrator account. You can now change the settings of your Local Area Connection, modify the properties of a printer in the Printers folder, browse the filesystem, or do anything you want to do as administrator. But be sure to leave Task Manager running, because it is your only connection to your original desktop! You can minimize it so it won't be in the way.&lt;br /&gt;&lt;br /&gt;Once you're finished performing your administrative tasks, you can return to your original desktop (the one running under the security context of your regular account) as follows. Maximize Task Manager so that you'll have access to it when your desktop disappears again. Then, to log off of your administrator session, click StartShut Down and select Log Off.&lt;br /&gt;&lt;br /&gt; Do not try to log off by pressing Ctrl-Alt-Del and clicking Log Off, because this will log off the session for your regular user account.&lt;br /&gt; &lt;br /&gt; &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Your administrator desktop has now disappeared, but Task Manager is still running (in the security context of your regular account), so switch to the Applications tab, click New Task, type runas /user:MTIT\Administrator explorer.exe, and click OK. Your desktop has returned.&lt;br /&gt;&lt;br /&gt;At this point, you might ask, "Why should I go to all that trouble? It would be faster just to log off as a regular user and log on as an administrator." True, but any applications you have running as a regular user would then have to be terminated. Doing it the way shown here, however, leaves all your desktop applications running in the background.&lt;br /&gt;&lt;br /&gt;Running programs from network shares&lt;br /&gt;Here's how to get around the limitation of running programs from network shares with appropriate credentials. To run a program named test.exe found in the TOOLS share on server SRV230, use StartRun to open a command-prompt window as administrator, type runas /user:MTIT\Administrator cmd to open a command shell in administrator context, and then map a drive to the shared folder by typing net use Z:\\SRV230\TOOLS. Now, switch to the Z: drive and run the program as desired. This lets you connect to the shared folder using domain administrator credentials and run the program under the same credentials. This approach is also useful for installing applications from a network distribution point.&lt;br /&gt;&lt;br /&gt;Run As Shortcuts&lt;br /&gt;To make your life easier, instead of having to type stuff at the command line, you can use Run As to create a shortcut that will run a program under alternate credentials. For example, to run the Computer Management console from a Run As shortcut, right-click on your desktop, select NewShortcut, and type %windir%\system32\compmgmt.msc as the command string. Name your shortcut Computer Management and click OK. Then, right-click on the shortcut, select Properties to open its properties sheet, and on the Shortcut tab select the checkbox labeled "Run program as other user" (on Windows Server 2003, click the Advanced button on the Shortcut tab to configure this). Now, whenever you double-click on the shortcut to run Computer Management, the Run As Other User dialog box (see Figure 1-1) will appear. Just type in your administrator password to run Computer Management in administrator context.&lt;br /&gt;&lt;br /&gt;There's another way to create Run As shortcuts that you might find even easier to use. Just right-click on your desktop, select NewShortcut, and type the following command string:&lt;br /&gt;&lt;br /&gt;%windir%\system32\runas.exe /user:MTIT\Administrator "mmc %windir%\system32\compmgmt.msc"&lt;br /&gt;Save the shortcut with the name Computer Management. Now, when you double-click the shortcut, a command-prompt window opens, prompting you for the password for the MTIT\Administrator account. Type the password, press Enter, and Computer Management starts in administrator context.&lt;br /&gt;&lt;br /&gt;What if you get tired of typing your administrator password each time you want to run a Run As shortcut? On Windows Server 2003, there's a way to get around that. Just create a new shortcut with this command string:&lt;br /&gt;&lt;br /&gt;%windir%\system32\runas.exe /user:MTIT\Administrator /savecred "mmc %windir%\&lt;br /&gt;&lt;br /&gt;system32\compmgmt.msc"&lt;br /&gt;Notice the /savecred switch in this string. This option first appeared in Windows XP. The first time you double-click on the shortcut, a command-prompt window opens to prompt you for the password for the alternate credentials, just like before. The next time you double-click on the shortcut, however, you are not prompted for the password; it was stored on your machine the first time you ran the shortcut. Now you no longer have to type a password each time you use your Run As shortcut. Time-saver, right? Yes, but it's also a possible security hole: once the credentials for your administrator account are stored locally on the machine, they can be used to run any command-line program using administrator credentials.&lt;br /&gt;&lt;br /&gt;Here's a scenario to illustrate what I mean. Let's say you need to run an administrative tool on a user's desktop machine without logging the user off the machine. You ask the user to take a coffee break. Then, you open a command-prompt window and use runas with /savecred to start the tool (you use /savecred because you might have to run several administrative tools and you don't want to have to type your complex 24-character password repeatedly). When you're finished, you close all the tools you started and walk away. When the user returns to her desktop, she opens a command prompt and types runas /user:MTIT\Administrator /savecred cmd. A command-prompt window opens, displaying administrator credentials in the title bar. The user now knows that she can use this approach to run any program on her machine using administrator credentials.&lt;br /&gt;&lt;br /&gt;What did you do wrong as administrator in this scenario? Two things: you used /savecred on a user's desktop machine, which saved your administrator password locally on the machine, and you haven't renamed the default administrator account. If you had changed the name of this account to something complex and unknown to ordinary users, the runas /user:MTIT\Administrator /savecred cmd command the user typed wouldn't work.&lt;br /&gt;&lt;br /&gt;What do you do if you have used /savecred on an unsecured machine without thinking about the consequences? Just delete your stored credentials on the machine by opening Stored User Names and Passwords in the Control Panel.&lt;br /&gt; &lt;/em&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3376060701639754039-4332244648436559029?l=booksrthere.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://booksrthere.blogspot.com/feeds/4332244648436559029/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3376060701639754039&amp;postID=4332244648436559029' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3376060701639754039/posts/default/4332244648436559029'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3376060701639754039/posts/default/4332244648436559029'/><link rel='alternate' type='text/html' href='http://booksrthere.blogspot.com/2008/03/hack-1-use-run-as-to-perform.html' title='Hack 1 Use Run As to Perform Administrative Tasks'/><author><name>jerin</name><uri>http://www.blogger.com/profile/10103476469626499932</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3376060701639754039.post-495423148281507239</id><published>2008-03-24T11:31:00.000-07:00</published><updated>2008-03-24T11:32:50.012-07:00</updated><title type='text'>About the Author</title><content type='html'>&lt;em&gt;Mitch Tulloch is the author of over a dozen computer books, including three Nutshells for O'Reilly &amp; Associates, Inc. (Microsoft Exchange Server in a Nutshell, Windows 2000 Administration in a Nutshell, and Windows Server 2003 in a Nutshell), two encyclopedias for Microsoft Press (the Microsoft Encyclopedia of Networking, currently in its second edition, and the Microsoft Encyclopedia of Security), and a string of titles for system administrators from Osborne/McGraw-Hill. Mitch has also written feature articles for industry magazines such as NetworkWorld and Microsoft Certified Professional Magazine, has developed university-level courses in Windows system administration, and provides training and consulting in Microsoft platforms and products. Mitch is based in Winnipeg, Canada, and you can contact him through his web site (http://www.mtit.com).&lt;/em&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/3376060701639754039-495423148281507239?l=booksrthere.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://booksrthere.blogspot.com/feeds/495423148281507239/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=3376060701639754039&amp;postID=495423148281507239' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3376060701639754039/posts/default/495423148281507239'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3376060701639754039/posts/default/495423148281507239'/><link rel='alternate' type='text/html' href='http://booksrthere.blogspot.com/2008/03/about-author.html' title='About the Author'/><author><name>jerin</name><uri>http://www.blogger.com/profile/10103476469626499932</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>
