运行VBS脚本以提升远程计算机序列号

时间:2014-10-16 15:50:42

标签: vbscript permissions

好的,我在这里有个错误,但不确定在哪里。无论如何,我不是编码器,这是我从几个不同来源汇总的东西。这段代码有效,但它似乎只能作为普通用户运行一次,而且一旦处于提升权限状态...我只需要在提升权限下运行一次。

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

2 个答案:

答案 0 :(得分:0)

这是使用"runas"动词使用Shell.ShellExecute方法以提升的权限重新运行脚本的部分:

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)并直接转到“工人”代码。

但是,上面的代码片段在重新运行脚本后不会退出,因此提升的原始调用都在执行工作代码。

在您的代码中添加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

答案 1 :(得分: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