我正在尝试学习如何在提升模式下运行 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
RunElevated.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
OSVersion.vbs 脚本运行良好:
cscript OSVersion.vbs Microsoft (R) Windows Script Host Version 5.8 Copyright (C) Microsoft Corporation. All rights reserved.
Microsoft Windows 7 Home Premium
Version 6.1.7601
问题 #1 当我尝试运行这个提升时,标准输出上没有任何显示
C:\Users\ChemModeling\Documents\FreeThink\Java\install>cscript RunElevated.vbs OSVersion.vbs Microsoft (R) Windows Script Host Version 5.8 Copyright (C) Microsoft Corporation. All rights reserved.
Running: 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
如果我运行:
cscript RunElevated.vbs OSVersion.vbs Test Microsoft (R) Windows Script Host Version 5.8 Copyright (C) Microsoft Corporation. All rights reserved.
Running: OSVersion.vbs Test
然后我收到一个错误“没有文件扩展名“.vbs 测试”的引擎。
谁能建议我需要改变什么?
我在解决这个问题方面取得了一些进展-
我将第一个脚本更改为:
操作系统版本.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 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
并使用: 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 可以解决此问题。更多内容请查看我的文章Error Trapping and Capturing Third-Party Output in WSH .
问题#2
看起来您的问题是您混合了不同类型的引号。命令应如下所示:
"C:\带空格的路径\command.exe""带空格的参数""和另一个"
试试这些脚本。
RunElevated.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
操作系统版本.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)
为了捕获输出,我可能会尝试使用 WshShell
Execute
的不同方法。方法。有可用的命令行工具,例如 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
关于vbscript - 在 vbscript 中将变量参数传递给 ShellExecute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11596365/