Wednesday, May 21, 2008

Get list of remote users network drives

Today I was in need to check the destination of a users mapped drive. Of course this is an issue that comes up a lot where I work where in a user sends an email to another user saying the file is under the S: drive. Of course the S: mapped drive can go anywhere so this is not very helpful if the person that is receiving this message doesn't have that drive letter. So this requires a little bit of leg work. You have to check the location of the Network mapped drive for that user (this is a per user setting in the registry). I wrote a script to remotely check this registry string/strings.

I can't take all the credit though. I used 2 critical functions (that really do all the real work) from scripting MVP Richard Mueller to convert the Byte Array to a Hex string and from the Hex string to a Decimal string. Very nice Richard!

I decided to write this in VBScript vs. Powershell only because it really is guaranteed to run on any 2000, XP or Vista box without installing anything. Powershell would have been much easier to write but because Powershell isn't installed by default on any MS system at the moment I chose VBscript.

   1:  Const HKEY_USERS = &H80000003
   2:  strDomain = "myDomain"
   3:  
   4:  strUserID = InputBox("Enter Valid Username using SAM Account Name."&VbCrLf&"For Example; jdelatorre","Input Username")
   5:  If IsEmpty(strUserID) Then
   6:      WScript.Quit
   7:  End If
   8:  
   9:  strComputer = InputBox("Enter Computer name to check remote registry.  The computer MUST be on.","Computer Name")
  10:  If IsEmpty(strComputer) Then
  11:      WScript.quit
  12:  End If
  13:  '...............................
  14:  'WinNT Provider
  15:  Set objUser = GetObject("WinNT://" & strDomain & "/" & strUserID & ",user")
  16:  '...............................
  17:  
  18:  arrbytSID = objUser.ObjectSid
  19:  strHexSID = OctetToHexStr(arrbytSID)
  20:  strDecSID = HexStrToDecStr(strHexSID)
  21:  strResult = "Network Drives that are connected for user: " & ucase(strUserID) & VbCrLf & "On computer: " & ucase(strComputer) & VbCrLf&VbCrLf
  22:  
  23:  Set objReg    = GetObject("winmgmts:{impersonationLevel=impersonate}//" & strComputer & "/root/default:StdRegProv")    
  24:  strKeyPath = strDecSID&"\Network"
  25:  objReg.Enumkey HKEY_USERS, strKeyPath, arrSubKeys
  26:   
  27:  For Each subkey In arrSubKeys
  28:      strKeyPath = strDecSID&"\Network\"&subkey
  29:      strValueName = "RemotePath"
  30:      objReg.GetStringValue HKEY_USERS,strKeyPath,strValueName,strValue
  31:      strResult = strResult & "DriveLetter: " & ucase(subkey) & VbCrLf
  32:      strResult = strResult & "DrivePath: " & ucase(strValue) & VbCrLf
  33:      strResult = strResult & VbCrLf
  34:  Next
  35:  
  36:  WScript.Echo strResult
  37:  
  38:  '------------------------------------------------------------------------------
  39:  ' Function to convert OctetString (byte array) to Hex string.
  40:  Function OctetToHexStr(arrbytOctet)
  41:      Dim intCounter
  42:  
  43:      OctetToHexStr = ""
  44:  
  45:      For intCounter = 1 To Lenb(arrbytOctet)
  46:          OctetToHexStr = OctetToHexStr & Right("0" & Hex(Ascb(Midb(arrbytOctet, intCounter, 1))), 2)
  47:      Next
  48:  End Function
  49:  
  50:  'Further conversion is required to get the decimal format the GUI displays.
  51:  Function HexStrToDecStr(strSid)
  52:      Dim arrbytSID, lngTemp, intCounter
  53:  
  54:      ReDim arrbytSID(Len(strSid)/2 - 1)
  55:      For intCounter = 0 To UBound(arrbytSID)
  56:          arrbytSID(intCounter) = CInt("&H" & Mid(strSid, 2*intCounter + 1, 2))
  57:      Next
  58:  
  59:      HexStrToDecStr = "S-" & arrbytSID(0) & "-" & arrbytSID(1) & "-" & arrbytSID(8)
  60:  
  61:      lngTemp = arrbytSID(15)
  62:      lngTemp = lngTemp * 256 + arrbytSID(14)
  63:      lngTemp = lngTemp * 256 + arrbytSID(13)
  64:      lngTemp = lngTemp * 256 + arrbytSID(12)
  65:  
  66:      HexStrToDecStr = HexStrToDecStr & "-" & CStr(lngTemp)
  67:  
  68:      lngTemp = arrbytSID(19)
  69:      lngTemp = lngTemp * 256 + arrbytSID(18)
  70:      lngTemp = lngTemp * 256 + arrbytSID(17)
  71:      lngTemp = lngTemp * 256 + arrbytSID(16)
  72:  
  73:      HexStrToDecStr = HexStrToDecStr & "-" & CStr(lngTemp)
  74:  
  75:      lngTemp = arrbytSID(23)
  76:      lngTemp = lngTemp * 256 + arrbytSID(22)
  77:      lngTemp = lngTemp * 256 + arrbytSID(21)
  78:      lngTemp = lngTemp * 256 + arrbytSID(20)
  79:  
  80:      HexStrToDecStr = HexStrToDecStr & "-" & CStr(lngTemp)
  81:  
  82:      lngTemp = arrbytSID(25)
  83:      lngTemp = lngTemp * 256 + arrbytSID(24)
  84:  
  85:      HexStrToDecStr = HexStrToDecStr & "-" & CStr(lngTemp)
  86:  End Function

No comments: