calisnowripper
MIS
I apologize in advance if my search for a solution was inadequate >.< I'm sure there are all sorts of problems with my code but it works so I've stuck with it.
Lately, however, I've been having an issue with seemingly random 32-bit Windows 7 machines. FSO.DriveExists returns "True" for drives after it maps them (so the script move son), but the drives never actually map, and aren't visible in Explorer. The script returns no errors, and works perfectly on most Windows 7 machines and 99.9% of WinXP machines.
Perhaps it has something to do with the enterprise switching to Forefront, but neither the system nor WSH report any errors. I'm stumped.
Any help would be appreciated.
Lately, however, I've been having an issue with seemingly random 32-bit Windows 7 machines. FSO.DriveExists returns "True" for drives after it maps them (so the script move son), but the drives never actually map, and aren't visible in Explorer. The script returns no errors, and works perfectly on most Windows 7 machines and 99.9% of WinXP machines.
Perhaps it has something to do with the enterprise switching to Forefront, but neither the system nor WSH report any errors. I'm stumped.
Any help would be appreciated.
Code:
'On Error Resume Next
'Start timer
startTime = Timer
Call statusMessage("Drive mapping has begun. [r16p][" & UsrName & "]")
'Creating necessary objects
Set oNetwork = Wscript.CreateObject("Wscript.Network")
Set oSysInfo = CreateObject("ADSystemInfo")
Set objFSO = CreateObject("scripting.filesystemobject")
'Unmapping any previously mapped drives
Set colDrives = oNetwork.EnumNetworkDrives
For i = 0 to colDrives.Count-1 Step 2
'[Depreciated]Wscript.Echo "3:" & colDrives.Item(i)
If colDrives.Item(i) <> "" Then
container1 = oNetwork.RemoveNetworkDrive(colDrives.Item(i), true)
End If
Next
Set colDrives = Nothing
'Semi-dynamically mapping based on group membership
UsrName = oNetwork.UserName
strPath = "LDAP://" & oSysInfo.UserName
Set oGroup = GetObject(strPath)
strNetBIOSDomain = oNetwork.UserDomain
'Preparing variable that will hold the list of groups a it is being generated
groupList = ""
'Parsing through direct memberships
For Each Prop In oGroup.MemberOf
strGroupPath = "LDAP://" & Prop
'[Depreciated]Wscript.Echo "1:" & strGroupPath
set objDomainGroup1 = GetObject(strGroupPath)
'Circular (dupe) reference-check ensures that "branches" that have already been searched aren't searched again
circCheck = howMany(objDomainGroup1.cn,grouplist)
If circCheck = 0 Then
'Add to security group list
groupList = groupList & "|" & objDomainGroup1.cn
'Call function to search for group membership of groups
Call searchUp(strGroupPath)
Else
'Call function to search for group membership of groups
Call searchUp(strGroupPath)
End If
Next
'Housekeeping
Set objDomainGroup1 = Nothing
Set oGroup = Nothing
'Strip the first empty group from list
grouplist = Mid(grouplist,2)
'Sort group list into an array
groupArray = Split(grouplist, "|", -1, 1)
'[Depreciated]Wscript.Echo "'|'-delimited list of user's memberships:" & groupList
For Each Prop2 in groupArray
'change case of groups to lower
Prop2 = LCase(Prop2)
'Fetch next available drive letter
strMap = DriveLetter()
'Call function to do actual mapping
Call MapLocation (Prop2,strMap)
Next
'Timer end,calculation, and logging
endTime = Timer
totalTime = (endTime - startTime)
Call statusMessage("Mapping script proccessed for " & totalTime & " seconds.[" & UsrName & "]")
'Housekeeping
Set oNetwork = Nothing
Set oSysInfo = Nothing
Set objFSO = Nothing
'[Depreciated]Wscript.Echo "Done! [" & totalTime & " seconds]"
'\\\\\\\\\\\\\\\\\\\\\\\\\ FUNCTION/SUB SECTION \\\\\\\\\\\\\\\\\\\\\\\\\
'map drives based on group (lower-case group names required here, but not in AD)
Function MapLocation(dmGroup,strLetter)
'Call statusMessage("Checking " & dmGroup & "...")
select case dmGroup
Case "ms imm ou managers"
Call mapCheck(dmGroup, strLetter, "\\uthsch-nas\imm")
Case "ms-mis"
Call mapCheck(dmGroup, strLetter, "\\uthsch-nas\lanmanagers")
Case "ms imm admin"
Call mapCheck(dmGroup, strLetter, "\\uthsch-nas\imm\admin")
Case "ms bme users"
Call mapCheck(dmGroup, strLetter, "\\uthsch-nas\nanomed\Nanomedicine")
Call mapCheck("BME Personal User Share", "H:", "\\uthsch-nas\nanomed\users\" & UsrName)
Case "ms imm mi all"
Call mapCheck(dmGroup, strLetter, "\\uthsch-nas\mig")
strLetter = DriveLetter() 'finds the next available letter since more than one is needed for this Case
Call mapCheck(dmGroup, strLetter, "\\uthsch-nas\ms_mig2")
'Case "ms imm mi clinical"
'Call mapCheck(dmGroup, strLetter, "\\uthsch-nas\mig")
'Case "ms imm mi staff"
'Call mapCheck(dmGroup, strLetter, "\\uthsch-nas\mig")
Case "ms imm stemcell"
Call mapCheck(dmGroup, strLetter, "\\uthsch-nas\imm\Stemcell")
Case "ms imm murad lab"
Call mapCheck(dmGroup, strLetter, "\\uthsch-nas\imm\Lab Shares\Murad Lab")
Case "ms imm drouin lab"
Call mapCheck(dmGroup, strLetter, "\\uthsch-nas\imm\Lab Shares\Drouin Lab")
Case "ms imm fornage lab"
Call mapCheck(dmGroup, strLetter, "\\uthsch-nas\imm\Lab Shares\Fornage Lab")
Case "ms imm chung lab"
Call mapCheck(dmGroup, strLetter, "\\uthsch-nas\imm\Lab Shares\Chung Lab")
Case "ms imm marian lab"
Call mapCheck(dmGroup, strLetter, "\\uthsch-nas\imm\Lab Shares\MarianLab")
Case "ms imm hamed lab"
Call mapCheck(dmGroup, strLetter, "\\uthsch-nas\imm\Lab Shares\Hamed Lab")
Case "ms imm harvey lab"
Call mapCheck(dmGroup, strLetter, "\\uthsch-nas\imm\Lab Shares\Harvey Lab")
Case "ms imm fornage lab"
Call mapCheck(dmGroup, strLetter, "\\uthsch-nas\imm\Lab Shares\Fornage Lab")
Case "ms imm bickel lab"
Call mapCheck(dmGroup, strLetter, "\\uthsch-nas\imm\Lab Shares\Bickel Lab")
Case "ms imm caskey"
Call mapCheck(dmGroup, strLetter, "\\uthsch-nas\imm\Caskey")
Case "ms imm murad admin"
Call mapCheck(dmGroup, strLetter, "\\uthsch-nas\imm\Murad")
Case "ms imm teng lab"
Call mapCheck(dmGroup, strLetter, "\\uthsch-nas\imm\Lab Shares\Teng Lab")
Case "ms imm zhang lab"
Call mapCheck(dmGroup, strLetter, "\\uthsch-nas\imm\Lab Shares\Zhang Lab")
Case "ms imm gorenstein lab"
Call mapCheck(dmGroup, strLetter, "\\uthsch-nas\imm\Lab Shares\Gorenstein Lab")
Case "ms imm liu lab"
Call mapCheck(dmGroup, strLetter, "\\uthsch-nas\imm\Lab Shares\Liu Lab")
Case "ms imm tong lab"
Call mapCheck(dmGroup, strLetter, "\\uthsch-nas\imm\Lab Shares\Tong Lab")
Case "ms imm doris lab"
Call mapCheck(dmGroup, strLetter, "\\uthsch-nas\imm\Lab Shares\Doris lab")
Case "ms imm 5th floor abi 7900"
Call mapCheck(dmGroup, strLetter, "\\uthsch-nas\imm\CommunityEquipment\SRB 504 ABI 7900")
Case "ms imm an lab"
Call mapCheck(dmGroup, strLetter, "\\uthsch-nas\imm\Lab Shares\An lab")
Case "ms imm rosenblatt lab"
Call mapCheck(dmGroup, strLetter, "\\uthsch-nas\imm\Lab Shares\Rosenblatt lab")
Case "ms imm narkar lab"
Call mapCheck(dmGroup, strLetter, "\\uthsch-nas\imm\Lab Shares\Narkar lab")
Case "ms imm martin lab"
Call mapCheck(dmGroup, strLetter, "\\uthsch-nas\imm\Lab Shares\Martin lab")
Case "ms imm bryan lab"
Call mapCheck(dmGroup, strLetter, "\\uthsch-nas\imm\Lab Shares\Bryan lab")
Case "ms imm misc faptc committee"
Call mapCheck(dmGroup, strLetter, "\\uthsch-nas\imm\Misc Shares\IMM FAPTC COMMITTEE")
Case "ms imm wenderfer lab"
Call mapCheck(dmGroup, strLetter, "\\uthsch-nas\imm\Lab Shares\Wenderfer Lab")
Case "ms imm kolonin lab"
Call mapCheck(dmGroup, strLetter, "\\uthsch-nas\imm\Lab Shares\Kolonin Lab")
Case "ms imm misc bwf student info share access"
Call mapCheck(dmGroup, strLetter, "\\uthsch-nas\imm\Misc Shares\bwf student info")
Case "ms imm all"
Call mapCheck("IMM Personal User Share", "H:", "\\uthsch-nas\imm\users\" & UsrName)
End Select
End function
'Determine the next available drive letter in reverse order
Function DriveLetter()
Set objDictionary = CreateObject("Scripting.Dictionary")
strComputer = "."
blnDone = False
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
'Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colDisks = objWMIService.ExecQuery("Select * from Win32_LogicalDisk")
For Each objDisk in colDisks
objDictionary.Add objDisk.DeviceID, objDisk.DeviceID
Next
'90 is the ASCII value of Z, so this will iterate through capital letters in reverse alphabetical order
i=90
Do
strDrive = Chr(i) & ":"
If objDictionary.Exists(strDrive) Then
i=i-1
Else
DriveLetter = strDrive
blnDone = True
End If
Loop Until blnDone = True
End Function
'Circular reference check function
Function howMany(strWhat,strWhere)
startPos = 1
numFound = 0
Do While 1
strFound = InStr(startPos, strWhere, strWhat, 1)
If strFound > 0 Then
startPos = strFound + 1
numFound = numFound + 1
Else
howMany = numFound
Exit Do
End if
Loop
End Function
'Successful mapping check function
Function mapCheck(checkGroup, checkLetter, checkShare)
count = 0
checkWait = 0
If objFSO.driveExists(checkLetter) = False Then
Do While count < 5
'[Depreciated]Wscript.Echo "For Group: " & checkGroup & ", script is mapping letter [" & checkLetter & "] to share: " & checkShare
oNetwork.MapNetworkDrive checkLetter, checkShare
count = count + 1
checkWait = checkWait + 1500 'amount of time (ms) to increase each retry wait by (1000)
Wscript.Sleep checkWait
loop1 = objFSO.driveExists(checkLetter)
'[Depreciated]Wscript.Echo "Mapping was successful (boolean):" & loop1
If loop1 = True Then
Call statusMessage("Mapping for group:" & checkGroup & " to drive letter " & checkLetter & " succeeded.[" & UsrName & "]" )
'[Depreciated]Wscript.Echo checkGroup & " " & checkLetter & " " & checkShare & " Success. " & count
Exit Do
End If
Loop
If loop1 = False Then
Call statusMessage("Mapping for group:" & checkGroup & " to drive letter " & checkLetter & " FAILED.[" & UsrName & "]" )
'[Depreciated]Wscript.Echo checkGroup & " " & checkLetter & " " & checkShare & " FAIL! " & count
End If
Else
Call statusMessage("Mapping for group:" & checkGroup & " to drive letter " & checkLetter & " FAILED because letter was previously mapped.[" & UsrName & "]" )
End If
End Function
'Event-log writing function
Function statusMessage(toWrite)
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.LogEvent 0, toWrite
Set WshShell = Nothing
End Function
'Function to enumerate indirect group memberships
Sub searchUp(ByVal objDomainGroupPassed)
set oGroup2 = GetObject(objDomainGroupPassed)
If TypeName(oGroup2.MemberOf) = "String" OR TypeName(oGroup2.MemberOf) = "Empty" Then
'[Depreciated]Wscript.Echo "4:" & oGroup2.MemberOf & "|" & TypeName(oGroup2.MemberOf)
Else
'[Depreciated]Wscript.Echo "4-SPECIAL:" & TypeName(oGroup2.MemberOf)
End If
If TypeName(oGroup2.MemberOf) = "Variant()" Then
For Each Prop In oGroup2.MemberOf
If Err.Number = 0 Then
strGroupPath2 = "LDAP://" & Prop
set objDomainGroup2 = GetObject(strGroupPath2)
'Circular (dupe) reference-check
circCheck = howMany(objDomainGroup2.cn,grouplist)
If circCheck = 0 Then
' Call sub recursively. If branch already explored, cancel this search thread.
groupList = groupList & "|" & objDomainGroup2.cn
Call searchUp(strGroupPath2)
End If
End If
Err.Clear
Next
End If
End Sub