我正在尝试学习如何在提升模式下运行 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 版本 5.8 版权所有 (C) Microsoft Corporation。版权所有。
微软视窗 7 家庭高级版
版本 6.1.7601
问题 #1 当我尝试运行此提升程序时,stdout 上什么也没有出现
C:\Users\ChemModeling\Documents\FreeThink\Java\install>cscript RunElevated.vbs OSVersion.vbs Microsoft (R) Windows Script Host Version 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
如果我运行:
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 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 并尝试将信息写入标准输出或使用 Wscript.StdOut.Write,它将出现在新的 shell 中而且我永远不会看到它,或者至少我认为这就是问题所在。
有没有解决这个问题的标准方法?