我需要檢查執行該腳本的用戶是否在機器上具有管理權限。知道用戶是否具有來自VBScript的管理權限的最佳方法
我已經指定用戶執行腳本,因爲腳本可能已經使用類似於「Runas」的類似登錄以外的用戶執行。
@Javier:這兩種解決方案均可在裝有英文版Windows的PC上運行,但如果安裝的語言不同,則無法運行。這是因爲管理員組不存在,名稱與西班牙語不同。我需要解決方案在所有配置中工作。
我需要檢查執行該腳本的用戶是否在機器上具有管理權限。知道用戶是否具有來自VBScript的管理權限的最佳方法
我已經指定用戶執行腳本,因爲腳本可能已經使用類似於「Runas」的類似登錄以外的用戶執行。
@Javier:這兩種解決方案均可在裝有英文版Windows的PC上運行,但如果安裝的語言不同,則無法運行。這是因爲管理員組不存在,名稱與西班牙語不同。我需要解決方案在所有配置中工作。
,如果你想看看是否登錄的用戶可以使用腳本管理員
Set objNetwork = CreateObject("Wscript.Network")
strComputer = objNetwork.ComputerName
strUser = objNetwork.UserName
isAdministrator = false
Set objGroup = GetObject("WinNT://" & strComputer & "/Administrators")
For Each objUser in objGroup.Members
If objUser.Name = strUser Then
isAdministrator = true
End If
Next
If isAdministrator Then
Wscript.Echo strUser & " is a local administrator."
Else
Wscript.Echo strUser & " is not a local administrator."
End If
我不知道如何處理它時,腳本與「運行方式」恐怕運行。
This article有代碼如何枚舉組的成員一個很好的塊(複製在這裏爲了方便和編輯不使用電子郵件地址):
Function RetrieveUsers(domainName,grpName)
dim GrpObj
dim mbrlist
dim mbr
'-------------------------------------------------------------------------------
' *** Enumerate Group Members ***
'-------------------------------------------------------------------------------
' Build the ADSI query and retrieve the group object
Set GrpObj = GetObject("WinNT://" & domainName & "/" & grpName & ",group")
' Loop through the group membership and build a string containing the names
for each mbr in GrpObj.Members
mbrlist = mbrlist & vbTab & mbr.name & vbCrLf
Next
RetrieveUsers=mbrlist
End Function
然後,您可以編寫一個函數來看看用戶在列表...
Function IsAdmin(user)
IsAdmin = InStr(RetrieveUsers("MachineName", "Administrators"), user) > 0
End Function
...並調用它像這樣:
If IsAdmin("LocalAccount") Then
Wscript.Echo "LocalAccount is an admin"
Else
Wscript.Echo "LocalAccount is not an admin"
End If
通過這樣做,可以中斷用戶具有所需腳本權限但不屬於管理員的情況。而不是檢查組員資格,檢查你需要的特定能力。
我同意這將是一個更好的方式來實現它,但它是一個要求,用戶有管理權限來安裝軟件,所以我認爲檢查會更容易。 – 2008-11-19 16:01:50
我在公司網絡上的Windows 7上嘗試了Tim C的解決方案,我確實擁有管理員權限。但它顯示我的用戶沒有管理員權限。
相反,我使用hackier方法,因爲在cmd提示符下調用「碎片整理」需要管理員訪問權限。儘管它有效,但請注意XP和7(以及Windows的未來版本)在返回碼中有所不同。可能會有比磁盤碎片整理更一致的選擇,但它現在可行。
Function isAdmin
Dim shell
set shell = CreateObject("WScript.Shell")
isAdmin = false
errlvl = shell.Run("%comspec% /c defrag /?>nul 2>nul", 0, True)
if errlvl = 0 OR errlvl = 2 Then '0 on Win 7, 2 on XP
isAdmin = true
End If
End Function
檢查「\\ computername \ Admin $ \ system32」怎麼辦?
function IsLoggedInAsAdmin()
isAdmin = false
set shell = CreateObject("WScript.Shell")
computername = WshShell.ExpandEnvironmentStrings("%computername%")
strAdmin = "\\" & computername & "\Admin$\System32"
isAdmin = false
set fso = CreateObject("Scripting.FileSystemObject")
if fso.FolderExists(strAdmin) then
isAdmin = true
end if
IsLoggedInAsAdmin = isAdmin
end function
又一個快速的骯髒的方法。返回<> 0如果IsNotAdmin
Function IsNotAdmin()
With CreateObject("Wscript.Shell")
IsNotAdmin = .Run("%comspec% /c OPENFILES > nul", 0, True)
End With
End Function
Function isAdmin
Dim shell
Set shell = CreateObject("WScript.Shell")
isAdmin = false
errorLevel = shell.Run("%comspec% /c net session >nul 2>&1", 0, True)
if errorLevel = 0
isAdmin = true
End If
End Function
使用「本地主機」,而不是真正的主機名增加了10倍左右的腳本運行!
我最後的代碼是:如果當前用戶是本地管理員
' get_admin_status.vbs
Option Explicit
Dim oGroup: Set oGroup = GetObject("WinNT://localhost/Administrators,group")
Dim oNetwork: Set oNetwork = CreateObject("Wscript.Network")
Dim sSearchPattern: sSearchPattern = "WinNT://" & oNetwork.UserDomain & "/" & oNetwork.UserName
Dim sMember
For Each sMember In oGroup.Members
If sMember.adsPath = sSearchPattern Then
' Found...
Call WScript.Quit(0)
End If
Next
' Not found...
Call WScript.Quit(1)
此腳本返回退出代碼0。
用法:cscript.exe get_admin_status.vbs
我知道這個線程是非常古老的,並且標記爲回答,但是答案並不真正給出OP詢問的內容。
對於其他人搜索和查找此頁面,此處是基於權限而非組成員身份報告的替代方法,因此Runas Administrator將管理員權限顯示爲True。
Option Explicit
msgbox isAdmin(), vbOkonly, "Am I an admin?"
Private Function IsAdmin()
On Error Resume Next
CreateObject("WScript.Shell").RegRead("HKEY_USERS\S-1-5-19\Environment\TEMP")
if Err.number = 0 Then
IsAdmin = True
else
IsAdmin = False
end if
Err.Clear
On Error goto 0
End Function
嗨Tim C,謝謝。 我檢查了它似乎工作正常也在我的情況。我得到的用戶名不是記錄的,而是腳本正在執行的那個。 只是一個評論。這有點慢。我從HTML頁面啓動時調用這個腳本,大約需要2/3秒。 – 2008-11-19 14:30:50
如果用戶不是直接在管理員組中,而是通過某些組成員資格,則這不起作用。 – Heinzi 2009-10-21 08:43:10