TFS电源外壳任务未将正确的参数数量传递给脚本

问题描述:

我遇到了一个问题,即作为持续集成过程的一部分运行电源外壳脚本任务,并且脚本未收到正确数量的参数。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

任何想法?

您必须编写-takePhysicalPath而不是$takePhysicalPath。作为替代方案,您可以使用Parameter属性并使其成为positional

+0

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

+0

如果您更改了作业,会发生什么情况。您正在使用'args',但您也可以使用'Parameter'块定义参数。那么,如果你把'$ dirPath = $ args [0]'改成'$ dirPath = $ directory_path'? – Moerwald

+0

刚刚测试,仍然获得相同数量的参数,3 – Heinrich

我测试了脚本,只需在脚本末尾添加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传递参数。确保您传递的参数名称与声明的参数一致(没有任何错字错误),否则参数不会传递给脚本。 –