' Use custom error handling On Error Resume Next ' Define constants and initialize variables Const wbemFlagReturnImmediately = &h10 Const wbemFlagForwardOnly = &h20 Const ForReading = 1 Const ForWriting = 2 Const ForAppending = 8 strComputer = "." strUserName = "" strMsg = vbCrLf ' Check "named" command line arguments (command line switches) If WScript.Arguments.Named.Count > 0 Then Syntax( ) End If ' Check "unnamed" command line arguments If WScript.Arguments.Unnamed.Count > 2 Then Syntax( ) Else For numArg = 0 To WScript.Arguments.Unnamed.Count - 1 Step 1 If Left( Wscript.Arguments.Unnamed( numArg ), 2 ) = "\\" Then strComputer = Mid( Wscript.Arguments.Unnamed( numArg ), 3 ) Else strUserName = Wscript.Arguments.Unnamed( numArg ) End If Next End If If WScript.Arguments.Unnamed.Count = 2 Then If strComputer = "." Then Syntax( ) End If End If ' Abort if no user was specified for a remote computer If Not strComputer = "." Then If strUserName = "" Then Syntax( ) End If End If ' Fill in logged on user's name if omitted If strUserName = "" Then Set WshShell = WScript.CreateObject( "WScript.Shell" ) If Err.Number Then ShowError( ) Set WshSysEnv = WshShell.Environment( "PROCESS" ) If Err.Number Then ShowError( ) strUserName = WshSysEnv( "USERNAME" ) End If Set objWMIService = GetObject( "winmgmts://" & strComputer & "/root/CIMV2" ) Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_UserAccount", _ "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly) If Err.Number Then ShowError( ) For Each objItem In colItems If LCase( objItem.Name ) = LCase( strUserName ) Then strMsg = strMsg _ & "Name : " & objItem.Name & vbCrLf _ & "SID : " & objItem.SID End If Next ' Display the result WScript.Echo strMsg Sub ShowError( ) strMsg = vbCrLf & "Error # " & Err.Number & vbCrLf _ & Err.Description & vbCrLf & vbCrLf Syntax( ) End Sub Sub Syntax( ) strMsg = strMsg & vbCrLf _ & "GetSID.vbs, Version 1.00" & vbCrLf _ & "Retrieve Security ID for the specified user on any computer." & vbCrLf & vbCrLf _ & "Usage: [ CSCRIPT ] GETSID.VBS [ username [ \\computername ] ]" & vbCrLf & vbCrLf _ & "Where: " & Chr(34) & "username" & Chr(34) & " is the optional user name (mandatory if computername" & vbCrLf _ & " is specified; default is the logged on user name)" & vbCrLf _ & " " & Chr(34) & "computername" & Chr(34) & " is the optional name of a remote computer (default" & vbCrLf _ & " is the local computer name)" & vbCrLf _ & vbCrLf _ & "Written by Rob van der Woude" & vbCrLf _ & "http://www.robvanderwoude.com" & vbCrLf & vbCrLf _ & "Created with Microsoft's Scriptomatic 2.0 tool" & vbCrLf _ & "http://www.microsoft.com/downloads/details.aspx?" & vbCrLf _ & " FamilyID=09dfc342-648b-4119-b7eb-783b0f7d1178&DisplayLang=en" WScript.Echo strMsg WScript.Quit(1) End Sub