2

Powershell を使用して Powershell スクリプトを実行し、単純な TFS vNext タスクを作成しようとしています。

タスクをロードできましたが、タスクを使用してリリースをトリガーした後、

##[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

2

スクリプト コンテキスト エラーから、現在受け取っているエラーは、powershell が「Get-VstsInput」コマンドで「 PoolName 」の値を取得できないことが原因であることがわかります。

次に、スクリプトでその場でパラメーターを渡す場合は、param 内でパラメーターを使用する必要があります。

パラメータを渡す方法として、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
}

この回答で十分/お役に立てば幸いです。それは他の人にも役立つので、気軽に気に入ってください。

于 2016-12-01T05:02:30.960 に答える
0

Ranadip Gupta からの回答に加えて、別の作業方法 (VSTS Microsoft タスクhttps://github.com/Microsoft/vsts-tasks/tree/master/Tasksで使用される) を見つけたので、質問に答えています。私はラナディップの答えに賛成票を投じました。しかし、さらに別の方法もあるので、私自身の答えを提供します。

  1. PS1スクリプトからTrace-VstsEnteringInvocation、Trace-VstsLeavingInvocation、およびGet-VstsInputコマンドを削除した後、Ranadip Guptaの答えはうまく機能します。

    このメソッドを機能させるには、以下のように param() 自体でパラメーターを定義する必要があります。

    param([string]$PoolName, [string]$AppPoolAction, [string]$ResetIIS)

    PS1 で Args[] を使用するには、PS1 に渡される引数が次の形式であることに注意してください。

    -PoolName TFD -AppPoolAction 停止 -ResetIIS true

    したがって、$($Args[1])、$($Args[3])、$($Args[5]) を使用して、さまざまなパラメーターに対して取得された値を参照する必要があります ($($Args[0])。 ),$($Args[2]),$($Args[4]) はパラメータ自体を参照します。

  2. 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 に答える