Introduction Logon Scripts
In this section I will give you examples of how to build the VBScript to use in your logon script. I will help you chose which methods to use and which variables to change. There are two sections; one section specialises in mapped network drives, while the other section deals with scripts for printers. Please choose a Windows Logon Script to suit your particular task.
Map Network Drive Logon Scripts
- MapNetworkDrive (Basic) – Getting started
- MapNetworkDrive (Adv) – 5 Arguments e.g. bForce
- Multiple Network Drives – Map to more than one UNC
- Mapping to UserName – Map one level further down
- RemoveNetworkDrive – Prevent script failing
- Rename Network Drive – Rename, Share Server (W:)
- EnumNetworkDrives – Get a ‘handle’ on the drives
- Drives already connected – Logic if drive exists
- Map First Available Drive Letter – See 2 Loops
- Troubleshooting Logon Scripts – Assign logon script
- Add Error Correcting Code – On Error
- Troubleshooting Logon Scripts – Assign logon script
♦
Connect to Printers Logon Scripts
- AddWindowsPrinterConnection – Join to UNC path
- SetDefaultPrinter – Which printer will be the default
- Add Multiple Printers – Add a second printer
- EnumPrinterConnections – Extends other methods
- RemovePrinterConnection – Deleting printer icons
- Connect a Printer to a Computer – Policy Loopback
- PowerShell Logon Script – Windows 7 Techniques
- Troubleshooting Logon Scripts – VBScript or AD?
Introduction to Logon Scripts with VBScript
When I first saw Windows 2003, I investigated the new features. I wondered: ‘How do I assign logon scripts to users?’ I came up with two methods; the traditional NT 4.0 style or assigning VBScripts via Active Directory Group Policies. (The traditional method was via the User’s Properties, Profile tab.)
I even took a I step back and asked the more fundamental question: ‘What am I trying to achieve here?’ The answer was, ‘To provide users with the best environment for their work’. This lead me to investigate VBScript methods for providing mapped network drives and connecting to shared printers.
Good news. Scripting possibilities improve dramatically when you progress from the old DOS batch files, to the new WSH with its rich VBScript verbs. When you survey the big picture, you realize that Logon Scripts are only one of many jobs for WSH and VBScript. My point is that knowledge of these scripting objects, syntax and methods will help you automate other computer tasks. As a bonus, if you master assigning Logon Scripts by Group Policy, then you will see how to apply other settings using Group Policy (GPMC).
Guy Recommends: A Free Trial of the Network Performance Monitor (NPM)
v11.5
SolarWinds’ Orion performance monitor will help you discover what’s happening on your network. This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.
What I like best is the way NPM suggests solutions to network problems. Its also has the ability to monitor the health of individual VMware virtual machines. If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.
Download a free trial of Solarwinds’ Network Performance Monitor
Example of a Logon Script
VBScript to map a network drive. Here are step-by-step instructions to create this logon script.
‘ MNDUserName.vbs
‘ VBScript to map a network drive to the UserName.
‘ Author Guy Thomas https://computerperformance.co.uk/
‘ Version 1.3 – April 24th 2010
‘ —————————————————-‘
Option Explicit
Dim objNetwork
Dim strDriveLetter, strRemotePath, strUserName
strDriveLetter = "J:"
strRemotePath = "\\grand\home"
‘ Purpose of script to create a network object. (objNetwork)
‘ Then to apply the MapNetworkDrive method. Result J: drive
Set objNetwork = WScript.CreateObject("WScript.Network")
‘ Here is where we extract the UserName
strUserName = objNetwork.UserName
objNetwork.MapNetworkDrive strDriveLetter, strRemotePath _
& "\" & strUserName
‘ Extra code just to add a message box
WScript.Echo " Launch Explorer, check: "& strDriveLetter
WScript.Quit
‘ End of MapNetworkDrive Example logon script.
More Tasks for VBScript
WSH provides the objects, which our scripts then manipulate. Here are examples of the tasks that these objects and services can do for you:
- Map network drives (Then assign to Logon Script)
- Connect to printers (Then assign to Logon Script)
- Manipulate Active Directory objects, e.g. Users passwords
- Run basic functions such as CreateObject and GetObject
- Modify environment variables e.g. Temp
- Modify keys and values in the registry
- Print a message to a Message Box on screen
- Combine with WMI to measure and automate the operating system
Guy Recommends: SolarWinds’ Free Bulk Import Tool
Import users from a spreadsheet. Just provide a list of the users with their fields in the top row, and save as .csv file. Then launch this FREE utility and match your fields with AD’s attributes, click and import the users.
Optionally, you can provide the name of the OU where the new accounts will be born. Download your FREE bulk import tool.
If you need more comprehensive software, download a free trial of SAM (Server & Application Monitor)
What is WSH? (Windows Scripting Host)
Windows Scripting Host is aptly named; think of WSH as a butler who pampers their guests. When a logon script arrives from the dedicated server,WSH makes sure that all the objects and services that the script needs are available. It alsochecks discretely for security and passes the script to the appropriate script engine, for example, ActiveX.
WSH is ideal for non-interactive scripting jobs, such as logon configuration, administrative scripting, or machine automation. Let me take you on a journey where you create mapped network drives or printers. Together we will study examples of VBScripts, object, method and properties model.
Try PowerShell for Logon Scripts
You may have noticed that VBScript is being phased out in favour of PowerShell. Although PowerShell is used primarily for interrogating the OS, it’s possible to employ cmdlets to configure the users’ environment. The best method is to create a ComObject, which acts as a wrapper for familiar VBScript commands. Here is example of PowerShell’s New-Object cmdlet manipulating MapNetworkDrive:
# PowerShell Logon 3 Script Example
Clear-Host
$Net = $(New-Object -ComObject WScript.Network)
$Net.MapNetworkDrive("X:", "\\alan\home")
$Net.objNetwork.EnumNetworkDrives
$Net
You could save the above commands into a .ps1 file. However, the tricky part is executing this .ps1 file as a logon script. I have more information here: PowerShell logon script and Group Policy.
Window 8 Group Policy Drive Maps
The modern group policy method of drive mapping does not require any scripting. In Windows Server 2008 you can launch the Group Policy Management Console and configure Drive Maps by clicking with a mouse. See more on Windows 8 Group Policy Drive Maps.
Download my Logon Script eBook for only $6.25
The extra features you get in your eBook include, more pages full of detailed examples. Also, ten ‘how to…’ sections, with screen shots showing which menus to use. Go for Guy’s eBook – and get a printable version with copy enabled and no expiry date.
If you like this page then please share it with your friends
See more logon scripts examples
• Logon Script Home •EnumNetworkDrives • Map Network Drive Group • Free Import CSV Tool
• Map Network Drive Script • Vbscript Map Network Drive Username • Map Multiple Network Drives
• ObjNetwork.MapNetworkDrive • Disconnect Network Drives • Logon script group policy