【发布时间】:2012-12-10 14:08:17
【问题描述】:
用户 JPBlanc 等人针对此类问题给出了很好的解决方案:
Set Args = Wscript.Arguments
'MsgBox "Chemin LDAP: " & Args(0)
'MsgBox "Classe: " & Args(1)
Set objShell = CreateObject("Wscript.Shell")
objShell.Run "c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -nologo -Noninteractive -file c:\SlxRH\RhModif.ps1 " & chr(34) & Args(0) & chr(34) , 0
我已经测试过这样的解决方案,它工作正常。但是,我的问题涉及标识为“-file”选项的参数的文件路径中包含空格的情况;例如,如果上面的代码示例的文件路径为:
c:\Slx RH\RhModif.PS1
注意文件路径中的空白。问题是:如何正确转义文件路径。我还没有找到一种方法来做到这一点(让它发挥作用)。
我在这个站点和一般的“interweb”上进行了搜索,并了解到(我认为)有两个级别的脚本解释正在进行:VBscript 和 Wscript。另一个很好的例子出现在这里(链接到带有此标题的完整页面):
Run scheduled tasks with WinForm GUI in PowerShell
Dim objShell
Set objShell=CreateObject("WScript.Shell")
strExpression="get-date | add-content c:\temp\test.txt"
strCMD="powershell -sta -noProfile -NonInteractive -nologo -command " & Chr(34) & "&{" & strExpression &"}" & Chr(34)
objShell.Run strCMD,0
我已经对此进行了测试,它对我有用。虽然不使用“-file”选项,但此示例给出(您会想到的)将是为 strCMD 变量放置“-command”或“-file”输入的通用方法。但是,当我使用以下代码时,它不起作用:
Dim objShell
Set objShell=CreateObject("WScript.Shell")
strExpression="C:\aP_RDB\Set Up\SimpleInsert.PS1"
strCMD="powershell -noprofile -executionpolicy Unrestricted -NonInteractive -file " & Chr(34) & "&{" & strExpression &"}" & Chr(34)
objShell.Run strCMD,0
在上面的示例中,我基本上复制了上面的代码参考(以及列出的链接),将 PS 命令替换为变量 strExpression 的文件路径(包括空格),并通过替换来更新 strCMD 定义“-command”参数和“-file”参数。但是,错误消息指向一个无法识别的参数“c:\aP_RDB\Set”,也就是说,它没有看到整个路径“C:\aP_RDB\Set Up\SimpleInsert.PS1”。引号(或其他东西)的转义似乎不起作用......更好的是,我没有正确地做。
任何深思熟虑的建议将不胜感激。仅供参考:我已经尝试过 Powershell 中的 -WindowStyle Hidden 选项;我和许多人一样发现这不是跨 Windows 平台的一致方法(更具体地说,对我来说:我无法让它工作)。
【问题讨论】:
-
未经测试,因为我只有 PS v1。但试试这个:
strCMD="powershell -noprofile -executionpolicy Unrestricted -NonInteractive -file " & Chr(34) & "&{'" & strExpression &"'}" & Chr(34) -
谢谢丹尼尔 - 这似乎没有用,但我进一步研究并尝试了以下方法并成功了:
strCMD="powershell -noprofile -executionpolicy Unrestricted -NonInteractive -file " & CHR(34) & strExpression & CHR(34)@DanielCook
标签: powershell vbscript window hide