2012-12-24 48 views
2

用戶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平臺上不是一個一致的方法(對我來說更具體:我無法實現它)。

+0

未測試,因爲我只有PS v1,但試試這個:'strCMD =「powershell -noprofile -executionpolicy Unrestricted -NonInteractive -file「&Chr(34)&」&{'「&strExpression&」'}「&Chr (34)' –

+0

感謝丹尼爾 - 這似乎沒有工作,但我進一步看了一下,並嘗試以下,它的工作: 'strCMD =「powershell -noprofile -executionpolicy無限制-NonInteractive -file」&CHR(34) &strExpression&CHR(34)'@DanielCook –

回答

1

感謝您的參考,下面是開始在名稱中的空白腳本兩種方式:採用雙

1)「(」「)

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 

2)由編寫自己的沒有控制檯的PowerShell.exe,因爲我在本文結尾處解釋了""PowerShell.exe" ne sait pas démarrer sans afficher une fenêtre"(對不起,法語)