我正在尝试学习如何在提升模式下运行 vbscript。如果我尝试运行提升的 vbscript 有参数,我就无法完全让它工作。
这是一个简单的例子:
操作系统版本.vbs
' Return a string indicating the operating system
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
("Select * from Win32_OperatingSystem")
'Wscript.echo Wscript.Arguments(0)
For Each objOperatingSystem in colOperatingSystems
Wscript.StdOut.Write objOperatingSystem.Caption
Wscript.StdOut.WriteBlankLines(1)
Wscript.StdOut.Write "Version " & objOperatingSystem.Version
Next
Run Code Online (Sandbox Code Playgroud)
运行Elevated.vbs
' Run the script in elevated mode
'
' This will be needed to install programs into Program Files
prgName = Wscript.Arguments.Item(0)
prgArgs = ""
If Wscript.Arguments.Count > 1 Then
For i = 1 To Wscript.Arguments.Count - 1
prgArgs = prgArgs & " " & Wscript.Arguments.Item(i)
Next
End If
Wscript.echo "Running: " & prgName & prgArgs
Set objShell = CreateObject("Shell.Application")
Set fso = CreateObject("Scripting.FileSystemObject")
strPath = fso.GetParentFolderName (WScript.ScriptFullName)
If fso.FileExists(strPath & "\" & prgName) Then
objShell.ShellExecute "wscript.exe", _
Chr(34) & strPath & "\" & prgName & Chr(34), _
"", "runas", 1
Else
Wscript.echo "Script file not found"
End If
Run Code Online (Sandbox Code Playgroud)
OSVersion.vbs 脚本运行良好:
cscript OSVersion.vbs Microsoft (R) Windows 脚本宿主版本 5.8 版权所有 (C) Microsoft Corporation。版权所有。
微软 Windows 7 家庭高级版
版本6.1.7601
问题#1 当我尝试运行此提升版本时,标准输出上没有显示任何内容
C:\Users\ChemModeling\Documents\FreeThink\Java\install>cscript RunElevated.vbs OSVersion.vbs Microsoft (R) Windows 脚本宿主版本 5.8 版权所有 (C) Microsoft Corporation。版权所有。
运行:OSVersion.vbs
问题 #2 我不知道如何将参数包含到我传递给 RunElevated 的脚本中。我读到你应该使用像“我的参数在这里”这样的语法,所以我尝试了这个:
' Run the script in elevated mode
'
' This will be needed to install programs into Program Files
prgName = Wscript.Arguments.Item(0)
prgArgs = ""
If Wscript.Arguments.Count > 1 Then
For i = 1 To Wscript.Arguments.Count - 1
prgArgs = prgArgs & " " & Wscript.Arguments.Item(i)
Next
End If
Wscript.echo "Running: " & prgName & prgArgs
Set objShell = CreateObject("Shell.Application")
Set fso = CreateObject("Scripting.FileSystemObject")
strPath = fso.GetParentFolderName (WScript.ScriptFullName)
If fso.FileExists(strPath & "\" & prgName) Then
objShell.ShellExecute "wscript.exe", _
Chr(39) & Chr(34) & strPath & "\" & prgName & prgArgs & Chr(34) & Chr(39), _
"", "runas", 1
Else
Wscript.echo "Script file not found"
End If
Run Code Online (Sandbox Code Playgroud)
如果我运行:
cscript RunElevated.vbs OSVersion.vbs 测试 Microsoft (R) Windows 脚本宿主版本 5.8 版权所有 (C) Microsoft Corporation。版权所有。
运行:OSVersion.vbs 测试
然后我收到错误“没有文件扩展名“.vbs Test”的引擎”。
谁能建议我需要改变什么?
我在解决这个问题方面取得了一些进展-
我将第一个脚本更改为:
操作系统版本.vbs
' Return a string indicating the operating system
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
("Select * from Win32_OperatingSystem")
'Wscript.echo Wscript.Arguments(0)
For Each objOperatingSystem in colOperatingSystems
Wscript.Echo objOperatingSystem.Caption
Wscript.Echo "Version " & objOperatingSystem.Version
Next
Run Code Online (Sandbox Code Playgroud)
这样我就能真正看到是否发生了什么事。
我将第二个脚本更改为:
' Run the script in elevated mode
'
' This will be needed to install programs into Program Files
prgName = Wscript.Arguments.Item(0)
prgArgs = ""
If Wscript.Arguments.Count > 1 Then
For i = 1 To Wscript.Arguments.Count - 1
prgArgs = prgArgs & " " & Wscript.Arguments.Item(i)
Next
End If
Wscript.echo "Running: " & prgName & prgArgs
Set objShell = CreateObject("Shell.Application")
Set fso = CreateObject("Scripting.FileSystemObject")
strPath = fso.GetParentFolderName (WScript.ScriptFullName)
If fso.FileExists(strPath & "\" & prgName) Then
objShell.ShellExecute "wscript.exe", _
Chr(34) & strPath & "\" & prgName & Chr(34) & prgArgs, _
"", "runas", 1
Else
Wscript.echo "Script file not found"
End If
Run Code Online (Sandbox Code Playgroud)
并使用:wscript RunElevated.vbs OSVersion.vbs 测试
现在,我看到了脚本运行时在弹出窗口中回显的信息。
所以我回到问题#1,我正在启动一个新的 shell 以管理员模式运行,所以如果我切换到 cscript 并尝试将信息写入 stdout 或使用 Wscript.StdOut.Write,它将出现在新的 shell 中我永远不会看到它,或者至少这就是我认为的问题所在。
有解决这个问题的标准方法吗?
问题#1:
当您运行提升的脚本时,您看不到任何输出,因为 RunElevated.vbs 脚本使用 WScript.exe 重新启动 OSVersion.vbs 脚本。OSVersion.vbs 使用只能从控制台获得的标准输入和输出。将 RunElevated.vbs 中的命令行更改为使用 cscript.exe 而不是 wscript.exe 可解决此问题。有关更多信息,请查看我的文章WSH 中的错误捕获和捕获第三方输出。
问题#2
看来您的问题在于您混合了不同类型的引号。命令应如下所示:
“C:\带空格的路径\command.exe”“带空格的参数”“另一个”
尝试这些脚本。
运行Elevated.vbs
' Run a script in Elevated Mode
strName = WScript.Arguments.Item(0)
strArgs = ""
If WScript.Arguments.Count > 1 Then
For i = 1 To WScript.Arguments.Count - 1
strArgs = strArgs & " " & WScript.Arguments.Item(i)
Next
End If
WScript.echo "Running: ", strName, strArgs
Set objShell = CreateObject("Shell.Application")
Set objFso = CreateObject("Scripting.FileSystemObject")
strPath = objFso.GetParentFolderName(WScript.ScriptFullName)
strParams = Chr(34) & strPath & "\" & strName & Chr(34)
If strArgs <> "" Then
strParams = strParams & " " & Chr(34) & strArgs & Chr(34)
End If
If objFso.FileExists(strPath & "\" & strName) Then
objShell.ShellExecute "cscript.exe", strParams, "", "runas", 1
Else
WScript.echo "Script file not found"
End If
Run Code Online (Sandbox Code Playgroud)
操作系统版本.vbs
'Test passing arguments to launched script
If WScript.Arguments.Count > 0 Then
For i = 0 To WScript.Arguments.Count - 1
strArgs = Trim(strArgs & " " & WScript.Arguments.Item(i))
Next
End If
' Return a string indicating the operating system
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
WScript.StdOut.Write objOperatingSystem.Caption
WScript.StdOut.WriteBlankLines(1)
WScript.StdOut.Write "Version " & objOperatingSystem.Version
WScript.StdOut.WriteBlankLines(2)
Next
' Print out any passed arguments
WScript.StdOut.WriteBlankLines(2)
WScript.StdOut.Write strArgs
WScript.StdOut.WriteBlankLines(2)
' Keep the window from closing before you get a chance to read it
WScript.StdOut.Write "Press any key to continue "
strInput = WScript.StdIn.Read(1)
Run Code Online (Sandbox Code Playgroud)
为了捕获输出,我可能会尝试使用 WshShellExecute方法的不同方法。有一些可用的命令行工具,例如Elevate或HStart,可以使用提升的权限启动命令行。以下是我使用 Elevate 的方法。
' Run a script in Elevated Mode
strName = WScript.Arguments.Item(0)
strArgs = ""
If WScript.Arguments.Count > 1 Then
For i = 1 To WScript.Arguments.Count - 1
strArgs = strArgs & " " & WScript.Arguments.Item(i)
Next
End If
WScript.echo "Running: ", strName, strArgs
Set WshShell = CreateObject("WScript.Shell")
Set objFso = CreateObject("Scripting.FileSystemObject")
strPath = objFso.GetParentFolderName(WScript.ScriptFullName)
strParams = Chr(34) & strPath & "\" & strName & Chr(34)
If strArgs <> "" Then
strParams = strParams & " " & Chr(34) & strArgs & Chr(34)
End If
If objFso.FileExists(strPath & "\" & strName) Then
Set WshShellExec = WshShell.Exec("elevate cscript.exe " & strParams)
Select Case WshShellExec.Status
Case WshFinished
strOutput = WshShellExec.StdOut.ReadAll
Case WshFailed
strOutput = WshShellExec.StdErr.ReadAll
End Select
Else
WScript.echo "Script file not found"
End If
WScript.echo strOutput
Run Code Online (Sandbox Code Playgroud)