Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for user2sid.vbs

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

  1. Option Explicit
  2.  
  3. Dim objAccount, objWMIService, wshNetwork
  4. Dim strComputer, strDomain, strSID, strUser
  5.  
  6. Set wshNetwork = WScript.CreateObject( "WScript.Network" )
  7. strComputer    = wshNetwork.ComputerName
  8. Set wshNetwork = Nothing
  9.  
  10. With WScript.Arguments
  11. 	If .Named.Count > 0 Then Syntax
  12. 	Select Case .Unnamed.Count
  13. 		Case 1
  14. 			strUser = .Unnamed(0)
  15. 			If InStr( strUser, "\" ) Then
  16. 				strDomain = Left( strUser, InStr( strUser, "\" ) - 1 )
  17. 				strUser   = Mid( strUser, InStr( strUser, "\" ) + 1 )
  18. 			Else
  19. 				strDomain = strComputer
  20. 				strUser   = .Unnamed(0)
  21. 			End If
  22. 		Case 2
  23. 			strDomain = .Unnamed(1)
  24. 			strUser   = .Unnamed(0)
  25. 		Case Else
  26. 			Syntax
  27. 	End Select
  28. End With
  29.  
  30. If InStr( strComputer, " " ) Then Syntax
  31. If InStr( strDomain,   " " ) Then Syntax
  32. If InStr( strUser,     " " ) Then Syntax
  33. If InStr( strUser,     "\" ) Then Syntax
  34.  
  35. Set objWMIService = GetObject( "winmgmts://" & strComputer & "/root/cimv2" )
  36. On Error Resume Next
  37. Set objAccount    = objWMIService.Get( "Win32_UserAccount.Name='" & strUser & "',Domain='" & strDomain & "'" )
  38. If Err Then Syntax
  39. On Error Goto 0
  40. Wscript.Echo objAccount.SID
  41. Set objAccount    = Nothing
  42. Set objWMIService = Nothing
  43.  
  44.  
  45. Sub Syntax
  46. 	Dim strMsg
  47. 	strMsg = vbCrLf _
  48. 	       & "User2SID.vbs,  Version 1.00" _
  49. 	       & vbCrLf _
  50. 	       & "Display the Security ID (SID) for the specified user" _
  51. 	       & vbCrLf & vbCrLf _
  52. 	       & "Usage:  CSCRIPT.EXE  //NoLogo  USER2SID.VBS  user_id  [ domain ]" _
  53. 	       & vbCrLf _
  54. 	       & "   or:  CSCRIPT.EXE  //NoLogo  USER2SID.VBS  user_id  [ computer ]" _
  55. 	       & vbCrLf _
  56. 	       & "   or:  CSCRIPT.EXE  //NoLogo  USER2SID.VBS  domain\user_id" _
  57. 	       & vbCrLf _
  58. 	       & "   or:  CSCRIPT.EXE  //NoLogo  USER2SID.VBS  computer\user_id" _
  59. 	       & vbCrLf & vbCrLf _
  60. 	       & "Notes:  If no domain or computer is specified, the local computer is assumed." _
  61. 	       & vbCrLf _
  62. 	       & "        Do not use double backslashes to prefix computer names." _
  63. 	       & vbCrLf _
  64. 	       & "        No spaces are allowed in user, computer or domain names." _
  65. 	       & vbCrLf & vbCrLf _
  66. 	       & "Core functionality borrowed from the Microsoft Scripting Guys" _
  67. 	       & vbCrLf _
  68. 	       & "www.microsoft.com/technet/scriptcenter/resources/qanda/dec04/hey1203.mspx" _
  69. 	       & vbCrLf _
  70. 	       & "Command line parsing by Rob van der Woude" _
  71. 	       & vbCrLf _
  72. 	       & "http://www.robvanderwoude.com"
  73. 	WScript.Echo strMsg
  74. 	WScript.Quit 1
  75. End Sub
  76.  

page last uploaded: 2017-07-06, 12:37