2014-10-16 20 views
0

好的,我在這裏有一個錯誤,但不知道在哪裏。我不是以任何方式編碼,這是我從幾個不同的來源彙集而成的。此代碼有效,但它似乎只能以普通用戶身份運行一次,並且只能在提升的權限下運行一次......我只需要它在運行時提升權限即可運行一次。運行VBS腳本提升到遠程計算機的序列號

Set WshShell = WScript.CreateObject("WScript.Shell") 
If WScript.Arguments.length = 0 Then 
Set ObjShell = CreateObject("Shell.Application") 
ObjShell.ShellExecute "wscript.exe", """" & _ 
WScript.ScriptFullName & """" &_ 
" RunAsAdministrator", , "runas", 1 
End if 
On Error Resume Next 
Dim System 
if Wscript.Arguments.Count >0 then 
sSystem=Wscript.Arguments(0) 
end if 
ComputerName = InputBox("Enter the name of the computer you wish to query") 
winmgmt1 = "winmgmts:{impersonationLevel=impersonate}!//"& ComputerName &"" 
Set SNSet = GetObject(winmgmt1).InstancesOf ("Win32_BIOS") 
for each SN in SNSet 
MsgBox "The serial number for the specified computer is: " & SN.SerialNumber 
next 

回答

0

這是部分通過Shell.ShellExecute方法與"runas"動詞重新運行具有提升的權限腳本:

If WScript.Arguments.length = 0 Then 
Set ObjShell = CreateObject("Shell.Application") 
ObjShell.ShellExecute "wscript.exe", """" & _ 
WScript.ScriptFullName & """" &_ 
" RunAsAdministrator", , "runas", 1 
End if 

重新運行帶有附加參數RunAsAdministrator腳本可確保重新運行腳本會跳過上面的部分(因爲WScript.Arguments.Length由於該參數而大於0)並直接轉到「worker」代碼。

但是,重新運行該腳本後,上述代碼片段不會退出,因此原始調用中升高的正在執行工作代碼。

添加WScript.Quit聲明,您的代碼,使原來調用退出之後重新運行本身有提升的權限和問題將消失:

If WScript.Arguments.Length = 0 Then 
    Set ObjShell = CreateObject("Shell.Application") 
    ObjShell.ShellExecute "wscript.exe", _ 
    """" & WScript.ScriptFullName & """ RunAsAdministrator", , "runas", 1 
    WScript.Quit 0 
End If
0

這一切(用於遠程計算機):

ComputerName = InputBox("Enter the name of the computer you wish to query") 
winmgmt1 = "winmgmts:(impersonationLevel=impersonate}!//"& ComputerName &"\root\cimv2") 
Set SNSet = winmgmt1.ExecQuery("Select * from Win32_BIOS") 
for each SN in SNSet 
MsgBox "The serial number for the specified computer is: " & SN.SerialNumber 
next 
相關問題