使用TFS vNext任务时出现问题:找不到与参数名称匹配的参数

时间:2016-11-30 22:28:13

标签: powershell tfs tfsbuild azure-pipelines azure-pipelines-build-task

我正在尝试创建一个简单的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
}

我有另一个简单的任务,它不带任何参数,它完全正常。

任何指针?

我错过了什么?

3 个答案:

答案 0 :(得分:5)

如果您使用的是VSTS Build Task SDK,则需要使用Powershell 3 ..

&#34;执行&#34;:{&#34; PowerShell3&#34;:{&#34;目标&#34;:&#34; ManageIIS.ps1&#34;,}}

答案 1 :(得分:2)

从脚本上下文错误,我可以看到您目前收到的错误是因为powershell无法获取“ PoolName ”中的值 Get-VstsInput “命令。

其次,如果你在脚本中动态传递参数,那么你必须使用参数内的参数。

我仍然建议你使用Args [0],Args [1]等作为参数传递的方法。 大多数情况下,我们在函数中使用params。

我相信您之前已经在某处编译了函数(“ 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
}

希望这个答案足够/帮助你。随意喜欢它,因为这也会对其他人有所帮助。

答案 2 :(得分:0)

我正在回答我的问题,因为除了Ranadip Gupta的回答之外,我找到了另一种工作方式(在VSTS Microsoft任务中使用https://github.com/Microsoft/vsts-tasks/tree/master/Tasks)。我赞成Ranadip的答案。但是提供我自己的答案,因为还有另一种方式。

  1. 我从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])引用参数本身。

  2. 第二种方法是将 task.json 的执行从 Powershell更改为Powershell3 。使用此方法,我可以使用Trace-Vsts *和Get-VstsInput命令。在这个方法中,param([string] $ PoolName,[string] $ AppPoolAction,[string] $ ResetIIS)可以保存为param()。

    &#34;执行&#34;:{     &#34; PowerShell&#34;:{         &#34; target&#34;:&#34; ManageIIS.ps1&#34;,     } }

    &#34;执行&#34;:{     &#34; PowerShell3&#34; :{         &#34; target&#34;:&#34; ManageIIS.ps1&#34;,     } }