使用-file文件路径中的VBscript:空白隐藏Powershell窗口

使用-file文件路径中的VBscript:空白隐藏Powershell窗口,powershell,vbscript,window,hide,Powershell,Vbscript,Window,Hide,用户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\powersh

用户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和WSScript。另一个很好的例子出现在这里(链接到此标题的完整页面):

我已经测试过了,它对我很有效。虽然不使用“-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
在上面的示例中,我基本上复制了上面的代码引用(以及列出的链接),用变量strExpression的文件路径(包括空格)替换了PS命令,并通过用“-file”参数替换“-command”参数更新了strCMD定义。但是,错误消息指向一个无法识别的参数“c:\aP\u RDB\Set”,即它没有看到整个路径“c:\aP\u RDB\Set\SimpleInsert.PS1”。看来引号(或其他东西)的转义不起作用。。。更好的说法是,我做得不对


如能提供任何周到的建议,将不胜感激。仅供参考:我在Powershell中尝试了-WindowStyle隐藏选项;我和许多人一样,发现这在Windows平台上不是一种一致的方法(更具体地说,对我来说:我无法让它工作)。

感谢您的参考,以下是两种在名称中使用空格启动脚本的方法:

1) 使用双“(“”)


2) 正如我在本文末尾解释的那样(抱歉,用法语),编写自己的PowerShell.exe时不带控制台。

未经测试,因为我只有PSV1。但是试试这个:
strCMD=“PowerShell-noprofile-executionpolicy Unrestricted-NonInteractive-file”&Chr(34)&{'&strExpression&'}&Chr(34)
谢谢Daniel-这似乎不起作用,但我进一步研究了它,并尝试了以下方法,它成功了:
strCMD=“powershell-noprofile-executionpolicy Unrestricted-NonInteractive-file”&CHR(34)&strExpression&CHR(34)
@DanielCook
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
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:\temp\test arg.ps1"" " & chr(34) & Args(0) & chr(34) , 0