Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for sid2user.vbs

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

  1. Option Explicit
  2.  
  3. Dim arrTest, blnValidSID, i, objAccount, objWMIService, strMsg, strSID
  4.  
  5. blnValidSID = True
  6.  
  7. With WScript.Arguments
  8. 	If .Named.Count    > 0 Then Syntax
  9. 	If .Unnamed.Count <> 1 Then Syntax
  10. 	strSID = .Unnamed(0)
  11. 	arrTest = Split( strSID, "-" )
  12. 	If UBound( arrTest ) <> 3 And UBound( arrTest ) <> 7 Then blnValidSID = False
  13. 	If UCase( arrTest(0) ) <> "S" Then blnValidSID = False
  14. 	For i = 1 To UBound( arrTest )
  15. 		If Not IsNumeric( arrTest(i) ) Then blnValidSID = False
  16. 		If Not CStr( CLng( arrTest(i) ) ) = arrTest(i) Then blnValidSID = False
  17. 	Next
  18. End With
  19.  
  20. If blnValidSID = False Then
  21. 	strMsg = "Error: invalid SID" & vbCrLf
  22. 	Syntax
  23. End If
  24.  
  25. Set objWMIService = GetObject( "winmgmts://./root/cimv2" )
  26. On Error Resume Next
  27. Set objAccount = objWMIService.Get( "Win32_SID.SID='" & strSID & "'" )
  28. If Err Then Syntax
  29. If objAccount.AccountName = "" Then
  30. 	WScript.Echo "-unknown-"
  31. 	WScript.Quit 1
  32. End If
  33. On Error Goto 0
  34. WScript.Echo objAccount.ReferencedDomainName & "\" & objAccount.AccountName
  35. Set objAccount = Nothing
  36. Set objWMIService = Nothing
  37.  
  38.  
  39. Sub Syntax
  40. 	strMsg = strMsg & vbCrLf _
  41. 	       & "SID2User.vbs,  Version 1.00" _
  42. 	       & vbCrLf _
  43. 	       & "Display the user ID for the specified SID" _
  44. 	       & vbCrLf & vbCrLf _
  45. 	       & "Usage:  CSCRIPT.EXE  //NoLogo  SID2USER.VBS  sid" _
  46. 	       & vbCrLf & vbCrLf _
  47. 	       & "Notes:  The user ID is returned in DOMAIN\UserID format." _
  48. 	       & vbCrLf _
  49. 	       & "        If the SID is not found, the script returns ""-unknown-""." _
  50. 	       & vbCrLf _
  51. 	       & "        If the SID is invalid, an error message and this screen are displayed." _
  52. 	       & vbCrLf & vbCrLf _
  53. 	       & "Core functionality borrowed from the Microsoft Scripting Guys" _
  54. 	       & vbCrLf _
  55. 	       & "www.microsoft.com/technet/scriptcenter/resources/qanda/dec04/hey1203.mspx" _
  56. 	       & vbCrLf _
  57. 	       & "Command line parsing/error handling by Rob van der Woude" _
  58. 	       & vbCrLf _
  59. 	       & "http://www.robvanderwoude.com"
  60. 	WScript.Echo strMsg
  61. 	WScript.Quit 1
  62. End Sub
  63.  

page last modified: 2024-04-16; loaded in 0.0082 seconds