Comparing Logon Script Alternatives
Scripting Competition
Logon scripts provide an important service when setting up a PC workstation: They create an individual workplace environment for each employee based on the employee's logon context. On Windows XP, scripting was limited to batch files and VBScripts; however, PowerShell has elevated the scripting game since Windows 7.
Is PowerShell really so much better, though? To try and find some clarity in this matter, I put the three technologies – batch (statements and binaries interpreted by cmd.exe
), VBScript (Visual Basic Script Edition, processed by Wscript.exe
or cscript.exe
), and PowerShell (version 3; console with additional script engine for PS1 files) – to the test. In doing so, I consider:
- How powerful is each technology?
- What are the limitations and restrictions?
- How intuitive is each technology, and how well is it documented?
Objective and Scenario
Logon script targets can be divided roughly into two areas: infrastructure and local system. Infrastructural objectives could be managing shares, particularly mapping network drives, or connecting a network printer and setting the default printer. For local systems, it might be necessary to run programs, change a registry key, or create a link.
The typical tasks I will give the three Microsoft scripting engines in this test involve mapping shares, adding a network printer, running a file, and changing the registry. Additionally, I expect to see output in the event of an error.
Mapping Shares
Connecting to shares on a server is a standard task in setting up a desktop. The DOCS share on server SRV will be mapped as drive M . To simplify matters, I am omitting the transfer of user information for securing rights for this action.
Batch: The batch command for this action is:
net use m: \\SRV\DOCS
The net.exe
file handles this command. The mixture of command interpreter access and commands is typical of batch files.
VBScript: The VBScript command is:
Set ObjNet = Wscript.CreateObject("Wscript.Network") ObjNet.MapNetworkDrive("m:","\\SRV\DOCS")
A VBScript logon script only provides the control constructs such as loops, branches, and so on. The actual functionality is in a collection of component object model (COM) objects (standard for software development at Microsoft) – in particular, the Windows Script Host (WSH) run-time library.
In this example, I create an object based on the WScript.Network
class in order to then use the MapNetworkDrive
method.
PowerShell: The PowerShell command is:
New-PsDrive -name m -Root \\SRV\DOCS \ -PsProvider FileSystem -persist
PowerShell uses the cmdlet command type. Simplified, cmdlets can be understood as an extension of .NET types. The <verb>-<noun>
structure makes usage very intuitive. Here, the New
action is connected with the PsDrive
target.
Printer Management
The second step involves sharing a network printer with the user. The server has the familiar name SRV , and the shared printer is referred to as PP . I also want this printer to be the default printer.
Batch: First up is the batch command:
Rundll32 printui.dll,PrintUIEntry /in /n /Y \\SRV\PP
The Rundll32.exe
application steps into the breach for the weak internal commands of command.com
. Here, it defines the printer as a network printer (/in
) with a name (/n
) in the form of \\<server>\<sharing name>
. The default printer property is set with the /Y
option.
VBScript: Visual Basic uses the following command chain for printer configuration:
Set ObjNet = Wscript.CreateObject("Wscript.Network") ObjNet.AddWindowsPrinterConnection("\\SRV\PP")
This is the second WScript.Network
type method. VBScript proves to rely very heavily on COM. Later, I will look at whether this is an advantage or disadvantage.
The same object can also carry out part two of the task,
ObjNet.SetDefaultPrinter("\\SRV\PP")
which defines the printer as the default.
PowerShell: PowerShell is more flexible with this command:
$pr = get-WmiObject -Class "win32_Printer" $pr.AddWindowsPrinterConnection("\\SRV\PP")
It can access, among other things, .NET types and WMI classes. The new printer receives the default
flag,
$default = get-wmiObject -Class win32_Printer \ -Filter "name = 'PP'" $default.SetDefaultPrinter()
which identifies it in the list of available printers. This is somewhat elaborate, but meaningful.
Buy this article as PDF
(incl. VAT)