Toon posts:

[VBScript] Drivemapping script, checkConnectedDrives

Pagina: 1
Acties:

Verwijderd

Topicstarter
Sinds een paar dagen ben ik bezig met een VBScript voor het aanmaken van drive mappings.
Dit script wordt aangeroepen via Group Policies in ons Active Directory (AD).

Het script kijkt in welke groepen van de AD ("Member Of...") je zit.
Op basis van de groepnaam moet er een mapping worden aangemaakt naar een opgegeven drive letter.
Vervolgens wordt er gekeken of de te gebruiken drive letter niet in gebruik is.
Is dat wel het geval? Dan wordt er een error gestuurd via de mail.
Als de drive letter niet bestaat wordt de mapping aangemaakt.

Dit werkt allemaal ok. Maar nu is het zo dat wanneer ik 2x dezelfde drive letter aan verschillende groepen wil koppelen er iets fout gaat.
Voorbeeld:
Groep "Administrators" moet een mapping M: -> \\server01\share1 hebben
Groep "Share Admins" moet een mapping M: -> \\server01\share2 hebben

Stel dat user1 toegevoegd is aan beide groepen ("Administrators" en "Share Admins"):
Bij de eerste mapping gaat alles goed... de drive bestaat niet en de mapping wordt gemaakt.
Maar bij de 2e mapping gaat het fout. Het script ziet niet dat er net een M: mapping is aangemaakt en ik krijg een VBScript error (ook als ik een timeout van 10 sec. tussen de mappings zet).

Zien jullie waar het fout gaat?

Hier het script:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
Option Explicit ' Force explicit declarations
'
' Variables
'
Dim WSHNetwork
Dim FSO
Dim strUserName ' Current user
Dim strUserDomain ' Current User's domain name
Dim strComputerName ' Current computer name
Dim ObjGroupDict ' Dictionary of groups to which the user belongs
Dim objEmail 'E-mail object
Dim strDriveLetter
Dim CheckDrive, intDrive, AlreadyConnected

Set WSHNetwork = WScript.CreateObject("WScript.Network")
Set FSO = CreateObject("Scripting.FileSystemObject")
Set CheckDrive = WSHNetwork.EnumNetworkDrives()
Set objEmail = CreateObject("CDO.Message")
'
' Wait until the user is really logged in...
'
strUserName = ""
While strUserName = ""
WScript.Sleep 100 ' 1/10 th of a second
strUserName = WSHNetwork.UserName
Wend
strUserDomain = WSHNetwork.UserDomain
strComputerName = WshNetwork.ComputerName

' Read the user's account "Member Of" tab info across the network
' once into a dictionary object. 

Set ObjGroupDict = CreateMemberOfObject(strUserDomain, strUserName)
'======================== COPY ==========================
If MemberOf(ObjGroupDict, "Administrators") Then

'Check if the drive is already mapped
AlreadyConnected = checkDriveAvailabilty(CheckDrive,"M:")
If AlreadyConnected = True then
   'Drive is aleady connected, sending error mail to system administrator
   call SendErrorEmail(strUserName,strComputerName,"M:")
Else
   'Map network Drives here
   WSHNetwork.MapNetworkDrive "M:", "\\server01\share1"
   WScript.Sleep 10000 ' 10 seconds
End If

End If
'======================= PASTE ============================

'======================== COPY ==========================
If MemberOf(ObjGroupDict, "Share Admins") Then

'Check is the drive is already mapped
AlreadyConnected = checkDriveAvailabilty(CheckDrive,"M:")
If AlreadyConnected = True then
   'Drive is aleady connected, sending error mail to system administrator
   call SendErrorEmail(strUserName,strComputerName,"M:")
Else
   'Map network Drives here
   WSHNetwork.MapNetworkDrive "M:", "\\server01\share2"
End If

End If
'======================= PASTE ============================


Function checkDriveAvailabilty(CheckDrive, strDriveLetter)
'
' Check is the given drivelettter is not already mapped
'
On Error Resume Next
'Set initial value to .False.
checkDriveAvailabilty = False

For intDrive = 0 To CheckDrive.Count - 1 Step 2
  If CheckDrive.Item(intDrive) = strDriveLetter _
  Then checkDriveAvailabilty = True
Next

End Function

Function MemberOf(ObjDict, strKey)
' Given a Dictionary object containing groups to which the user
' is a member of and a group name, then returns True if the group
' is in the Dictionary else return False. 
'
' Inputs:
' strDict - Input, Name of a Dictionary object
' strKey - Input, Value being searched for in
' the Dictionary object
' Sample Usage:
'
' If MemberOf(ObjGroupDict, "DOMAIN ADMINS") Then
' wscript.echo "Is a member of Domain Admins."
' End If
'
'
MemberOf = CBool(ObjGroupDict.Exists(strKey))

End Function


Function CreateMemberOfObject(strDomain, strUserName)
' Given a domain name and username, returns a Dictionary
' object of groups to which the user is a member of.
'
' Inputs:
'
' strDomain - Input, NT Domain name
' strUserName - Input, NT username
'
Dim objUser, objGroup

Set CreateMemberOfObject = CreateObject("Scripting.Dictionary")
CreateMemberOfObject.CompareMode = vbTextCompare
Set objUser = GetObject("WinNT://" _
& strDomain & "/" _
& strUserName & ",user")
For Each objGroup In objUser.Groups
CreateMemberOfObject.Add objGroup.Name, "-"
Next
Set objUser = Nothing

End Function


Sub SendErrorEmail(strUserName,strComputerName,DriveLetter)
objEmail.From = "administrator@domain.com"
objEmail.To = "administrator@domain.com"
objEmail.Subject = "Drivemapping error: " & DriveLetter & " in use"
'objEmail.Textbody = "User: " & strUserName & VBCrLf & "Computername: " & strComputerName & VBCrLf & "Driveletter " & DriveLetter & " is in use." & VBCrLf & VBCrLf & "Drive is not mapped"
objEmail.HTMLbody = "<b>User:</b> " & strUserName & "<br><b>Computername:</b>" & strComputerName & "<br><b><i>Driveletter " & DriveLetter & " is in use.</b></i><p>Drive is not mapped"
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail.domain.com"
objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
'objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoBasic
'objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "YourLogin@YourDomain.com"
'objMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "Password"
objEmail.Configuration.Fields.Update
objEmail.Send
End Sub

  • PhysicsRules
  • Registratie: Februari 2002
  • Laatst online: 31-03 07:26

PhysicsRules

Dux: Linux voor Eenden

Je haalt éénmalig een lijst op met beschikbare drives. Die lijst refresht blijkbaar niet. Je zult die lijst dus elke keer opnieuw moeten ophalen als je een mapping gemaakt hebt of als je de controle doet.

Verwijderd

Topicstarter
Bedankt PhysicsRules,

De refresh van de lijst is het inderdaad.
Heb nu de volgende regel in de functie checkDriveAvailabilty(strDriveLetter) gestopt
code:
1
Set CheckDrive = WSHNetwork.EnumNetworkDrives()


Nu wordt de lijst met drives elke keer bij het aanroepen van de functie vernieuwd.

Nu werkt het na behoren! d:)b