我遇到了一个问题,我在执行power shell脚本任务时将其作为持续集成过程的一部分,并且脚本没有收到正确数量的参数。
这是我正在运行的脚本
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
这是输出
我传递给脚本的参数是以下
-directory_path "C:\SolutionsContent" -website_name "websiteName" -app_n "Styles" -takePhysicalPath "true"
永远不会传递变量$ takePhysicalPath,下面是输出
有什么想法吗?
答案 0 :(得分:0)
您要写$takePhysicalPath
而不是Parameter
。作为替代方案,您可以使用{{1}} attribue并将其设为positional。
答案 1 :(得分:0)
我测试了脚本,只需在脚本末尾添加invoke命令,然后传递给脚本的参数按预期工作。
确保您引用了正确的参数,在您上面发布的调用命令中,$ takePhy i sicalPath(注意字母" i ")是与param $ 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