Die Methode, auf die ich mich schließlich festgelegt habe, hängt von der Tatsache ab, dass Vista und Windows 2008 das Dienstprogramm whoami.exe enthalten und das Integritätslevel des Benutzers erkennt, der den Prozess besitzt. Ein paar Screenshots helfen hier:
WHOAMI, normal and elevated, on Vista http://lh3.ggpht.com/_Svunm47buj0/SQ6ql4iNjPI/AAAAAAAAAeA/iwbcSrAZqRg/whoami%20-%20adminuser%20-%20groups%20-%20cropped.png?imgmax=512
Sie können sehen, dass, wenn cmd erhöht läuft, whoami/groups meldet einen „High“ obligatorischen Integritätslevel und einen anderen SID als beim Laufen nicht erhöht. Im Bild ist die oberste Sitzung normal, die darunterliegende wird nach der UAC-Eingabeaufforderung erhöht angezeigt.
Wissen, das ist der Code, den ich verwendet habe. Es prüft im Wesentlichen die Betriebssystemversion, und wenn es Vista oder Server 2008 ist, ruft CheckforElevation whofami.exe/groups auf und sucht in der Ausgabe nach der Zeichenfolge S-1-16-12288. In diesem Beispiel gebe ich nur den Status zurück; Im realen Skript verzweige ich basierend auf dem Ergebnis zu verschiedenen Aktionen.
sub GetOSVersion
Dim strComputer, oWMIService, colOSInfo, oOSProperty, strCaption, strOSFamily
strComputer = "."
Set oWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colOSInfo = oWMIService.ExecQuery("Select * from Win32_OperatingSystem")
'I hate looping through just to get one property. But dunno another way!
For Each oOSProperty in colOSInfo
strCaption = oOSProperty.Caption
Next
If InStr(1,strCaption, "Vista", vbTextCompare) Then strOSFamily = "Vista"
If InStr(1,strCaption, "2008", vbTextCompare) Then strOSFamily = "2008"
If InStr(1,strCaption, "XP", vbTextCompare) Then strOSFamily = "XP"
If InStr(1,strCaption, "2003", vbTextCompare) Then strOSFamily = "2003"
If InStr(1,strCaption, "2000", vbTextCompare) Then strOSFamily = "2000"
If strOSFamily = "" Then
Wscript.Echo "No known OS found. (Script can detect Windows 2000, 2003, XP, Vista, 2008.)"
Else
Wscript.Echo "OS Family = " & strOSFamily
End If
Select Case strOSFamily 'if Vista/2008 then call CheckforElevation
Case "Vista"
CheckforElevation
Case "2008"
CheckforElevation
Case Else
Exit Sub
End Select
end sub
sub CheckforElevation 'test whether user has elevated token
Dim oShell, oExecWhoami, oWhoamiOutput, strWhoamiOutput, boolHasElevatedToken
Set oShell = CreateObject("WScript.Shell")
Set oExecWhoami = oShell.Exec("whoami /groups")
Set oWhoamiOutput = oExecWhoami.StdOut
strWhoamiOutput = oWhoamiOutput.ReadAll
If InStr(1, strWhoamiOutput, "S-1-16-12288", vbTextCompare) Then boolHasElevatedToken = True
If boolHasElevatedToken Then
Wscript.Echo "Current script is running with elevated privs."
Else
Wscript.Echo "Current script is NOT running with elevated privs."
End If
end sub
Heads up, eine viel bessere Antwort als die akzeptierte unter hinzugefügt wurde. – Tomalak