2016-11-22 64 views
4

以下參數Split-Path不正確,應該是$delZipExe
這使得$delZipCmd散列被設置爲空。
我希望WorkingDirectory值在$delZipCmd散列中設置爲空。Powershell splat銷燬變量

爲什麼會發生這種情況?

Set-StrictMode -Version latest 
$delZipExe = '\\servername\ziptools\SP3DDeliverZips.exe' 
$delZipDest = "D:\" 
$delZipArgs = @('/execute', 
       '/source', '/RAD ', '/debugpdb', '/wait' 
       ) 
$delZipCmd = @{ FilePath = $delZipExe; 
       ArgumentList = $delZipArgs; 
       NoNewWindow = $true; 
       WorkingDirectory = (Split-Path $delZipCmd); # <== should be $delZipExe 
       Wait = $true; 
       } 
$delZipCmd | ft 
+1

我不明白你的問題,你做了什麼錯誤,你期待什麼,也沒有人知道第10行的註釋你的代碼意味着什麼。你能澄清嗎? –

+0

在最後一行,$ delZipCmd是$ null。正如我在問題中所說的,我想明白爲什麼會發生這種情況,因爲我預計WorkingDirectory的值將爲$ null,但所有其他條目的設置都會正確。 – opedroso

回答

4

由於參數變量的驗證,以Split-Path期間構造哈希表的拋出終止錯誤,整個表達式結束。

您可以在子表達式($())隔離Split-Path語句來避免這種情況:

$delZipCmd = @{ 
    FilePath = $delZipExe; 
    ArgumentList = $delZipArgs; 
    NoNewWindow = $true; 
    WorkingDirectory = $(Split-Path $delZipCmd); # <== notice the $ 
    Wait = $true; 
} 
+0

爲什麼使用$ delZipCmd作爲Split-Path,*在$ delZipCmd哈希表的聲明中?這段代碼對我來說沒有意義。 –

+0

@KoryGill由於OP特別詢問有關*的行爲*,請重新閱讀 –

+0

嚴格模式,我期望原始代碼不會運行,因爲$ delZipCmd沒有設置(這是您得到的當你從一個新的/乾淨的PowerShell窗口運行它沒有任何prev變量設置)。所以對我而言,這個問題以一個正確的前提爲主張。繼續... –