Previous Page
Next Page

Output Information

Once you work through each case in the Select Case statement, you enter into a subroutine. Each subroutine is designed around the particular needs of various groups within your organization. The WScript.Echo commands let you know which subroutine is being run. These are primarily used for troubleshooting and can be either left in or deleted, depending on the type of customer experience your users are willing to put up with.

To map a network drive, you use the MapNetworkDrive method of a WshNetwork object. The important issue to keep in mind here is that assigning a drive letter requires a letter and a colon surrounded by double quotation marks. Next, a comma is required to separate the drive letter from the path statement.

When you use WshNetwork to map to a printer, you use the AddWindowsPrinterConnection method. (Although this command name is descriptive, it could have been shortened just a little.) The AddWindowsPrinterConnection method needs only a Universal Naming Convention (UNC) path to the print server and the share name. No commas are required here. (In fact, commas here will cause the command to fail.)

The last task our subroutine needs to perform is assigning the default Windows printer, so you use a method named SetDefaultPrinter. Again, the only work you need to do is include the UNC path to the print server and encase the share name in double quotation marks. Here are the subroutines for the Worker information section of the script:

Sub HRsub
wshNet.MapNetworkDrive "g:",fServer & "\Hr"
  wshNet.AddWindowsPrinterConnection pServer &"\HrPrinter"
  wshNet.SetDefaultPrinter pServer & "\HrPrinter"
  subRunScript
End Sub

Sub SalesSub
  wshNet.MapNetworkDrive "s:", fServer & "\Sales"
  wshNet.AddWindowsPrinterConnection pServer & "\SalesPrinter"
  wshNet.SetDefaultPrinter pServer & "\SalesPrinter"
End Sub

Sub MarketingSub
         wshNet.MapNetworkDrive "m:", fServer & "\Marketing"
  wshNet.AddWindowsPrinterConnection pServer & "\MarketingPrinter"
  wshNet.SetDefaultPrinter pServer & "\MarketingPrinter"
End Sub

Note

To run the following script, you will need to have the following: AD configured; a server called London; several groupsHRGroup, SalesGroup, MarketingGroup; file shares; print shares; a set of users; and a set of home directories. Please edit the script below to match your own test environment.


Call an additional script during logon

1.
Open \My Documents\Microsoft Press\VBScriptSBS\ch16\LogonScript.vbs in Notepad or the script editor of your choice and save it as YourNameLogonScriptCall ExternalScript.vbs.

2.
At the bottom of your script, create a subroutine called subRunScript. This will look like the following:

Sub subRunScript

End Sub

3.
Declare a variable, objShell, that will be used to hold the wshShell object. These are added into the subRunScript subroutine.

4.
Create an instance of the wshShell object and assign it to the objShell variable. Use the CreateObject method to do this, as seen in the following code:

Set objShell = CreateObject("WScript.Shell")

5.
Use the Run method to launch the additional script. This is seen in the following code:

objShell.run ("CheckForHotFix.vbs")

6.
Save and run your script by logging in from a remote machine (because ordinary users do not have the ability to log on to the domain controller). If your script has problems, compare it with \My Documents\Microsoft Press\VBScriptSBS\ch16\LogonScriptCallExternalScript.vbs.

Capture an error from calling an additional script during logon

1.
Open the \My Documents\Microsoft Press\VBScriptSBS\ch16\LogonScriptCall ExternalScript.vbs script in Notepad or your script editor of choice and save it as YourNameLogonScriptCallExternalScriptCaptureError.vbs.

2.
In the subRunScript subroutine, add a variable to hold the return code from the Run method. Call this variable intRTN.

3.
On a new line after the intRTN variable, declare a constant called HideWindow and set it equal to 1.

4.
On a new line after the HideWindow constant, create a new constant called WaitForReturn and set it equal to True. The completed Header and Reference section of the subRunScript subroutine now looks like the following:

Dim objShell
Dim intRTN
Const HideWindow = 0
Const WaitForReturn = True

5.
Edit the objShell.run line of code, so that you use the intRTN variable to capture the return code. Add the HideWindow constant and the WaitForReturn constants as additional parameters to the Run method. The completed line now looks like the following:

intRTN = objShell.run ("CheckForHotFix.vbs",HideWindow,WaitForReturn)

6.
Use an inline If...Then statement to evaluate intRTN. If intRTN is not equal to 0, then use WScript.Echo to print out the error. This is seen below:

If intRTN <> 0 Then WScript.Echo "Error",intRTN,"occurred"

7.
Save and run your script by logging into the domain from a remote workstation. If your script does not work as expected, compare it with \My Documents\Microsoft Press \VBScriptSBS\ch16\ LogonScriptCallExternalScriptCaptureError.vbs.


Previous Page
Next Page