用户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
我在这个网站和普通的“互联网”上进行了搜索,并学习到(我认为)有两个脚本解释级别正在进行:VBscript和Wscript。另一个非常好的例子出现在这里(链接到具有此标题的完整页面):
使用PowerShell在WinForm GUI中运行定期任务
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”选项的情况下,这个例子给出了一个通用的方法,可以将“-command”或“-file”输入作为strCMD变量。然而,当我使用以下代码时,它不起作用:
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
在我的示例中,我基本上复制了上面的代码参考(以及列出的链接),用包含空格的文件路径替换了变量strExpression,并通过将“-command”参数替换为“-file”参数来更新strCMD定义。然而,错误消息指向一个无法识别的参数“c:\aP_RDB\Set”,即它没有看到整个路径“C:\aP_RDB\SetUp\SimpleInsert.PS1”。看起来引号的转义(或者其他什么东西)没有起作用……更好地说,我没有做正确。任何周到的建议都将不胜感激。FYI:我已经尝试过在PowerShell中使用“-WindowStyle Hidden”选项;像许多人一样,我发现这不是跨Windows平台的一种一致的方法(更具体地说,对我而言:我无法使其工作)。
strCMD="powershell -noprofile -executionpolicy Unrestricted -NonInteractive -file " & Chr(34) & "&{'" & strExpression &"'}" & Chr(34)
- DanielstrCMD="powershell -noprofile -executionpolicy Unrestricted -NonInteractive -file " & CHR(34) & strExpression & CHR(34)
@DanielCook - Dave Ef