Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for getsid.vbs

(view source code of getsid.vbs as plain text)

  1. ' Use custom error handling
  2. On Error Resume Next
  3.  
  4.  
  5. ' Define constants and initialize variables
  6. Const wbemFlagReturnImmediately = &h10
  7. Const wbemFlagForwardOnly       = &h20
  8. Const ForReading                = 1
  9. Const ForWriting                = 2
  10. Const ForAppending              = 8
  11. strComputer = "."
  12. strUserName = ""
  13. strMsg      = vbCrLf
  14.  
  15. ' Check "named" command line arguments (command line switches)
  16. If WScript.Arguments.Named.Count > 0 Then
  17. 	Syntax( )
  18. End If
  19.  
  20.  
  21. ' Check "unnamed" command line arguments
  22. If WScript.Arguments.Unnamed.Count > 2 Then
  23. 	Syntax( )
  24. Else
  25. 	For numArg = 0 To WScript.Arguments.Unnamed.Count - 1 Step 1
  26. 		If Left( Wscript.Arguments.Unnamed( numArg ), 2 ) = "\\" Then
  27. 			strComputer = Mid( Wscript.Arguments.Unnamed( numArg ), 3 )
  28. 		Else
  29. 			strUserName = Wscript.Arguments.Unnamed( numArg )
  30. 		End If
  31. 	Next
  32. End If
  33. If WScript.Arguments.Unnamed.Count = 2 Then
  34. 	If strComputer = "." Then
  35. 		Syntax( )
  36. 	End If
  37. End If
  38.  
  39.  
  40. ' Abort if no user was specified for a remote computer
  41. If Not strComputer = "." Then
  42. 	If strUserName = "" Then
  43. 		Syntax( )
  44. 	End If
  45. End If
  46.  
  47.  
  48. ' Fill in logged on user's name if omitted
  49. If strUserName = "" Then
  50. 	Set WshShell  = WScript.CreateObject( "WScript.Shell" )
  51. 	If Err.Number Then ShowError( )
  52. 	Set WshSysEnv = WshShell.Environment( "PROCESS" )
  53. 	If Err.Number Then ShowError( )
  54. 	strUserName   = WshSysEnv( "USERNAME" )
  55. End If
  56.  
  57.  
  58. Set objWMIService = GetObject( "winmgmts://" & strComputer & "/root/CIMV2" )
  59. Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_UserAccount", _
  60.                "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
  61. If Err.Number Then ShowError( )
  62.  
  63. For Each objItem In colItems
  64. 	If LCase( objItem.Name ) = LCase( strUserName ) Then
  65. 		strMsg = strMsg _
  66. 		       & "Name : " & objItem.Name & vbCrLf _
  67. 		       & "SID  : " & objItem.SID
  68. 	End If
  69. Next
  70.  
  71. ' Display the result
  72. WScript.Echo strMsg
  73.  
  74.  
  75. Sub ShowError( )
  76. 	strMsg = vbCrLf & "Error # " & Err.Number & vbCrLf _
  77. 	       & Err.Description & vbCrLf & vbCrLf
  78. 	Syntax( )
  79. End Sub
  80.  
  81.  
  82. Sub Syntax( )
  83. 	strMsg = strMsg & vbCrLf _
  84. 	       & "GetSID.vbs,  Version 1.00" & vbCrLf _
  85. 	       & "Retrieve Security ID for the specified user on any computer." & vbCrLf & vbCrLf _
  86. 	       & "Usage:  [ CSCRIPT ]  GETSID.VBS  [ username [ \\computername ] ]" & vbCrLf & vbCrLf _
  87. 	       & "Where:  " & Chr(34) & "username" & Chr(34) & "      is the optional user name (mandatory if computername" & vbCrLf _
  88. 	       & "                        is specified; default is the logged on user name)" & vbCrLf _
  89. 	       & "        " & Chr(34) & "computername" & Chr(34) & "  is the optional name of a remote computer (default" & vbCrLf _
  90. 	       & "                        is the local computer name)" & vbCrLf _
  91. 	       & vbCrLf _
  92. 	       & "Written by Rob van der Woude" & vbCrLf _
  93. 	       & "http://www.robvanderwoude.com" & vbCrLf & vbCrLf _
  94. 	       & "Created with Microsoft's Scriptomatic 2.0 tool" & vbCrLf _
  95. 	       & "http://www.microsoft.com/downloads/details.aspx?" & vbCrLf _
  96. 	       & "    FamilyID=09dfc342-648b-4119-b7eb-783b0f7d1178&DisplayLang=en"
  97. 	WScript.Echo strMsg
  98. 	WScript.Quit(1)
  99. End Sub
  100.  

page last uploaded: 2017-08-21, 14:26