Tuesday, 6 October 2015

Convert SID to Username using VBScript

We can easily convert object sid value to user friendly username by using the WMI service class Win32_SID with VBScript.

1. Copy the below example vb script code and paste it in notepad or a VBScript editor.
2. Save the file with a .vbs extension, for example: ConvertSIDtoUser.vbs.
3. Double-click the vbscript file (or Run this file from command window).
4. Enter the sid value that you want resolve as user name and click Enter.
Option Explicit
Dim objWMIService,objAccount 
Dim strUserName,strSID,strDomainName,server

' Asks object sid from user.
strSID = InputBox ("Please enter user sid value")
   If strSID = "" then
      Msgbox "No sid value entered"
   end if

  server = "."
  Set objWMIService = GetObject("winmgmts:\\" & server & "\root\cimv2")
  Set objAccount = objWMIService.Get("Win32_SID.SID='" & strSID & "'")
  strUserName = objAccount.AccountName
  strDomainName = objAccount.ReferencedDomainName
  If Err.Number <> 0 Then
       Msgbox Err.Description
 Msgbox "User name: " & vbtab & UCase(strUserName) & vbcrlf & "Domain name: " & vbtab & UCase(strDomainName)
  End If


No comments:

Post a Comment