Logon Scripts – Simple Sample Script

Logon Script

This page will get you started writing VBScripts.  I will explain how to create, then and test a simple script.  All you need is notepad.  As you work through the example, see how you manipulate the Object (network) with the Method (Echo).Sample VBScript

The purpose of this script is to echo the name ofthe User who is logged-on.  Also VBScript will display the ‘hostname’ of the Computer where the script is run.

Instructions to Create Your VBscript:

  1. Pre-requisites.  You need a domain controller for this script to work.
    If you have no domain controller try this script.
  2. Copy and paste the script below into notepad.
  3. Save the file with .vbs extension e.g. MyInfo.vbs
  4. Double click and observe the message box

‘  VBScript.
‘  Guy Thomas October 2010
‘  Purpose of script to display the UserName and ComputerName
Set WshNetwork = WScript.CreateObject("WScript.Network")
WScript.Echo "Computer Name = "& WshNetwork.ComputerName & VBCrLf _
& "User Name = " & WshNetwork.UserName

Learning Points

Note 1: VBCrLf is needed to force a carriage return in yourscript.

Note 2: Underscore(_) is necessary because my script will not fit on one line.  ( The _ is not an error on your screen!)

°

Testing:

Double click the .vbs file to test your script.  VBScript will work on Windows 2003, XP and later client machines.

Bonus Sample 2

Stage 2)  Let us ask the script to display the domain, naturally you must be logged to a domainfor this part to work!

Instructions:

  1. Pre-requisites.  You need a domain controller for this script to work.
  2. Copy and paste the script below into notepad.
  3. Save the file with .vbs extension e.g. MyDomain.vbs
  4. Double click and observe the message box
  5. Add to your first script:  & "Domain = " & WshNetwork.UserDomain
    Check the script below to see the extra:  & vbCrLF _  on the line above.

‘  VBScript.
‘  Guy Thomas October 2004
‘  Purpose of script to display the Domain name
Set WshNetwork = WScript.CreateObject("WScript.Network")
WScript.Echo "Computer Name = " & WshNetwork.ComputerName & vbCrLF _
& "User Name = " & WshNetwork.UserName & vbCrLf _
& "Domain = " & WshNetwork.UserDomain

Two ways to run your scripts.

  • Double-click the icon of the script. Useful for testing (I expect you have done this already.)
  • From the command line, type the name of the script.  This is what will happen at logon.

Assign the script toyour users

Guy Recommends: Permissions Analyzer – Free Active Directory ToolFree Permissions Analyzer for Active Directory

I like thePermissions Monitor because it enables me to see quickly WHO has permissions to do WHAT.  When you launch this tool it analyzes a users effective NTFS permissions for a specific file or folder, takes into account network share access, then displays the results in a nifty desktop dashboard!

Think of all the frustration that this free utility saves when you are troubleshooting authorization problems for users access to a resource.  Give this permissions monitor a try – it’s free!

Download Permissions Analyser – Free Active Directory Tool

Another Very Simple Script

Here is a sample script to check your computer’s version of WSH.

‘Script to display WSH version
On Error Resume Next
WScript.Echo "WSH Version: " & WScript.Version

Now let us see practical examples VBScript put to work:

 

Download my Logon Script eBook for only $6.25

Logon ScriptThe 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.

  Jumbo Script 7 Package