azure VSTS扩展:“由于缺少一个或多个强制参数,无法处理命令:应用程序目录Web应用程序名称资源组名称”

dsekswqp  于 2022-12-19  发布在  其他
关注(0)|答案(1)|浏览(87)

我正在为某个版本创建VSTS任务,但遇到了上述错误。我知道该错误的含义,但不确定如何解决,因为这是我第一次创建自定义任务。
基本上,该任务基于脚本中获得的已发布配置文件设置,将源文件和FTP文件上传到Azure。
下面是PowerShell代码:

[CmdletBinding()]
param()

Trace-VstsEnteringInvocation $MyInvocation

$appdirectory = get-vstsinput -Name appdirectory
$webappname = get-vstsinput -Name webappname
$ResourceGroupName = get-vstsinput -Name ResourceGroupName

#write-host $appdirectory

$location="East US"
    try {
    # Get inputs.
    # Get publishing profile for the web app
    $xml = [xml](Get-AzureRmWebAppPublishingProfile -Name $webappname `
    -ResourceGroupName $ResourceGroupName `
    -OutputFile null)

    # Extracts connection information from publishing profile
    $username = $xml.SelectNodes("//publishProfile[@publishMethod=`"FTP`"]/@userName").value
    $password = $xml.SelectNodes("//publishProfile[@publishMethod=`"FTP`"]/@userPWD").value
    $url = $xml.SelectNodes("//publishProfile[@publishMethod=`"FTP`"]/@publishUrl").value

    Write-Output "$username"
    Write-Output "$password"
    Write-Output "$url"
    #Write-Output "$localpath"

    # Upload files recursively
    Set-Location $appdirectory
    $webclient = New-Object -TypeName System.Net.WebClient
    $webclient.Credentials = New-Object System.Net.NetworkCredential($username,$password)
    $files = Get-ChildItem -Path $appdirectory -Recurse | Where-Object{!($_.PSIsContainer)}
    foreach ($file in $files)
    {
        $relativepath = (Resolve-Path -Path $file.FullName -Relative).Replace(".\", "").Replace('\', '/')
        $uri = New-Object System.Uri("$url/$relativepath")
        "Uploading to " + $uri.AbsoluteUri
        $webclient.UploadFile($uri, $file.FullName)
    }
    $webclient.Dispose()

    } finally {
        Trace-VstsLeavingInvocation $MyInvocation
    }

与此相关的是一个JSON文件,用于TFS中的构建和发布管理器。

{
    "id": "LongStringofNumbers",
    "name": "NameOfTask",
    "friendlyName": "FriendlyNameOfTask",
    "description": "Build Task that will upload a file or folder to a destination in Azure using Published Profile Credentials.",
    "helpMarkDown": "",
    "category": "Build",
    "visibility": [
        "Build"
    ],
    "runsOn": [
        "Agent",
        "DeploymentGroup"
    ],
    "author": "ME",
    "version": {
        "Major": 0,
        "Minor": 0,
        "Patch": 16
    },
    "instanceNameFormat": "Uploads File Using Published Profile Credentials",
    "groups": [
        {
            "name": "advanced",
            "displayName": "Advanced",
            "isExpanded": true
        }
    ],
    "inputs": [
        {
            "name": "appdirectory",
            "type": "filePath",
            "label": "Source Path",
            "defaultValue": "",
            "required": true,
            "helpMarkDown": "Location of file(s) for uploading to Azure."
        },
        {
            "name": "webappname",
            "type": "string",
            "label": "Azure Webapp name",
            "defaultValue": "",
            "required": true,
            "helpMarkDown": "Azure App name; I.E. - 900014campuslinkapi."
        },
        {
            "name": "ResourceGroupName",
            "type": "string",
            "label": "Azure Resource Group name",
            "defaultValue": "",
            "required": true,
            "helpMarkDown": "Azure Resource Group Name I.E. - 900014-prod."
        }
    ],
    "execution": {
        "PowerShell3": {
            "target": "powershell.ps1",
            "platforms": [
                "windows"
            ],
            "argumentFormat": "",
            "workingDirectory": "$(currentDirectory)"
        }
    }
}

所以这个错误是说我需要JSON文件中的三个强制参数作为一个字段添加到发布管理器中。但是我不认为脚本和JSON文件出于某种原因连接,所以即使我在发布管理器字段中放置项目,它仍然失败并出现错误。

3yhwsihp

3yhwsihp1#

您仍然需要将这些值拉入脚本中。看看reference tasks是如何工作的,或者看看documentation
您的脚本需要导入和使用任务SDK,并调用Get-VstsInput来检索值。
不管怎样,您应该使用PowerShell3处理程序,因为PowerShell是遗留的。
另一个选择是不创建自定义任务,如果这只打算供内部使用,虽然100%的偏见在我的一部分。我不喜欢把简单的PowerShell脚本变成不透明的黑盒子,并喜欢驱动一切从源代码控制。

相关问题