2017-06-16 65 views
1

我遇到了一個問題,即作爲持續集成過程的一部分運行電源外殼腳本任務,並且腳本未收到正確數量的參數。TFS電源外殼任務未將正確的參數數量傳遞給腳本

這是我正在

Param 
(
    [string]$directory_path, 
    [string]$website_name, 
    [string]$app_n, 
    [string]$takePhysicalPath 
) 
$ScriptBlockContent = 
{ 
    $dirPath = $args[0] 
    $websiteName = $args[1] 
    $appName = $args[2] 
    $takePhysicalPath = $args[3] 
    $physicalPath = $False 

    Write-Host "Param: directory_path: " $dirPath 
    Write-Host "Param website_name: " $websiteName 
    Write-Host "Param app_n: " $appName 
    Write-Host "Param takePhysicalPath: " $takePhysicalPath 

    Write-Host 'Parameter Count: ' $args.Count 

    if ([bool]::TryParse($takePhysicalPath, [ref]$physicalPath)) 
    { 
     Write-Host 'Parsed: ' $takePhysicalPath ' -> ' $physicalPath 
    } 
    else 
    { 
     Write-Host 'Not Parsed' 
    } 

    Write-Host $dirPath 
    Write-Host $websiteName 
    Write-Host $appName 
    Write-Host $physicalPath 


} 

劇本,如果我在一個PowerShell功能運行腳本我正確地獲取價值

$directory_path = "C:\SolutionsContent" 
$website_name = "websiteName" 
$app_n = "Styles" 
$takePhysicalPath = "true" 

Invoke-Command -ScriptBlock $ScriptBlockContent -ArgumentList $directory_path, $website_name, $app_n, $takePhysicalPath 

這是輸出

enter image description here

問題出現在我試圖通過TFS enter image description here

,我傳遞給腳本的參數有以下幾種

-directory_path "C:\SolutionsContent" -website_name "websiteName" -app_n "Styles" -takePhysicalPath "true" 

變量$ takePhysicalPath是從來沒有過,下面是輸出

enter image description here

任何想法?

回答

0

您必須編寫-takePhysicalPath而不是$takePhysicalPath。作爲替代方案,您可以使用Parameter屬性並使其成爲positional

+0

我做了一太,不幸的是我抄我的測試方法之一,但即使它不工作 – Heinrich

+0

如果您更改了作業,會發生什麼情況。您正在使用'args',但您也可以使用'Parameter'塊定義參數。那麼,如果你把'$ dirPath = $ args [0]'改成'$ dirPath = $ directory_path'? – Moerwald

+0

剛剛測試,仍然獲得相同數量的參數,3 – Heinrich

0

我測試了腳本,只需在腳本末尾添加invoke命令,然後傳遞給該腳本的參數按預期工作。

確保您引用正確的PARAM,在您發佈上述$ takePhy invoke命令 sicalPath(注意字母「」)是帕拉姆$ takePhysicalPath不一致

腳本應該是:

Param 
(
    [string]$directory_path, 
    [string]$website_name, 
    [string]$app_n, 
    [string]$takePhysicalPath 
) 
$ScriptBlockContent = 
{ 
    $dirPath = $args[0] 
    $websiteName = $args[1] 
    $appName = $args[2] 
    $takePhysicalPath = $args[3] 
    $physicalPath = $False 

    Write-Host "Param: directory_path: " $dirPath 
    Write-Host "Param website_name: " $websiteName 
    Write-Host "Param app_n: " $appName 
    Write-Host "Param takePhysicalPath: " $takePhysicalPath 

    Write-Host 'Parameter Count: ' $args.Count 

    if ([bool]::TryParse($takePhysicalPath, [ref]$physicalPath)) 
    { 
     Write-Host 'Parsed: ' $takePhysicalPath ' -> ' $physicalPath 
    } 
    else 
    { 
     Write-Host 'Not Parsed' 
    } 

    Write-Host $dirPath 
    Write-Host $websiteName 
    Write-Host $appName 
    Write-Host $physicalPath 


} 
Invoke-Command -ScriptBlock $ScriptBlockContent -ArgumentList $directory_path, $website_name, $app_n, $takePhysicalPath 

enter image description here

+0

「我」是一個錯字錯誤,我仍然得到相同的行爲 – Heinrich

+0

@海因裏希我再次測試,它在我身邊工作。只是請嘗試使用我發佈的上述腳本,然後通過TFS傳遞參數。確保您傳遞的參數名稱與聲明的參數一致(沒有任何錯字錯誤),否則參數不會傳遞給腳本。 –