【发布时间】:2012-07-21 22:33:20
【问题描述】:
我正在尝试学习如何在提升模式下运行 vbscript。如果我尝试提升的 vbscript 存在参数,我将无法完全正常工作。
这是一个简单的例子:
OSVersion.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 脚本宿主版本 5.8 版权所有 (C) 微软公司。保留所有权利。
Microsoft Windows 7 家庭高级版
版本 6.1.7601
问题 #1 当我尝试运行此提升程序时,stdout 上没有显示任何内容
C:\Users\ChemModeling\Documents\FreeThink\Java\install>cscript RunElevated.vbs OSVersion.vbs Microsoft (R) Windows 脚本宿主版本 5.8 版权所有 (C) 微软公司。保留所有权利。
运行: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) 微软公司。保留所有权利。
正在运行:OSVersion.vbs 测试
然后我得到一个错误“没有用于文件扩展名“.vbs 测试”的引擎。
谁能建议我需要改变什么?
我在解决这个问题上取得了一些进展-
我把第一个脚本改成:
OSVersion.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,它将出现在新的外壳,我永远也看不到它,或者至少我认为问题是这样的。
有解决这个问题的标准方法吗?
【问题讨论】:
标签: vbscript