将变量参数传递给vbscript中的ShellExecute

时间:2012-07-21 22:33:21

标签: vbscript

我正在尝试学习如何在高架模式下运行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 Script Host Version 5.8   版权所有(C)Microsoft Corporation。保留所有权利。

     

Microsoft Windows 7 Home Premium

     

版本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 Script Host Version 5.8   版权所有(C)Microsoft Corporation。保留所有权利。

     

正在运行: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并尝试将信息写入stdout或使用Wscript.StdOut.Write,它将出现在新的外壳,我永远不会看到它,或者至少我相信这个问题。

是否有解决此问题的标准方法?

2 个答案:

答案 0 :(得分:3)

问题#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:\ path with spaces \ command.exe”“参数with spaces”“和另一个”

试试这些剧本。

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

OSVersion.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方法的不同方法。有一些命令行工具可用,例如ElevateHStart,可以启动具有提升权限的命令行。以下是使用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

答案 1 :(得分:1)

我发现了两件有趣的事情;首先,我必须得到适当数量的引用和空格,其次,我必须首先调用临时脚本,然后调用最终脚本。

Intial Calling Script片段:

If objFSO.FileExists("C:\Windows\System32\elevate.vbs") Then
     objWSHShell.ShellExecute "wscript.exe", Chr(34) & Chr(34) & "C:\Windows\System32\elevate.vbs" & Chr(32) & strCampus & Chr(32) & strLocation & Chr(32) & strAssetTag & Chr(34) & Chr(34), "", "", 1
Else
     MsgBox "Regbrand script file not found",0,"Error Message"
End If

注意Chr()语句和从原始脚本传递到临时脚本的3个变量。

Interim Script片段:

If Args.Count = 0 Then
    MsgBox "Missing arguments to UAC permissions script.",0,"Missing Parameters"
Else
    strCampus = Args.Item(0)
    strLocation = Args.Item(1)
    strAssetTag = Args.Item(2)

    strFilePath = objFSO.GetParentFolderName (WScript.ScriptFullName)
    If objFSO.FileExists("C:\Windows\System32\regbrand.vbs") Then
         objWSHShell.ShellExecute "wscript.exe", Chr(34) & Chr(34) & "C:\Windows\System32\regbrand.vbs" & Chr(32) & strCampus & Chr(32) & strLocation & Chr(32) & strAssetTag & Chr(34) & Chr(34), "", "runas", 1
    Else
         MsgBox "Script file not found",0,"Error Message"
    End If
End If

在临时脚本中,我们将相同的参数传递给最终脚本。再次注意Chr()语句。

第一个脚本传递3个参数并调用临时脚本,后者又使用提升的权限调用第3个脚本“regbrand.vbs”。它的工作原理就像应该的那样。如果删除临时脚本,则无效。我还没想出来,但相信它与从网络位置调用原始脚本有关。但是,我希望调用和传递参数的语法很有用。