code_junkie

Vbscript detect whether UAC-elevated

Nov 11th, 2011
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. sub GetOSVersion
  2. Dim strComputer, oWMIService, colOSInfo, oOSProperty, strCaption, strOSFamily
  3. strComputer = "."
  4. Set oWMIService = GetObject("winmgmts:\" & strComputer & "rootcimv2")
  5. Set colOSInfo = oWMIService.ExecQuery("Select * from Win32_OperatingSystem")
  6. 'I hate looping through just to get one property. But dunno another way!
  7. For Each oOSProperty in colOSInfo
  8. strCaption = oOSProperty.Caption
  9. Next
  10. If InStr(1,strCaption, "Vista", vbTextCompare) Then strOSFamily = "Vista"
  11. If InStr(1,strCaption, "2008", vbTextCompare) Then strOSFamily = "2008"
  12. If InStr(1,strCaption, "XP", vbTextCompare) Then strOSFamily = "XP"
  13. If InStr(1,strCaption, "2003", vbTextCompare) Then strOSFamily = "2003"
  14. If InStr(1,strCaption, "2000", vbTextCompare) Then strOSFamily = "2000"
  15. If strOSFamily = "" Then
  16. Wscript.Echo "No known OS found. (Script can detect Windows 2000, 2003, XP, Vista, 2008.)"
  17. Else
  18. Wscript.Echo "OS Family = " & strOSFamily
  19. End If
  20. Select Case strOSFamily 'if Vista/2008 then call CheckforElevation
  21. Case "Vista"
  22. CheckforElevation
  23. Case "2008"
  24. CheckforElevation
  25. Case Else
  26. Exit Sub
  27. End Select
  28. end sub
  29.  
  30. sub CheckforElevation 'test whether user has elevated token
  31. Dim oShell, oExecWhoami, oWhoamiOutput, strWhoamiOutput, boolHasElevatedToken
  32. Set oShell = CreateObject("WScript.Shell")
  33. Set oExecWhoami = oShell.Exec("whoami /groups")
  34. Set oWhoamiOutput = oExecWhoami.StdOut
  35. strWhoamiOutput = oWhoamiOutput.ReadAll
  36. If InStr(1, strWhoamiOutput, "S-1-16-12288", vbTextCompare) Then boolHasElevatedToken = True
  37. If boolHasElevatedToken Then
  38. Wscript.Echo "Current script is running with elevated privs."
  39. Else
  40. Wscript.Echo "Current script is NOT running with elevated privs."
  41. End If
  42. end sub
Add Comment
Please, Sign In to add comment