Wednesday, January 20, 2010

VbScript - Find mapped drives on remote PC

ignore the naming convention.... im not too good at naming vars and things..

hope it helps.... did this to help out the helpdesk guys....

Note: pls leave a comment, would like to get feedback.

'This script will give you the mapped drives on a remote machine for the specified user
' It requires the pc name or ip address
' It requires the username of the user whose drive mapping you want to find out on the remote machine
' The script gets the logonsession id and username in the first part of the script and then passes it ot Mappedlogicaldisk to get the drive mappings.
' This script can be refined further to only report on network drive type. however, i cant be stuffed.
'Auth: Rahul & Help from Chris (he wants me to do documentation and indenting... i dont like him


Option explicit

Dim strComputer
Dim strusername
Dim objWMIService
Dim colLogonSession
Dim namevar1, name1, name2, namestr3, nametemp1
Dim logonidvar1, logonid1, logonid2, logonidstr3, logonidtemp1
Dim objSession, colDrives, objDrive

Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" &_
strComputer & "\root\cimv2")
' Getting the username and pc name
strcomputer = InputBox("Enter the PC Name")
strusername = InputBox("Enter the username")
'Find out the username and sessionid/logonid
Set colLogonSession = objWMIService.ExecQuery _
("Select * From Win32_LoggedOnUser")
'Working on the string values returned to get the username and logonid
For Each objSession in colLogonSession
namevar1 = objSession.antecedent
logonidvar1 = objSession.dependent
Name1 = (instr(1,Namevar1,"Name=""",1) + 6)
Logonid1 = (instr(1,logonidvar1,"Logonid=""",1) + 9)
name2 = len(namevar1)
Logonid2 = len(Logonidvar1)
nametemp1 = name2 - name1
Logonidtemp1 = Logonid2 - Logonid1
Namestr3 = Mid(namevar1,name1,nametemp1)
logonidstr3 = Mid(logonidvar1,logonid1,logonidtemp1)
'wscript.echo namestr3
'wscript.echo logonidstr3

'Matching username and passing it on to Mappedlogicaldisk to get the network drive mappings.
IF lcase(namestr3) = lcase(strusername) THEN 'it is case sensitive
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" &_
strComputer & "\root\cimv2")
Set colDrives = objWMIService.ExecQuery _
("Select * From Win32_MappedLogicalDisk Where Sessionid = '" & Logonidstr3 & "'")
For Each objDrive in colDrives

Wscript.Echo "Drive letter: " & objDrive.DeviceID
Wscript.Echo "Network path: " & objDrive.ProviderName

Next

END IF
Next


Hope it helps.... i did a bit of searching on google before i wrote it, didnt get much luck with googling....

Wednesday, January 6, 2010

Tech Corner

Trying to get my hands onto learning vb scripting. Seems the tech world is pushing powershell.
Lets see how I go.

I will try and share my work experiences and things i learn, so keep visiting.

Cheers

Rahul