Tutorial netlogon script for window client “netlogon .bat and .vbs”

Window Bat file

@echo off

REM removes any network drives in use by user
@if exist h:  use h: /delete /yes
@if exist g:  use g: /delete /yes
@if exist p:  use p: /delete /yes
@if exist u:  use u: /delete /yes
#repeat this fo any other mapped drives @if exist p: net use p: /delete /yes

REM Maps shares as network drives
start  %LOGONSERVER%\netlogon\logon.vbs

REM WPKG installer
cscript \\pdc\wpkg\wpkg.js /synchronize /quiet

REM Syncronize the time on the workstation to that of the server.
net time %LOGONSERVER% /SET /Y
 

VBscript

First make sure all variables are dimensioned.
This isn’t necessary for functionality; it’s for coding discipline only.


Option Explicit

'dimension all our variables
dim objNetwork
dim strDriveLetter, strRemotePath, strUser, strGrp
dim strGroupADSPath, strUserADSPath, grp
dim objShell, grouplistD, ADSPath, userPath, listGroup
dim objFileSys,  objExecObject
dim arrShares
dim strShare,  strCommandText, strResults
dim arrPrivaryDrives(4)

Const SERVERPATH = "\\devadom\"

On Error Resume Next

'This script will use the MapNetworkDrive method
'for each network drive mapped.

'We'll be using the Wscript.Network Object to enumerate the user as well as to map drives.
'We only need to instantiate it once at the beginning.
Set objNetwork = Wscript.CreateObject("Wscript.Network")

'First let's get the user name since we'll use it for mapping the home directory
'as well as checking group memberships.
strUser = objNetwork.UserName

'In just about every network at least two drives are mapped:
'One for the user's home directory, and one for an organizational public share.
'Set array Primary Drives
arrPrivaryDrives(0) = strUser
arrPrivaryDrives(1) = "public"
arrPrivaryDrives(2) = "transit"

' *****************************************************
'We'll map those first since they don't depend on group memberships.
Function mapPrimaryNetworkDrive()
'PublicShare Drive to P:
strDriveLetter = "P:"
strRemotePath = SERVERPATH & "public"
objNetwork.MapNetworkDrive strDriveLetter, strRemotePath

strDriveLetter = "T:"
strRemotePath = SERVERPATH & "transit"
objNetwork.MapNetworkDrive strDriveLetter, strRemotePath

end Function
' *****************************************************

' *****************************************************
' This function is listing all network drives
Function getNetworkDriveList ()
Set objNetwork = Wscript.CreateObject("Wscript.Network")
Set objFileSys = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Wscript.Shell")

strCommandText = "net view " & SERVERPATH
strResults=""

Set objExecObject = objShell.Exec(strCommandText)
strCommandText =""

' Loop the object status
Do
WScript.Sleep 60
Loop Until objExecObject.Status <> 0

' Read all object
strResults = objExecObject.StdOut.ReadAll()
arrShares = fetchShareNetworkDrive(strResults)

For Each strShare In arrShares
Call mapNetworkDrive(strShare)
Next

wscript.quit
End Function
' *****************************************************

' *****************************************************
' This function is mapping the network drives
' Parameter string strShare
Function mapNetworkDrive(strShare)

'Find next available drive letter
'TO-DO What if all drive letters are already taken? Add guard to prevent further mapping

strDriveLetter = Asc("c")    'Convert CHAR C to ASCII code
While objFileSys.DriveExists(Chr(strDriveLetter)+":")    'Increase ASCII code index by 1 until free letter is found
strDriveLetter = strDriveLetter+1
Wend

strDriveLetter = Chr(strDriveLetter) + ":"    'Convert ASCII code back to character and concatenate : to it
'Wscript.Echo "The next available drive letter is "+strShare +" :" + strDriveLetter
strRemotePath = SERVERPATH&CStr(strShare)
strRemotePath = CStr(strRemotePath)

objNetwork.MapNetworkDrive strDriveLetter, strRemotePath    'map the drive

End Function
' *****************************************************

' *****************************************************
' This function is feching the shared network drives
' Parameter string
' Return method

Function fetchShareNetworkDrive(str)
'set variables
Dim i    'current position in string
Dim asciiChar    'current string character converted to ascii
Dim startFound, parse 'bool
Dim strShare    'share name
Dim arrayPosition    'position in array of shares
Dim Shares()    'shares array

'init default values
arrayPosition = 0
parse = False
startFound = False

For i = 1 To Len(str)

If (startFound=False) Then
asciiChar = CStr(Asc(Mid(str, i, 1)))+CStr(Asc(Mid(str, i+1, 1)))+CStr(Asc(Mid(str, i+2, 1)))
Else
asciiChar = CStr(Asc(Mid(str, i, 1)))
End If

If (asciiChar = "451310") Then 'match char '-' Line Feed and Carriage Return -> at this stage next char will be first letter of our share name
parse = True    ' allow parsing
startFound = True    'start of share list found set flag to true
i=i+3    'increase i by 3 to skip - LF CR
ElseIf ((asciiChar = "10") And (startFound)) Then    'if current character in Line Feed and we already found start -> start parsing
parse = True
i=i+1
End If

If ((parse)) Then
While((asciiChar <> "32"))    'While character is not ' ' parse chars to out strShare buffer
If(i=Len(str)) Then    'safeguard if we reach end of file
asciiChar="32"
Else
asciiChar = CStr(Asc(Mid(str, i, 1)))
If (asciiChar <> "32") Then 'If character is not whitespace add it
strShare=strShare+ Mid(str, i, 1)
End if
i = i+1
End if
Wend
ReDim Preserve Shares(arrayPosition) 'increase array size
Shares(arrayPosition)= strShare    'add new share to array
arrayPosition = arrayPosition+1
parse=False
End If
strShare =""
Next

ReDim Preserve Shares(arrayPosition-3)    'remove 3 last items of array -> it is rubbish I had no time to tune parser, will do later, works fine anyways
fetchShareNetworkDrive = Shares
End Function
' *****************************************************

' *****************************************************
'This function returns a particular environment variable's value.
' for example, if you use EnvString("username"), it would return
' the value of %username%.
Function EnvString(variable)
variable = "%" & variable & "%"
EnvString = objShell.ExpandEnvironmentStrings(variable)
End Function
' *****************************************************

' *****************************************************
' returns the index of obj in array. obj can be anything.
' Returns false if found.
Function IsNotInArray(strIn, arrCheck)
'IsInArray: Checks for a value inside an array
Dim bFlag
bFlag = True
If IsArray(arrCheck) AND Not IsNull(strIn) Then
Dim i
For i = 0 to UBound(arrCheck)
If LCase(arrcheck(i)) = LCase(strIn) Then
bFlag = False
Exit For
End If
Next
End If
IsNotInArray = bFlag
End Function
' *****************************************************

' Clean up
Set objShell = Nothing

'**********************************
' Main Program              '
'**********************************
mapPrimaryNetworkDrive()
getNetworkDriveList()