2015-10-06 74 views
1

批處理文件運行PowerShell腳本。問題是路徑位置有空格。批處理文件只能看到空格前的第一個單詞。批處理文件沒有正確添加文件夾結構與空格?

SET targetDir="\\server.com\xxx\First Folder with spaces\Second folder with spaces" 
SET archiveDir="\\server.com\xxx\First Folder with spaces\Second folder with spaces\Archive" 
SET siteID=ABC 


REM Run the import Powershell script for files. 
powershell.exe -File D:\localPath\powershell\PowerShellScript.ps1 -Path "%targetDir%" -Filter *.dlu -Site %siteID% -ArchiveDir "%archiveDir%" 

所以,當我跑我的PowerShell腳本它得到的參數從批處理文件

param 
( 
[string] $Path = $(Throw "You must specify a directory path containing the files to import."), 
[string] $Filter = $(Throw "You must provide a file pattern/filter (e.g. *.dlu) to be used for locating the files to import"), 
[string] $Site = $(Throw "You must provide a site identifier for the import."), 
[string] $ArchiveDir = $null # If not provided, the imported files will be deleted rather than archived to another location. 
) 

但是當我有輸出路徑到屏幕上,我得到:

Path= C:\First 
Site= ABC 
Archive= C:\First 

什麼我做錯了嗎?

如果我使用硬代碼運行.bat文件,它的工作原理非常完美。

REM SET targetDir="\\server.com\xxx\First Folder with spaces\Second folder with spaces" 
REM SET archiveDir="\\server.com\xxx\First Folder with spaces\Second folder with spaces\Archive" 
REM SET siteID=ABC 

REM Run the import Powershell script for files. 
powershell.exe -File D:\localPath\powershell\PowerShellScript.ps1 -Path "\\server.com\xxx\First Folder with spaces\Second folder with spaces" -Filter *.dlu -Site ABC -ArchiveDir "\\server.com\xxx\First Folder with spaces\Second folder with spaces\Archive" 

回答

1

這是因爲您正在複製報價。您在SET targetDir=聲明中設置了引號,然後在調用powershell.exe時再次執行引號。由此產生的命令是:

powershell.exe -File D:\localPath\powershell\PowerShellScript.ps1 -Path ""\\server.com\xxx\First Folder with spaces\Second folder with spaces"" -Filter *.dlu -Site ABC -ArchiveDir ""\\server.com\xxx\First Folder with spaces\Second folder with spaces\Archive"" 
+0

謝謝@MobyDisk那就是問題 –

1

@MobyDisk已經告訴了手邊的問題的根源是什麼。

你應該改變你的SET命令行的批處理文件如下:

SET "targetDir=\\server.com\xxx\First Folder with spaces\Second folder with spaces" 
SET "archiveDir=\\server.com\xxx\First Folder with spaces\Second folder with spaces\Archive" 

注意移動打開引號"。此更改可避免引號成爲變量值的一部分。
(你可以簡單地也刪除所有的報價,不過你可能會用一些特殊字符,如^&遇到麻煩。任何%跡象路徑仍然需要加倍雖然不被無意中刪除。)

+0

謝謝@aschipfl –