2

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

我有另一个不带任何参数的简单任务,它工作得很好。

任何指针?

我错过了什么?

4

3 回答 3

5

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

“执行”:{“PowerShell3”:{“目标”:“ManageIIS.ps1”,}}

于 2016-12-01T19:47:18.270 回答
2

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

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

我仍然建议您使用 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
}

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

于 2016-12-01T05:02:30.960 回答
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() 本身中定义参数,如下所示,此方法才能正常工作。

    参数([字符串]$PoolName,[字符串]$AppPoolAction,[字符串]$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()。

    “执行”:{“PowerShell”:{“目标”:“ManageIIS.ps1”,}}

    “执行”:{ “PowerShell3”:{“目标”:“ManageIIS.ps1”,}}

于 2016-12-01T12:32:39.087 回答