用户JPBlanc和其他人都给予了很好的解决方案与解决这样一个问题:隐藏使用VBScript PowerShell窗口:在-file文件路径空白
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:\→六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,并通过用“-file”参数替换“-command”参数来更新strCMD定义。但是,错误消息指向一个无法识别的参数“c:\ aP_RDB \ Set”,即它不是,请参阅整个路径“C:\ aP_RDB \ Set Up \ SimpleInsert.PS1”。看起来,引号(或某物)的转义不起作用...更好地说,我没有正确地做。
任何周到的意见将不胜感激。仅供参考:我已经尝试了Powershell中的-WindowStyle隐藏选项;我喜欢很多人发现在Windows平台上不是一个一致的方法(对我来说更具体:我无法实现它)。
未测试,因为我只有PS v1,但试试这个:'strCMD =“powershell -noprofile -executionpolicy Unrestricted -NonInteractive -file“&Chr(34)&”&{'“&strExpression&”'}“&Chr (34)' –
感谢丹尼尔 - 这似乎没有工作,但我进一步看了一下,并尝试以下,它的工作: 'strCMD =“powershell -noprofile -executionpolicy无限制-NonInteractive -file”&CHR(34) &strExpression&CHR(34)'@DanielCook –