Dave's Tech Blog
Posts tagged Get Logged in User Name
VBScript – Disconnecting Network Drive and Reconnecting with User Name in the Path of the Network Drive.
11 years
by dahancock1
in VBScript
The below VBScript will get the current user that is logged into the computer, then disconnect and reconnect a failed network drive. In this case the user name is a part of the path to the user’s personal drive, \\spksan\users\davidh\ for example. It then displays a pop up stating that it’s done.
On Error Resume Next
userName = “”
strComputer = “.”
Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”)
Set colItems = objWMIService.ExecQuery(“Select * from Win32_ComputerSystem”,,48)
For Each objItem in colItems
userName = objItem.UserName
If InStr(objItem.UserName, “\”) Then
userNameArray = Split(objItem.UserName, “\”)
userNameCounter = 0
For Each userNames In userNameArray
If userNameCounter = 1 Then
userName = userNames
End If
userNameCounter = userNameCounter + 1
Next
End If
Next
Set objItem = Nothing: Set colItems = Nothing: Set objWMIService = Nothing
If Not userName = “” Then
Set objShell = CreateObject(“WScript.Shell”)
Set objNetwork = CreateObject(“WScript.Network”)
objNetwork.MapNetworkDrive “U:”, “\\spksan\users\” & userName
If Err.Number <> 0 Then
objNetwork.RemoveNetworkDrive “U:”, true
WScript.Sleep 10000
objNetwork.MapNetworkDrive “U:”, “\\spksan\users\” & userName
End If
End If
WScript.Echo “Done fixing U Drive”