Powershell TFS电源外壳任务没有';t将正确数量的参数传递给脚本
我面临一个问题,作为持续集成过程的一部分,我运行powershell脚本任务,但是脚本没有收到正确数量的参数 这是我正在运行的脚本Powershell TFS电源外壳任务没有';t将正确数量的参数传递给脚本,powershell,tfs,tfs-2015,Powershell,Tfs,Tfs 2015,我面临一个问题,作为持续集成过程的一部分,我运行powershell脚本任务,但是脚本没有收到正确数量的参数 这是我正在运行的脚本 Param ( [string]$directory_path, [string]$website_name, [string]$app_n, [string]$takePhysicalPath ) $ScriptBlockContent = { $dirPath = $args[0] $websiteName = $args[1]
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
这是输出
当我试图通过TFS运行它时,问题就出现了
我传递给脚本的参数如下
-directory_path "C:\SolutionsContent" -website_name "websiteName" -app_n "Styles" -takePhysicalPath "true"
永远不会传递变量$takePhysicalPath,下面是输出
有什么想法吗?你必须写
-takePhysicalPath
而不是$takePhysicalPath
。作为一种替代方法,您可以使用参数
属性并使其生效。您必须编写-takePhysicalPath
而不是$takePhysicalPath
。作为替代方法,您可以使用参数
属性并使其生效。我测试了脚本,只需在脚本末尾添加invoke命令,然后传递到脚本的参数就会按预期工作
确保引用了正确的参数,在$takePhyisicalPath(注意字母“i”)上方发布的invoke命令中,该参数与$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
我测试了脚本,只需在脚本末尾添加invoke命令,然后传递到脚本的参数就可以正常工作了 确保引用了正确的参数,在$takePhyisicalPath(注意字母“i”)上方发布的invoke命令中,该参数与$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
我也做了一次,不幸的是,我复制了我的一种测试方法,但即使它不起作用,如果你改变了作业会发生什么。您使用的是
args
,但是您还使用参数
块定义了参数。那么,如果您将$dirPath=$args[0]
更改为$dirPath=$directory\u path
,会发生什么呢?刚刚测试过,但仍然得到相同数量的参数,3I也做了一个,不幸的是,我复制了我的一个测试方法,但即使它不起作用,如果您更改了分配,会发生什么。您使用的是args
,但是您还使用参数
块定义了参数。那么,如果您将$dirPath=$args[0]
更改为$dirPath=$directory\u path
,会发生什么情况呢?刚刚经过测试,仍然获得相同数量的参数,3“i”是一个输入错误,我仍然得到相同的参数behavior@Heinrich我再次测试,它在我身边工作。请尝试使用上面我发布的脚本,然后通过TFS传递参数。确保您传递的参数名称与声明的参数一致(没有任何键入错误),否则参数将不会传递到脚本。如果“i”是一个键入错误,我仍然会得到相同的结果behavior@Heinrich我再次测试,它在我身边工作。请尝试使用上面我发布的脚本,然后通过TFS传递参数。确保您传递的参数名称与声明的参数一致(没有任何打字错误),否则参数将不会传递给脚本。