使用 TFS vNext 任务时出现问题:找不到与参数名称匹配的参数
Issue with using a TFS vNext task: A parameter cannot be found that matches parameter name
我正在尝试创建一个简单的 TFS vNext 任务,使用 Powershell 执行 Powershell 脚本。
我可以加载任务,但在使用任务触发发布后,我得到
##[error]System.Management.Automation.ParameterBindingException: A parameter cannot be found that matches parameter name 'PoolName'.
我的task.json如下。
{
"id": "7fe28fb2-c323-4494-beb6-b5c5939b09e7",
"name": "ManageIIS",
"friendlyName": "ManageIIS",
"description": "ManageIIS",
"helpMarkDown": "ManageIIS",
"category": "Utility",
"visibility": [
"Build"
],
"author": "Baskar Lingam",
"version": {
"Major": 1,
"Minor": 0,
"Patch": 3
},
"demands": [],
"minimumAgentVersion": "1.83.0",
"groups": [
{
"name": "advanced",
"displayName": "Advanced",
"isExpanded": false
}
],
"instanceNameFormat": "ManageIIS",
"inputs": [
{
"name": "PoolName",
"type": "string",
"label": "Application Pool Name",
"required": true,
"defaultValue": "AppPoolName"
},
{
"name": "AppPoolAction",
"type": "pickList",
"label": "App Pool Action (Stop or Start)",
"required": true,
"defaultValue": "",
"helpMarkDown": "Name of the Database",
"options": {
"Stop": "Stop App Pool",
"Start": "Start App Pool"
}
},
{
"name": "ResetIIS",
"type": "boolean",
"label": "Reset IIS",
"defaultValue": "false",
"required": true,
"helpMarkDown": "To reset IIS on a web server"
}
],
"execution": {
"PowerShell": {
"target": "ManageIIS.ps1",
"argumentFormat": "",
"workingDirectory": "$(currentDirectory)"
}
}
}
我的 Powershell 脚本在这里。
#####################################################################
# Author : Baskar Lingam Ramachandran
# Created Date : 25th Nov 2016
# Updated Date : 30th Nov 2016
######################################################################
[CmdletBinding()]
Param()
Trace-VstsEnteringInvocation $MyInvocation
try
{
# Get the inputs.
[string]$PoolName = Get-VstsInput -Name PoolName
[string]$AppPoolAction = Get-VstsInput -Name AppPoolAction
[bool]$ResetIIS = Get-VstsInput -Name ResetIIS -AsBool
# Load the require module to manage IIS
<#if(-not(Get-Module WebAdministration))
{
Import-Module WebAdministration
}
#>
# Code for App pool stop or start
if (($AppPoolAction -eq "Stop") -or ($AppPoolAction -eq "Start"))
{
if(-not($PoolName))
{
Write-Host "`t`tApp pool name is not provided. Please provide the same.`r`n" -ForegroundColor Green
}
else
{
if($AppPoolAction -eq "Stop")
{
if(Test-Path IIS:\AppPools$PoolName)
{
Write-Host "`t`tApp Pool $PoolName exists`r`n"
if(-not((Get-WebAppPoolState $PoolName).Value -eq "stopped"))
{
Write-Host "`t`t-------------------------------------------------`r`n"
Write-Host "`t`tStopping application pool $PoolName`r`n"
Write-Host "`t`t-------------------------------------------------`r`n"
Write-Host "`t`tApp Pool $PoolName is not stopped. Stopping it now.`r`n" -ForegroundColor DarkYellow
Stop-WebAppPool $PoolName
Write-Host "`t`tThe command execution is complete.`r`n" -ForegroundColor Cyan
$appPoolState = Get-WebAppPoolState $PoolName
Write-Host "`t`tThe current state of the app pool $PoolName is $($appPoolState.Value).`r`n" -ForegroundColor DarkMagenta
}
else
{
Write-Host "`t`tApp Pool $PoolName is already stopped.`r`n" -ForegroundColor DarkGreen
}
}
else
{
Write-Host "`t`tApp Pool $PoolName does not exist`r`n" -ForegroundColor DarkRed
}
}
if($AppPoolAction -eq "Start")
{
if(Test-Path IIS:\AppPools$PoolName)
{
Write-Host "`t`tApp Pool $PoolName exists`r`n"
if(-not((Get-WebAppPoolState $PoolName).Value -eq "Started"))
{
Write-Host "`t`t-------------------------------------------------`r`n"
Write-Host "`t`tStarting application pool $PoolName`r`n"
Write-Host "`t`t-------------------------------------------------`r`n"
Write-Host "`t`tApp Pool $PoolName is not started. Starting it now.`r`n" -ForegroundColor DarkYellow
Start-WebAppPool $PoolName
Write-Host "`t`tThe command execution is complete.`r`n" -ForegroundColor Cyan
$appPoolState = Get-WebAppPoolState $PoolName
Write-Host "`t`tThe current state of the app pool $PoolName is $($appPoolState.Value).`r`n" -ForegroundColor DarkMagenta
}
else
{
Write-Host "`t`tApp Pool $PoolName is already started.`r`n" -ForegroundColor DarkGreen
}
}
else
{
Write-Host "`t`tApp Pool $PoolName does not exist`r`n" -ForegroundColor DarkRed
}
}
}
}
if ($ResetIIS -eq "true")
{
iisreset -stop -noforce
if ($LASTEXITCODE -eq 0)
{
Write-Host "`t`tInternet services successfully stopped`r`n" -ForegroundColor DarkGreen
}
iisreset -start -noforce
if ($LASTEXITCODE -eq 0)
{
Write-Host "`t`tInternet services successfully started`r`n" -ForegroundColor DarkRed
}
}
} finally {
Trace-VstsLeavingInvocation $MyInvocation
}
我有另一个简单的任务,它不带任何参数而且工作得很好。
有什么指点吗?
我错过了什么?
从脚本上下文错误,我可以看出您目前收到的错误是因为 powershell 无法获取 'PoolName[ 的值=30=]' 在“Get-VstsInput”命令中。
其次,如果您在脚本中动态传递参数,那么您必须在参数中使用参数。
我还是推荐大家使用Args[0]、Args[1]等作为参数传递的方式。
大多数情况下,我们在函数中使用参数。
我相信您之前已经在某处编译了函数(“Trace-VstsEnteringInvocation”和“Get-VstsInput”);否则它会抛出一个错误作为无法识别的术语。
根据您的脚本,我做了一些调整,并对脚本进行了评论;
以下是脚本:
#####################################################################
# Author : Baskar Lingam Ramachandran
# Created Date : 25th Nov 2016
# Updated Date : 30th Nov 2016
######################################################################
[CmdletBinding()]
Param($PoolName,$AppPoolAction,$ResetIIS) # Taking the input as parameters
# I would suggest you to make the functions as Global so that you can use it anywhere freely and call from any block.
Trace-VstsEnteringInvocation $MyInvocation
try
{
# Get the inputs.
[string]$PoolName = Get-VstsInput -Name PoolName
"The value of PoolName is $PoolName" # This helps in input validation, we can check it if it is returning the proper value or not.
[string]$AppPoolAction = Get-VstsInput -Name AppPoolAction
[bool]$ResetIIS = Get-VstsInput -Name ResetIIS -AsBool
# Load the require module to manage IIS
<#if(-not(Get-Module WebAdministration))
{
Import-Module WebAdministration
}
#>
# Code for App pool stop or start
if (($AppPoolAction -eq "Stop") -or ($AppPoolAction -eq "Start"))
{
if(-not($PoolName))
{
Write-Host "`t`tApp pool name is not provided. Please provide the same.`r`n" -ForegroundColor Green
}
else
{
if($AppPoolAction -eq "Stop")
{
if(Test-Path IIS:\AppPools$PoolName)
{
Write-Host "`t`tApp Pool $PoolName exists`r`n"
if(-not((Get-WebAppPoolState $PoolName).Value -eq "stopped"))
{
Write-Host "`t`t-------------------------------------------------`r`n"
Write-Host "`t`tStopping application pool $PoolName`r`n"
Write-Host "`t`t-------------------------------------------------`r`n"
Write-Host "`t`tApp Pool $PoolName is not stopped. Stopping it now.`r`n" -ForegroundColor DarkYellow
Stop-WebAppPool $PoolName
Write-Host "`t`tThe command execution is complete.`r`n" -ForegroundColor Cyan
$appPoolState = Get-WebAppPoolState $PoolName
Write-Host "`t`tThe current state of the app pool $PoolName is $($appPoolState.Value).`r`n" -ForegroundColor DarkMagenta
}
else
{
Write-Host "`t`tApp Pool $PoolName is already stopped.`r`n" -ForegroundColor DarkGreen
}
}
else
{
Write-Host "`t`tApp Pool $PoolName does not exist`r`n" -ForegroundColor DarkRed
}
}
if($AppPoolAction -eq "Start")
{
if(Test-Path IIS:\AppPools$PoolName)
{
Write-Host "`t`tApp Pool $PoolName exists`r`n"
if(-not((Get-WebAppPoolState $PoolName).Value -eq "Started"))
{
Write-Host "`t`t-------------------------------------------------`r`n"
Write-Host "`t`tStarting application pool $PoolName`r`n"
Write-Host "`t`t-------------------------------------------------`r`n"
Write-Host "`t`tApp Pool $PoolName is not started. Starting it now.`r`n" -ForegroundColor DarkYellow
Start-WebAppPool $PoolName
Write-Host "`t`tThe command execution is complete.`r`n" -ForegroundColor Cyan
$appPoolState = Get-WebAppPoolState $PoolName
Write-Host "`t`tThe current state of the app pool $PoolName is $($appPoolState.Value).`r`n" -ForegroundColor DarkMagenta
}
else
{
Write-Host "`t`tApp Pool $PoolName is already started.`r`n" -ForegroundColor DarkGreen
}
}
else
{
Write-Host "`t`tApp Pool $PoolName does not exist`r`n" -ForegroundColor DarkRed
}
}
}
}
if ($ResetIIS -eq "true")
{
iisreset -stop -noforce
if ($LASTEXITCODE -eq 0)
{
Write-Host "`t`tInternet services successfully stopped`r`n" -ForegroundColor DarkGreen
}
iisreset -start -noforce
if ($LASTEXITCODE -eq 0)
{
Write-Host "`t`tInternet services successfully started`r`n" -ForegroundColor DarkRed
}
}
} finally {
Trace-VstsLeavingInvocation $MyInvocation
}
希望这个回答suffice/helps你。随意喜欢它,因为这也会帮助其他人。
我正在回答我的问题,因为除了 Ranadip Gupta 的回答之外,我还找到了另一种工作方式(在 VSTS Microsoft 任务 https://github.com/Microsoft/vsts-tasks/tree/master/Tasks 中使用)。我赞成 Ranadip 的回答。但提供我自己的答案,因为还有另一种方式。
在我从 PS1 脚本中删除 Trace-VstsEnteringInvocation、Trace-VstsLeavingInvocation 和 Get-VstsInput 命令后,Ranadip Gupta 的回答效果很好。
您需要像下面这样在 param() 本身中定义参数,此方法才能起作用。
param([string]$PoolName, [string]$AppPoolAction, [string]$ResetIIS)
要在 PS1 中使用 Args[],请注意传递给 PS1 的参数的格式为
-PoolName TFD -AppPoolAction Stop -ResetIIS true
所以需要用$($Args[1]), $($Args[3]), $($Args[5])来引用各个参数得到的值,如$( $Args[0]),$($Args[2]),$($Args[4]) 指参数本身。
第二种方法是将 task.json 的执行从 Powershell 更改为 Powershell3。通过这种方法,我可以使用 Trace-Vsts* 和 Get-VstsInput 命令。在此方法中,param([string]$PoolName,[string]$AppPoolAction, [string]$ResetIIS) 可以保留为 param().
来自
"execution": {
"PowerShell":{
"target": "ManageIIS.ps1",
}
}
至
"execution": {
"PowerShell3": {
"target": "ManageIIS.ps1",
}
}
如果您使用的是 VSTS 构建任务 SDK,则需要使用 Powershell 3..
"execution": { "PowerShell3": { "target": "ManageIIS.ps1", } }
我正在尝试创建一个简单的 TFS vNext 任务,使用 Powershell 执行 Powershell 脚本。
我可以加载任务,但在使用任务触发发布后,我得到
##[error]System.Management.Automation.ParameterBindingException: A parameter cannot be found that matches parameter name 'PoolName'.
我的task.json如下。
{
"id": "7fe28fb2-c323-4494-beb6-b5c5939b09e7",
"name": "ManageIIS",
"friendlyName": "ManageIIS",
"description": "ManageIIS",
"helpMarkDown": "ManageIIS",
"category": "Utility",
"visibility": [
"Build"
],
"author": "Baskar Lingam",
"version": {
"Major": 1,
"Minor": 0,
"Patch": 3
},
"demands": [],
"minimumAgentVersion": "1.83.0",
"groups": [
{
"name": "advanced",
"displayName": "Advanced",
"isExpanded": false
}
],
"instanceNameFormat": "ManageIIS",
"inputs": [
{
"name": "PoolName",
"type": "string",
"label": "Application Pool Name",
"required": true,
"defaultValue": "AppPoolName"
},
{
"name": "AppPoolAction",
"type": "pickList",
"label": "App Pool Action (Stop or Start)",
"required": true,
"defaultValue": "",
"helpMarkDown": "Name of the Database",
"options": {
"Stop": "Stop App Pool",
"Start": "Start App Pool"
}
},
{
"name": "ResetIIS",
"type": "boolean",
"label": "Reset IIS",
"defaultValue": "false",
"required": true,
"helpMarkDown": "To reset IIS on a web server"
}
],
"execution": {
"PowerShell": {
"target": "ManageIIS.ps1",
"argumentFormat": "",
"workingDirectory": "$(currentDirectory)"
}
}
}
我的 Powershell 脚本在这里。
#####################################################################
# Author : Baskar Lingam Ramachandran
# Created Date : 25th Nov 2016
# Updated Date : 30th Nov 2016
######################################################################
[CmdletBinding()]
Param()
Trace-VstsEnteringInvocation $MyInvocation
try
{
# Get the inputs.
[string]$PoolName = Get-VstsInput -Name PoolName
[string]$AppPoolAction = Get-VstsInput -Name AppPoolAction
[bool]$ResetIIS = Get-VstsInput -Name ResetIIS -AsBool
# Load the require module to manage IIS
<#if(-not(Get-Module WebAdministration))
{
Import-Module WebAdministration
}
#>
# Code for App pool stop or start
if (($AppPoolAction -eq "Stop") -or ($AppPoolAction -eq "Start"))
{
if(-not($PoolName))
{
Write-Host "`t`tApp pool name is not provided. Please provide the same.`r`n" -ForegroundColor Green
}
else
{
if($AppPoolAction -eq "Stop")
{
if(Test-Path IIS:\AppPools$PoolName)
{
Write-Host "`t`tApp Pool $PoolName exists`r`n"
if(-not((Get-WebAppPoolState $PoolName).Value -eq "stopped"))
{
Write-Host "`t`t-------------------------------------------------`r`n"
Write-Host "`t`tStopping application pool $PoolName`r`n"
Write-Host "`t`t-------------------------------------------------`r`n"
Write-Host "`t`tApp Pool $PoolName is not stopped. Stopping it now.`r`n" -ForegroundColor DarkYellow
Stop-WebAppPool $PoolName
Write-Host "`t`tThe command execution is complete.`r`n" -ForegroundColor Cyan
$appPoolState = Get-WebAppPoolState $PoolName
Write-Host "`t`tThe current state of the app pool $PoolName is $($appPoolState.Value).`r`n" -ForegroundColor DarkMagenta
}
else
{
Write-Host "`t`tApp Pool $PoolName is already stopped.`r`n" -ForegroundColor DarkGreen
}
}
else
{
Write-Host "`t`tApp Pool $PoolName does not exist`r`n" -ForegroundColor DarkRed
}
}
if($AppPoolAction -eq "Start")
{
if(Test-Path IIS:\AppPools$PoolName)
{
Write-Host "`t`tApp Pool $PoolName exists`r`n"
if(-not((Get-WebAppPoolState $PoolName).Value -eq "Started"))
{
Write-Host "`t`t-------------------------------------------------`r`n"
Write-Host "`t`tStarting application pool $PoolName`r`n"
Write-Host "`t`t-------------------------------------------------`r`n"
Write-Host "`t`tApp Pool $PoolName is not started. Starting it now.`r`n" -ForegroundColor DarkYellow
Start-WebAppPool $PoolName
Write-Host "`t`tThe command execution is complete.`r`n" -ForegroundColor Cyan
$appPoolState = Get-WebAppPoolState $PoolName
Write-Host "`t`tThe current state of the app pool $PoolName is $($appPoolState.Value).`r`n" -ForegroundColor DarkMagenta
}
else
{
Write-Host "`t`tApp Pool $PoolName is already started.`r`n" -ForegroundColor DarkGreen
}
}
else
{
Write-Host "`t`tApp Pool $PoolName does not exist`r`n" -ForegroundColor DarkRed
}
}
}
}
if ($ResetIIS -eq "true")
{
iisreset -stop -noforce
if ($LASTEXITCODE -eq 0)
{
Write-Host "`t`tInternet services successfully stopped`r`n" -ForegroundColor DarkGreen
}
iisreset -start -noforce
if ($LASTEXITCODE -eq 0)
{
Write-Host "`t`tInternet services successfully started`r`n" -ForegroundColor DarkRed
}
}
} finally {
Trace-VstsLeavingInvocation $MyInvocation
}
我有另一个简单的任务,它不带任何参数而且工作得很好。
有什么指点吗?
我错过了什么?
从脚本上下文错误,我可以看出您目前收到的错误是因为 powershell 无法获取 'PoolName[ 的值=30=]' 在“Get-VstsInput”命令中。
其次,如果您在脚本中动态传递参数,那么您必须在参数中使用参数。
我还是推荐大家使用Args[0]、Args[1]等作为参数传递的方式。 大多数情况下,我们在函数中使用参数。
我相信您之前已经在某处编译了函数(“Trace-VstsEnteringInvocation”和“Get-VstsInput”);否则它会抛出一个错误作为无法识别的术语。
根据您的脚本,我做了一些调整,并对脚本进行了评论; 以下是脚本:
#####################################################################
# Author : Baskar Lingam Ramachandran
# Created Date : 25th Nov 2016
# Updated Date : 30th Nov 2016
######################################################################
[CmdletBinding()]
Param($PoolName,$AppPoolAction,$ResetIIS) # Taking the input as parameters
# I would suggest you to make the functions as Global so that you can use it anywhere freely and call from any block.
Trace-VstsEnteringInvocation $MyInvocation
try
{
# Get the inputs.
[string]$PoolName = Get-VstsInput -Name PoolName
"The value of PoolName is $PoolName" # This helps in input validation, we can check it if it is returning the proper value or not.
[string]$AppPoolAction = Get-VstsInput -Name AppPoolAction
[bool]$ResetIIS = Get-VstsInput -Name ResetIIS -AsBool
# Load the require module to manage IIS
<#if(-not(Get-Module WebAdministration))
{
Import-Module WebAdministration
}
#>
# Code for App pool stop or start
if (($AppPoolAction -eq "Stop") -or ($AppPoolAction -eq "Start"))
{
if(-not($PoolName))
{
Write-Host "`t`tApp pool name is not provided. Please provide the same.`r`n" -ForegroundColor Green
}
else
{
if($AppPoolAction -eq "Stop")
{
if(Test-Path IIS:\AppPools$PoolName)
{
Write-Host "`t`tApp Pool $PoolName exists`r`n"
if(-not((Get-WebAppPoolState $PoolName).Value -eq "stopped"))
{
Write-Host "`t`t-------------------------------------------------`r`n"
Write-Host "`t`tStopping application pool $PoolName`r`n"
Write-Host "`t`t-------------------------------------------------`r`n"
Write-Host "`t`tApp Pool $PoolName is not stopped. Stopping it now.`r`n" -ForegroundColor DarkYellow
Stop-WebAppPool $PoolName
Write-Host "`t`tThe command execution is complete.`r`n" -ForegroundColor Cyan
$appPoolState = Get-WebAppPoolState $PoolName
Write-Host "`t`tThe current state of the app pool $PoolName is $($appPoolState.Value).`r`n" -ForegroundColor DarkMagenta
}
else
{
Write-Host "`t`tApp Pool $PoolName is already stopped.`r`n" -ForegroundColor DarkGreen
}
}
else
{
Write-Host "`t`tApp Pool $PoolName does not exist`r`n" -ForegroundColor DarkRed
}
}
if($AppPoolAction -eq "Start")
{
if(Test-Path IIS:\AppPools$PoolName)
{
Write-Host "`t`tApp Pool $PoolName exists`r`n"
if(-not((Get-WebAppPoolState $PoolName).Value -eq "Started"))
{
Write-Host "`t`t-------------------------------------------------`r`n"
Write-Host "`t`tStarting application pool $PoolName`r`n"
Write-Host "`t`t-------------------------------------------------`r`n"
Write-Host "`t`tApp Pool $PoolName is not started. Starting it now.`r`n" -ForegroundColor DarkYellow
Start-WebAppPool $PoolName
Write-Host "`t`tThe command execution is complete.`r`n" -ForegroundColor Cyan
$appPoolState = Get-WebAppPoolState $PoolName
Write-Host "`t`tThe current state of the app pool $PoolName is $($appPoolState.Value).`r`n" -ForegroundColor DarkMagenta
}
else
{
Write-Host "`t`tApp Pool $PoolName is already started.`r`n" -ForegroundColor DarkGreen
}
}
else
{
Write-Host "`t`tApp Pool $PoolName does not exist`r`n" -ForegroundColor DarkRed
}
}
}
}
if ($ResetIIS -eq "true")
{
iisreset -stop -noforce
if ($LASTEXITCODE -eq 0)
{
Write-Host "`t`tInternet services successfully stopped`r`n" -ForegroundColor DarkGreen
}
iisreset -start -noforce
if ($LASTEXITCODE -eq 0)
{
Write-Host "`t`tInternet services successfully started`r`n" -ForegroundColor DarkRed
}
}
} finally {
Trace-VstsLeavingInvocation $MyInvocation
}
希望这个回答suffice/helps你。随意喜欢它,因为这也会帮助其他人。
我正在回答我的问题,因为除了 Ranadip Gupta 的回答之外,我还找到了另一种工作方式(在 VSTS Microsoft 任务 https://github.com/Microsoft/vsts-tasks/tree/master/Tasks 中使用)。我赞成 Ranadip 的回答。但提供我自己的答案,因为还有另一种方式。
在我从 PS1 脚本中删除 Trace-VstsEnteringInvocation、Trace-VstsLeavingInvocation 和 Get-VstsInput 命令后,Ranadip Gupta 的回答效果很好。
您需要像下面这样在 param() 本身中定义参数,此方法才能起作用。
param([string]$PoolName, [string]$AppPoolAction, [string]$ResetIIS)
要在 PS1 中使用 Args[],请注意传递给 PS1 的参数的格式为
-PoolName TFD -AppPoolAction Stop -ResetIIS true
所以需要用$($Args[1]), $($Args[3]), $($Args[5])来引用各个参数得到的值,如$( $Args[0]),$($Args[2]),$($Args[4]) 指参数本身。
第二种方法是将 task.json 的执行从 Powershell 更改为 Powershell3。通过这种方法,我可以使用 Trace-Vsts* 和 Get-VstsInput 命令。在此方法中,param([string]$PoolName,[string]$AppPoolAction, [string]$ResetIIS) 可以保留为 param().
来自
"execution": { "PowerShell":{ "target": "ManageIIS.ps1", } }
至
"execution": { "PowerShell3": { "target": "ManageIIS.ps1", } }
如果您使用的是 VSTS 构建任务 SDK,则需要使用 Powershell 3..
"execution": { "PowerShell3": { "target": "ManageIIS.ps1", } }