Immediate Solutions


Creating Logon Scripts with Shell Scripting

Shell scripting is the original source of logon scripting for Windows . Although it may lack some of the more complex features of other scripting languages, its main advantage is compatibility. Unlike KiXtart or Windows Script Host, shell scripting does not require any installed client files to run (other than the operating system). Shell scripting provides a simple, logon script solution for quick and easy deployment.

Setting the Window Title

Windows 2000/XP/2003 supports the title command to change the title of a shell prompt window. The basic syntax of the title command is as follows :

 Title  name  

Here, name is the name to give the current command-prompt window. Here is an example to change the shell prompt title to "Logon Script":

 If "%  OS  %"=="Windows_NT" Title Logon Script 

Here, % OS % is an environment variable that indicates the operating system type.

Changing the Background and Foreground Colors

Windows 2000/XP/2003 supports the color command to change the background and foreground in a shell prompt. The basic syntax of the color command is as follows:

 COLOR  BF  

Here, B is the background color value and F is the foreground color value. The color command supports the following color values:

  • ”Black

  • 1 ”Blue

  • 2 ”Green

  • 3 ”Aqua

  • 4 ”Red

  • 5 ”Purple

  • 6 ”Yellow

  • 7 ”White

  • 8 ”Gray

  • 9 ”Light Blue

  • A ”Light Green

  • B ”Light Aqua

  • C ”Light Red

  • D ”Light Purple

  • E ”Light Yellow

  • F ”Bright White

Here is an example to change the shell prompt colors to bright white text on a blue background:

 IF "%OS%"= ="Windows_NT" COLOR 1F 

Here, % OS % is an environment variable that indicates the operating system type.

Synchronizing the Local System Time

Synchronizing the local system to a central time source allows you to perform enterprise-wide tasks simultaneously . The basic syntax to synchronize the local clock with a specified time source is as follows:

 Net Time \  server  /  commands  

Here, \\ server is the name of the time source server to sync with. This parameter is only necessary when syncing with a specific server. If this parameter is omitted ( Net Time ), the system will search the local domain for a time source server. / commands are any of the following parameters:

  • /SET ”Sets the local time to the time source server

  • /Y ”Forces to sync the time with the server specified, regardless of whether the server is a time source server or not

  • / DOMAIN: domainname ”Searches the specified domain for a time source server

The following script attempts to sync the local system time with the server named servername . If this fails, the domain will be searched for a time source to sync with. To execute this script, proceed as follows:

  1. Create a new directory to store all files included in this example.

  2. Select StartRun and enter " scriptfile .bat."

Here, scriptfile is the full path and file name of a script file that contains the following:

 @Echo Off CLS ; Clears the screen Set  TServer  =  ServerName  Echo Syncing the time with %  TServer  %... Net Time \%  TServer  % /set /yes If %  errorlevel  % NEQ 0 CLS && Goto Domain CLS && Echo Sync Successful Goto End :Domain Echo Searching the local domain for a time-server... Net Time /set /yes If %  errorlevel  % EQU 0 CLS && Echo Sync Successful && Goto End CLS && Echo Time Sync Error :End 

Here, tserver is a variable containing the name of the time source server; NEQ is the "not equal to" operator; and && allows you to run a second command after the first has completed.

Mapping Universal Drives

Mapping common drives for all users allows you to present a central resource location for applications or data. In Chapter 8, you learned how to map network drives from within Windows and the command prompt. To map a network drive and display the status from the command prompt, proceed as follows:

  1. Create a new directory to store all files included in this example.

  2. Select StartRun and enter " scriptfile .bat."

Here, scriptfile is the full path and file name of a script file that contains the following:

 @Echo Off CLS ; Clears the screen Set  Drive  =  DriveLetter  Set  Share  =\  server  \  sharename  Echo Mapping drive %  Drive  % to %  Share  % Net Use %  Drive  %: /Delete && CLS Net Use %  Drive  %: %  Share  % If %  errorlevel  % EQU 0 CLS && Echo Map Successful && Goto End CLS && Echo Error mapping drive %  Drive  % to %  Share  % :End 

Here, driveletter is the drive letter to map a share to, and server contains the sharename you want to map to.

Mapping Drives by Group

Mapping drives by group membership allows you to control which drives and resources will be available to which users. The resource kit utility IfMember allows you to determine a user 's group membership from the command line. The basic syntax of the IfMember utility is as follows:

 IfMember /  Commands Groups  

Here, Groups are any group, separated by spaces, whose membership you want to check. An errorlevel of 1 indicates the user is a member of the specified group. The available commands are as follows:

  • /List ”Lists all groups the user belongs to

  • /Verbose ”Displays all group matches

To map a network drive according to group membership and display the status from the command prompt, proceed as follows:

  1. Create a new directory to store all files included in this example.

  2. Select StartRun and enter " scriptfile .bat."

Here, scriptfile is the full path and file name of a script file that contains the following:

 @Echo Off CLS ; Clears the screen  Fullpath  \IfMember GroupName > Nul If Not %  errorlevel  % EQU 1 Goto End Set  Drive  =  DriveLetter  Set  Share  =\  server  \  sharename  Echo Mapping drive %  Drive  % to %  Share  % Net Use %  Drive  %: /Delete && CLS Net Use %  Drive  %: %  Share  % If %  errorlevel  % EQU 0 CLS && Echo Map Successful && Goto End CLS && Echo Error mapping drive %  Drive  % to %  Share  % :End 

Here, fullpath is the full path where the IfMember utility is located; GroupName is the name of the group to check membership; driveletter is the drive letter to map a share to; NEQ is the "not equal to" operator; EQU is the "equal to" operator; server contains the sharename you want to map to; and && allows you to run a second command after the first has completed.

Mapping Printers Using Con2PRT

Mapping printers through a logon script provides an easy method to remotely update printer connections. Con2PRT (Connect To Port) is a Windows 2000 Resource Kit utility used to control printer connections from the command line. The basic syntax of the con2PRT utility is as follows:

 Con2prt /  commands  \  server  \  printer  

Here, server is the name of the printer server containing the shared printer to map. The available commands are:

  • /F ”Removes all printer connections

  • /C ”Connects to the printer specified

  • /CD ”Connects to the printer specified and marks it as the default printer

To remove all current printer connections and map a default printer using con2PRT, proceed as follows:

  1. Create a new directory to store all files included in this example.

  2. Select StartRun and enter " scriptfile .bat."

Here, scriptfile is the full path and file name of a script file that contains the following:

 @Echo Off Set  Pserver  =  server  Set  DPrinter  =  Printer  
  fullpath  \con2prt /F  fullpath  \con2prt /CD \%  server  %\%  printer  % 

Here, pserver is the variable holding the printer server name; dprinter is the variable holding the name of the printer share; and fullpath is the full path where con2prt is located.

Adding Printers Using the PrintUI DLL

Windows 2000/XP/2003 includes the PrintUI.dll to add and remove printers from the command line. To use the PrintUI.dll, you must call the PrintUIEntry function through the rundll32 command. To add a default printer using the PrintUI DLL, start a command prompt and enter the following:

 rundll32 printui.dll,PrintUIEntry /in /y /n \pserver\dprinter 

Here, pserver is the name of the print server and dprinter is name of the printer share.

Checking for Remote Access

Determining whether a client is logging in through the network or remote access helps you specify which parts of the script to run. CheckRAS is a command-line, SMS resource kit utility to determine whether a user is using remote access. To determine whether the current user is using remote access during a logon script, proceed as follows:

  1. Create a new directory to store all files included in this example.

  2. Select StartRun and enter " scriptfile .bat."

Here, scriptfile is the full path and file name of a script file that contains the following:

 @Echo Off CLS ; Clears the screen Set  RAS  =NO  fullpath  \CheckRAS > Nul If %  errorlevel  % EQU 1 Set  RAS  =YES 

Here, fullpath is the full path where the CheckRAS utility is located, and RAS indicates whether the current user is using remote access or not.

Displaying Time-Based Greetings

Although it's not essential, many administrators like to display a greeting to the user depending on the time of day. To display a time-based greeting from the command line, proceed as follows:

  1. Create a new directory to store all files included in this example.

  2. Select StartRun and enter " scriptfile .bat."

Here, scriptfile is the full path and file name of a script file that contains the following:

 @Echo Off CLS For /F "Delims=: Tokens=1" %%  I  in ('Time /T') Do Set  Hour  =%%  I  For /F "Delims=: Tokens=2" %%  I  in ('Time /T') Do Set  Min  =%%  I   For /F "Delims=0,1,2,3,4,5,6,7,8,9 Tokens=2" %%  I  in ('Set  Min  ') Do Set  AP  =%%  I   If %  AP  % EQU p Goto PM Set  Greet  =Good Morning Goto End :PM If %  Hour  % EQU 12 Set  Hour  =0 If %  Hour  % LSS 12 Set  Greet  =Good Evening If %  Hour  % LSS 6 Set  Greet  =Good Afternoon :End Echo %  Greet  % Set  Hour  = Set  Min  = Set  AP  = 
Note  

The highlighted code above should be placed on one line.

Here, the Time /T command indicates the local system time.

Updating McAfee Antivirus Files

To update your McAfee antivirus engine and/or signature files with shell scripting, proceed as follows:

  1. Create a new directory to store all files included in this example.

  2. Select StartRun and enter " scriptfile .bat."

Here, scriptfile is the full path and file name of a script file that contains the following:

 @Echo Off CLS Set  SDAT  ="  superdat  " Set  DAT  ="  datfile  " Set  NAILOG  ="  textlog  " Set  DDAY  ="  DOTW  " For /F "Tokens=1"  %%I  in ('Date /T') Do Set  Day  =  %%I  If  %DAY%  EQU  %DDAY%  Goto UENGINE  %DAT%  /F /PROMPT /REBOOT /SILENT /LOGFILE  %NAILOG%  GOTO END :UENGINE  %SDAT%  /F /PROMPT /REBOOT /SILENT /LOGFILE  %NAILOG%  GOTO END :END Set  SDAT  = Set  DAT  = Set NAILOG= Set  DAY  = 

Here, SDAT is a variable containing the complete path and file name of the SuperDAT executable; DAT is a variable containing the complete path and file name of the DAT executable; NAILOG is a variable containing the complete path and file name of the status log text file; and DDAY is the day of the week (Mon, Tue, Wed, Thu, Fri, Sat, Sun) to run the SuperDAT as opposed to the daily DAT file.

Updating Norton Antivirus Files

To update your Norton antivirus files with shell scripting, proceed as follows:

  1. Create a new directory to store all files included in this example.

  2. Download the latest Intelligent Updater file from http://www. symantec .com to the new directory.

  3. Select StartRun and enter " scriptfile .bat."

Here, scriptfile is the full path and file name of a script file that contains the following:

 @Echo Off Set  IUPDATER  =  iufile   %IUPDATER%  /Q > Nul 

Here, IUPDATER is a variable containing the complete path and file name of the Intelligent Updater executable.

Creating Logon Scripts with KiXtart

KiXtart is a powerful scripting tool primarily focused and used for logon scripts. KiXtart contains many built-in methods and macros to retrieve quick information; other scripting languages would require external tools or extensive scripting to retrieve the same information.

Setting Up the Environment

When creating a logon script, it is important to make sure the script looks and feels as it was intended. KiXtart includes several commands to customize the logon script environment. To set up a customized logon script environment using KiXtart, proceed as follows:

  1. Create a new directory to store all files included in this example.

  2. Download and extract the latest version of KiXtart, from http://www.kixtart.org, to the new directory.

  3. Select StartRun and enter "kix32 scriptfile ."

Here, scriptfile is the full path of the new directory from step 1 and file name of a script file that contains the following:

 CLS ; Clear screen BREAK OFF ; Logoff user when attempt to close logon box $RCODE= SETCONSOLE("ALWAYSONTOP") ; Set box on top  $RCODE  = SETASCII("ON") ; Turn on ASCII characters 

This script first clears the screen (CLS) and sets the logon script box to log off the current user if he/she attempts to close the box. The logon script box is then set to be on top of all other windows. The final command turns on ASCII characters. This is a new feature included with KiXtart 3.62 and higher that allows you to change the look of text by turning ASCII on or off.

Changing the Background and Foreground Colors

KiXtart supports the color command to change the background and foreground in a shell prompt. The basic syntax of the color command is as follows:

 COLOR  Fx  /  By  

Here, F is the foreground color value, x is an optional indicator to increase the color intensity if a plus sign (+) is specified, B is the background color value, and y is an optional indicator that causes the background to blink if a plus sign (+) is specified. The color command supports the following color values:

  • N ”Black

  • B ”Blue

  • G ”Green

  • C ”Cyan

  • R ”Red

  • M ”Magenta

  • Y ”Yellow/Brown

  • W ”White

Here is an example to change the shell prompt colors to bright white text on a blue background:

 COLOR W+/B 

Synchronizing the Local System Time

Synchronizing the local system to a central time source allows you to perform enterprise-wide tasks simultaneously. KiXtart includes the SetTime command to synchronize the local system time to a time source. The basic syntax of the SetTime command is as follows:

 SetTime  source  

Here, source is any one of the following types:

  • \\ Server ”Specifies the name of a time source server

  • DomainName ”Searches the specified domain for a time source

  • "*" ”Specifies to search the local domain for a time source

The following script attempts to sync the local system time with the logon server. If this fails, the domain will be searched for a time source to sync with. To execute this script, proceed as follows:

  1. Create a new directory to store all files included in this example.

  2. Download and extract the latest version of KiXtart, from http://www.kixtart.org, to the new directory.

  3. Select StartRun and enter "kix32 scriptfile. "

Here, scriptfile is the full path of the new directory from step 1 and file name of a script file that contains the following:

 ? "Syncing the time with @LSERVER..." SETTIME "@LSERVER" If @ERROR <> 0   ? "Searching the local domain for a time-server..."   SETTIME "*"   If @ERROR <> 0     ? "Time Sync Error"   Else     ? "Sync Successful"   EndIf EndIf 

Mapping Universal Drives

Mapping common drives for all users allows you to present a central resource location for applications or data. In Chapter 8, you learned how to map network drives from within Windows and the command prompt. KiXtart includes the use command, similar to the Net Use command, to attach a drive letter to a network share. To map a network drive and display the status using KiXtart, proceed as follows:

  1. Create a new directory to store all files included in this example.

  2. Download and extract the latest version of KiXtart, from http://www.microsoft.com, to the new directory.

  3. Select StartRun and enter "kix32 scriptfile. "

Here, scriptfile is the full path of the new directory from step 1 and file name of a script file that contains the following:

  $Drive  ="  DriveLetter  "  $Share  ="\  server  \  sharename  " ? " Mapping drive  $Drive  to  $Share  " Use "  $Drive  : " /Delete Use "  $Drive  : "  $Share  If @Error = 0   ? " Map Successful" Else   ? " Error mapping drive  $Drive  to  $Share  " EndIf 

Here, driveletter is the drive letter to map a share to, and server contains the sharename you want to map to.

Mapping Drives by Group

Mapping drives by group membership allows you to control which drives and resources will be available to which users. KiXtart includes the InGroup command, similar to the IfMember resource kit utility, to determine group membership. To map a network drive according to group membership and display the status using KiXtart, proceed as follows:

  1. Create a new directory to store all files included in this example.

  2. Download and extract the latest version of KiXtart, from http://www.kixtart.org, to the new directory.

  3. Select StartRun and enter "kix32 scriptfile. "

Here, scriptfile is the full path of the new directory from step 1 and file name of a script file that contains the following:

 $Drive="DriveLetter" $Share="\server\sharename" If InGroup("  GroupName  ")   ? "Mapping drive  $Drive  to  $Share  "   Use "  $Drive  : "/Delete   Use "  $Drive:  "  $Share  If @Error = 0     ? "Map Successful"   Else     ? "Error mapping drive  $Drive  to  $Share  "   EndIf EndIf 

Here, GroupName is the name of the group to check membership; driveletter is the drive letter to map a share to; and server contains the sharename you want to map to.

Mapping Printers

Mapping printers through a logon script provides an easy method to remotely update printer connections. KiXtart contains several commands to add, remove, and set default printers. To map a printer using KiXtart, proceed as follows:

  1. Create a new directory to store all files included in this example.

  2. Download and extract the latest version of KiXtart, from http://www.kixtart.org, to the new directory.

  3. Select StartRun and enter "kix32 scriptfile. "

Here, scriptfile is the full path of the new directory from step 1 and file name of a script file that contains the following:

 $  Pserver  ="  Server  " $  DPrinter  ="  Printer  " If AddPrinterConnection("\$  PServer  $  DPrinter  ") = 0   ? "Added printer $  DPrinter  " Else   ? "Error adding $  DPrinter  " EndIf 

Here, pserver is the variable holding the printer server name, and dprinter is the variable holding the name of the printer share.

Checking for Remote Access

Determining whether a client is logging in through the network or remote access helps you specify which parts of the script to run. KiXtart includes the @RAS macro to the number of remote access connections. To determine whether a user is logging on through remote access using KiXtart, proceed as follows:

  1. Create a new directory to store all files included in this example.

  2. Download and extract the latest version of KiXtart, from http://www.kixtart.org, to the new directory.

  3. Select StartRun and enter "kix32 scriptfile. "

Here, scriptfile is the full path of the new directory from step 1 and file name of a script file that contains the following:

 If @RAS = 0   ? " You are logging in through the local network. " Else   ? "You are logging on through remote access" EndIf 

Displaying Time-Based Greetings

Although it's not essential, many administrators like to display a greeting to the user depending on the time of day. To display a time-based greeting using KiXtart, proceed as follows:

  1. Create a new directory to store all files included in this example.

  2. Download and extract the latest version of KiXtart, from http://www.kixtart.org, to the new directory.

  3. Select StartRun and enter "kix32 scriptfile. "

Here, scriptfile is the full path of the new directory from step 1 and file name of a script file that contains the following:

 SELECT   CASE ((@TIME > "00:00:00") AND (@TIME < "12:00:00"))     ? "Good Morning @FULLNAME"   CASE ((@TIME > "12:00:00") AND (@TIME < "18:00:00"))     ? "Good Afternoon @FULLNAME"   CASE 1     ? "Good Evening @FULLNAME" ENDSELECT 

Here, the @TIME macro indicates the current time, and @FULLNAME indicates the full name of the current user.

Updating McAfee Antivirus Files

To update your McAfee antivirus engine and/or signature files with KiXtart, proceed as follows:

  1. Create a new directory to store all files included in this example.

  2. Download and extract the latest version of KiXtart, from http://www.kixtart.org, to the new directory.

  3. Select StartRun and enter "kix32 scriptfile. "

Here, scriptfile is the full path of the new directory from step 1 and file name of a script file that contains the following:

  $SDAT  ="  superdat  "  $DAT  ="  datfile  "  $NAILOG  ="  textlog  "  $DDAY  ="  DOTW  " If @DAY =  $DDAY   SHELL "  %COMSPEC%  /C  $SDAT  /F /PROMPT /REBOOT /SILENT   /LOGFILE  $NAILOG  "  Else  SHELL "  %COMSPEC%  /C  $DAT  /F /PROMPT /REBOOT /SILENT   /LOGFILE  $NAILOG"   EndIf 

Here, SDAT is a variable containing the complete path and file name of the SuperDAT executable; DAT is a variable containing the complete path and file name of the DAT executable; NAILOG is a variable containing the complete path and file name of the status log text file; and DDAY is the day of the week (Monday-Sunday) to run the SuperDAT as opposed to the daily DAT file.

Updating Norton Antivirus Files

To update your Norton antivirus files with KiXtart, proceed as follows:

  1. Create a new directory to store all files included in this example.

  2. Download the latest Intelligent Updater file from http://www.symantec.com to the new directory.

  3. Download and extract the latest version of KiXtart, from http://www.kixtart.org, to the new directory.

  4. Select StartRun and enter "kix32 scriptfile. "

Here, scriptfile is the full path of the new directory from step 1 and file name of a script file that contains the following:

  $IUPDATER  = "  iufile"  SHELL "%COMSPEC% /C  $IUPDATER  /Q" 

Here, IUPDATER is a variable containing the complete path and file name of the Intelligent Updater executable.

Creating Logon Scripts with Windows Script Host

Windows Script Host is a relatively new scripting language and is rather limited with logon scripts. Although you can call external functions or custom COM objects to perform specific logon script tasks, WSH does not contain many of the standard logon script functions other scripting languages may have, such as a time synchronization command.

Synchronizing the Local System Time

Windows Script Host does not have a time synchronization command to sync the local system time with a network time source. You can use the shell run command to call external commands, such as the Net Time command, and use a return variable to indicate whether the command was successful. The following script attempts to sync the local system time with the server named servername using the Net Time command. If this synchronization fails, the domain will be searched for a time source to sync with. To execute this script, proceed as follows:

  1. Create a new directory to store all files included in this example.

  2. Download and install the latest version of Windows Script Host, from http://www.microsoft.com, to the new directory.

  3. Select StartRun and enter "cscript scriptfile .vbs."

Here, scriptfile is the full path and file name of a script file that contains the following:

 On Error Resume Next Set  SHELL  = CreateObject("WScript.Shell")  TServer  ="  ServerName  " Wscript.Echo "Syncing the time with " &  TServer  & "..."  ELevel = Shell.Run("Net Time \" &  TServer  & " /Set /Yes",0,True)  If (  ELevel  <> 0) Then   Wscript.Echo "Searching the local domain for a " & _    "time-server..."  ELevel  =  Shell  .Run("Net Time / Set /Yes",0,True) If (  ELevel  = 0) Then    Wscript.Echo "Sync Successful" Else    Wscript.Echo "Time Sync Error"  End If Else    Wscript.Echo "Sync Successful" End If 
Note  

The highlighted code above must be placed on one line.

Mapping Universal Drives

Mapping common drives for all users allows you to present a central resource location for applications or data. In Chapter 8, you learned how to map network drives from within Windows and the command prompt. You can use the Windows Script Host network object to attach a drive letter to a network share. To map a network drive and display the status using Windows Script Host, proceed as follows:

  1. Create a new directory to store all files included in this example.

  2. Download and install the latest version of Windows Script Host, from http://www.microsoft.com, to the new directory.

  3. Select StartRun and enter "cscript scriptfile .vbs."

Here, scriptfile is the full path and file name of a script file that contains the following:

 On Error Resume Next Set  Network  = CreateObject("WScript.Network")  Drive  = "  DriveLetter  :" Share = "\  server  \  sharename  " Wscript.Echo "Mapping drive " &  Drive  & " to " &  Share Network  .MapNetworkDrive  Drive  ,  Share  If Err.Number = 0 Then   Wscript.Echo "Map Successful" Else   Wscript.Echo "Error mapping drive " &  Drive  & " to " & _  Share  End If 

Here, driveletter is the drive letter to map a share to, and server contains the sharename you want to map to.

Mapping Drives by Group

Mapping drives by group membership allows you to control which drives and resources will be available to which users. Windows Script Host does contain a method to determine group membership. Although you can use the ADSI IfMember method, this method can be slow on larger networks. Alternatively, you can use the WSH shell run command to call external commands, such as the IfMember resource kit utility, and use a return variable to indicate whether the command was successful. To map a network drive according to group membership and display the status using Windows Script Host, proceed as follows:

  1. Create a new directory to store all files included in this example.

  2. Download and install the latest version of Windows Script Host, from http://www.microsoft.com, to the new directory.

  3. Select StartRun and enter "cscript scriptfile .vbs."

Here, scriptfile is the full path and file name of a script file that contains the following:

 On Error Resume Next Set  SHELL  = CreateObject("WScript.Shell") Set  Network  = CreateObject("WScript.Network")  Drive  = "  DriveLetter  :"  Share  = "\  server  \  sharename  "  DGroup  = "  groupname  "  ELevel  =  Shell  .Run("  fullpath  \IfMember " & DGroup,0,True) If (  ELevel  = 1) Then   Wscript.Echo "Mapping drive " &  Drive  & " to " &  Share   Network  .MapNetworkDrive  Drive  ,  Share  If Err.Number = 0 Then     Wscript.Echo "Map Successful"   Else     Wscript.Echo "Error mapping drive " & Drive & " to " & _  Share  End If End If 

Here, fullpath is the full path where the IfMember utility is located; GroupName is the name of the group to check membership; driveletter is the drive letter to map a share to; and server contains the sharename you want to map to.

Mapping Printers

Mapping printers through a logon script provides an easy method to remotely update printer connections. Starting with version 2, Windows Script Host provides several commands to add, remove, and set default printers. To map a printer using Windows Script Host, proceed as follows:

  1. Create a new directory to store all files included in this example.

  2. Download and install the latest version of Windows Script Host, from http://www.microsoft.com, to the new directory.

  3. Select StartRun and enter "cscript scriptfile .vbs."

Here, scriptfile is the full path and file name of a script file that contains the following:

 On Error Resume Next  PServer  = "  Server  "  DPrinter  = "  Printer  "  Port  = "LPT1" Set  Network  = CreateObject("Wscript.Network")  Network.AddPrinterConnection  Port  , "\" &  PServer  & "\" &  Printer   If Err.Number <> 0 Then   Wscript.Echo "Added printer " & Printer Else   Wscript.Echo "Error adding printer " &  Printer  End If 
Note  

The highlighted code above must be placed on one line.

Here, pserver is the variable holding the printer server name, and dprinter is the variable holding the name of the printer share.

Tip  

You can use the AddWindowsPrinterConnection method to add printers to Windows NT/2000 systems without having to supply a port.

Checking for Remote Access

Determining whether a client is logging in through the network or remote access helps you specify which parts of the script to run. Windows Script Host does not contain a method to detect remote access connections. CheckRAS is a command-line, SMS resource kit utility to determine whether a user is using remote access. To determine whether the current user is using remote access during a logon script using Windows Script Host, proceed as follows:

  1. Create a new directory to store all files included in this example.

  2. Download and install the latest version of Windows Script Host, from http://www.microsoft.com, to the new directory.

  3. Select StartRun and enter "cscript scriptfile .vbs."

Here, scriptfile is the full path and file name of a script file that contains the following:

 On Error Resume Next Set  SHELL  = CreateObject("WScript.Shell")  ELevel  =  Shell  .Run("  fullpath  \CheckRAS",0,True) If (  ELevel  = 0) Then  R  AS  = "YES" Else  RAS  = "NO" End If 

Here, fullpath is the full path where the CheckRAS utility is located, and RAS indicates whether the current user is using remote access or not.

Displaying Time-Based Greetings

Although it's not essential, many administrators like to display a greeting to the user depending on the time of day. To display a time-based greeting using Windows Script Host, proceed as follows:

  1. Create a new directory to store all files included in this example.

  2. Download and install the latest version of Windows Script Host, from http://www.microsoft.com, to the new directory.

  3. Select StartRun and enter "cscript scriptfile .vbs."

Here, scriptfile is the full path and file name of a script file that contains the following:

 On Error Resume Next If Hour(Now) < 12 Then   Wscript.Echo "Good Morning" ElseIf Hour(Now) < 18 Then   Wscript.Echo "Good Afternoon" Else   Wscript.Echo "Good Evening" End If 

Updating McAfee Antivirus Files

To update your McAfee antivirus engine and/or signature files with shell scripting, proceed as follows:

  1. Create a new directory to store all files included in this example.

  2. Download and install the latest version of Windows Script Host from http://www.microsoft.com to the new directory.

  3. Select StartRun and enter "cscript scriptfile .vbs."

Here, scriptfile is the full path and file name of a script file that contains the following:

 On Error Resume Next Set  SHELL  = CreateObject("WScript.Shell")  SDAT  ="  superdat  "  DAT  ="  datfile  "  NAILOG  ="  textlog  "  DDAY  ="  DOTW  " If WeekDayName(WeekDay(Date)) =  DDAY    Shell  .Run CHR(34) &  SDAT  & CHR(34) & " /F /PROMPT /REBOOT   /SILENT /LOGFILE  NAILOG  ",1,True  Else   Shell  .Run CHR(34) &  DAT  & CHR(34) & " /F /PROMPT /REBOOT   /SILENT /LOGFILE  NAILOG  ",1,True  EndIf 
Note  

The highlighted code above must be placed on one line. Chr(34) translates the ASCII code character 34 into a quotation mark ("). This is necessary when using the Shell.Run command with long file names .

Here, SDAT is a variable containing the complete path and file name of the SuperDAT executable; DAT is a variable containing the complete path and file name of the DAT executable; NAILOG is a variable containing the complete path and file name of the status log text file; and DDAY is the day of the week (Monday-Sunday) to run the SuperDAT as opposed to the daily DAT file.

Updating Norton Antivirus Files

To update your Norton antivirus files with Windows Script Host, proceed as follows:

  1. Create a new directory to store all files included in this example.

  2. Download the latest Intelligent Updater file from http://www.symantec.com to the new directory.

  3. Download and install the latest version of Windows Script Host, from http://www.microsoft.com, to the new directory.

  4. Select StartRun and enter "cscript scriptfile .vbs."

Here, scriptfile is the full path and file name of a script file that contains the following:

 On Error Resume Next Set  SHELL  = CreateObject("WScript.Shell")  IUPDATER  = "  iufile" Shell  .Run CHR(34) &  IUPDATER  & CHR(34) & " /Q",1,True 
Note  

Chr(34) translates the ASCII code character 34 into a quotation ("). This is necessary when using the Shell.Run command with long file names.

Here, IUPDATER is a variable containing the complete path and file name of the Intelligent Updater executable.

Using Microsoft Internet Explorer as a Logon Script Box

Through Automation, you can use Internet Explorer to display logon script status to the user. To use Internet Explorer as a logon script box using the previous WSH logon scripts, proceed as follows:

  1. Create a new directory to store all files included in this example.

  2. Download and install the latest version of Windows Script Host, from http://www.microsoft.com, to the new directory.

  3. Select StartRun and enter "cscript scriptfile .vbs."

Here, scriptfile is the full path and file name of a script file that contains the following:

 On Error Resume Next Set  Network  = CreateObject("WScript.Network") Set  MSIE  = CreateObject("InternetExplorer.Application")  sTITLE  = "Processing Logon Script, please wait..."  Drive  = "  DriveLetter  :"  Share  = "\  server  \  sharename  " SetupMSIE  MSIE  .Document.Write "<HTML><TITLE>" & sTitle & _  "</TITLE><BODY bgcolor=#C0C0C0><FONT FACE=ARIAL>" IfHour(Now) < 12 Then  MSIE  .Document.Write "<B>Good Morning " & _  Network  .UserName & "</B><BR><BR>" ElseIf Hour(Now) < 18 Then  MSIE  .Document.Write "<B>Good Afternoon " & _  Network  .UserName & "</B><BR><BR>" Else  MSIE  .Document.Write "<B>Good Evening " & _  Network  .UserName & "</B><BR><BR>" End If  MSIE  .Document.Write "<B>Mapping drive " &  Drive  & " to " & _  Share  & "...</B><BR>"  Network  .MapNetworkDrive  Drive  ,  Share  If Err.Number = 0 Then  MSIE  .Document.Write " Mapping Successful<BR>" Else  MSIE  .Document.Write " Error mapping drive " &  Drive  & _    " to " &  Share  & "<BR>" End If  MSIE  .Document.Write "<BR><B>Closing in 3 seconds</B><BR>" Wscript.Sleep 3000  MSIE  .Quit Sub SetupMSIE  MSIE  .Navigate "About:Blank"  MSIE  .ToolBar = False  MSIE  .StatusBar = False  MSIE  .Resizable = False  Do  Loop While  MSIE  .Busy  SWidth  =  MSIE  .Document.ParentWindow.Screen.AvailWidth  SHeight  =  MSIE  .Document.ParentWindow.Screen.AvailHeight  MSIE  .Width =  SWidth  /2  MSIE  .Height =  SHeight  /2  MSIE  .Left = (  SWidth  -  MSIE  .Width)/2  MSIE  .Top = (  SHeight  -  MSIE  .Height)/2  MSIE  .Visible = True End Sub 

Here, driveletter is the drive letter to map a share to, and server contains the sharename you want to map to.

Related solution:

Found on page:

Using Microsoft Internet Explorer as a Display Tool

100




Windows Admin Scripting Little Black Book
Windows Admin Scripting Little Black Book (Little Black Books (Paraglyph Press))
ISBN: 1933097108
EAN: 2147483647
Year: 2004
Pages: 89

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net