简体   繁体   English

Powershell 拆分功能在 VSTS 发布管道中不起作用

[英]Powershell Split function not working in VSTS Release pipeline

I have written a powershell script which takes multiple webapps(comma separated) as input.我编写了一个 powershell 脚本,它将多个 webapps(逗号分隔)作为输入。 I am splitting these webapps using powershell split function and configuring webapps by traversing each one of them using for-each loop.我正在使用 powershell 拆分功能拆分这些 webapp,并通过使用 for-each 循环遍历它们中的每一个来配置 webapp。 Everything works fine in Powershell editor but when I configure the same script to VSTS release pipeline , split function doesn't work and which results in failure.在 Powershell 编辑器中一切正常,但是当我将相同的脚本配置为 VSTS 发布管道时,拆分功能不起作用并导致失败。

Input : devopstestwebapp1,devopstestwebapp2输入:devopstestwebapp1,devopstestwebapp2

Code : $WebAppName = $WebAppName.Split(',')代码:$WebAppName = $WebAppName.Split(',')

Output (After Split) : devopstestwebapp1 devopstestwebapp2输出(拆分后):devopstestwebapp1 devopstestwebapp2

Error : The Resource 'Microsoft.Web/sites/devopstestwebapp1 devopstestwebapp2' under resource group 'DevOpsResourseGroup' was not found.错误:未找到资源组“DevOpsResourseGroup”下的资源“Microsoft.Web/sites/devopstestwebapp1 devopstestwebapp2”。

Following is my powershell script以下是我的powershell脚本

# Parameters
param (   
    [Parameter(Position=0,mandatory=$true)]
    [string] $AADAppID,
    [Parameter(Position=1,mandatory=$true)]
    [string] $AADKey,
    [Parameter(Position=2,mandatory=$true)]
    [string] $TenantId,
    [Parameter(Position=3,mandatory=$true)]
    [string] $ResourceGroupName,
    [Parameter(Position=4,mandatory=$true)]
    [string] $ServerName,
    [Parameter(Position=5,mandatory=$true)]
    [string] $RGLocation,
    [Parameter(Position=6,mandatory=$true)]
    [string] $WebAppName,
    [Parameter(Position=7,mandatory=$true)]
    [string] $SubscriptionName
 )

    # Connect to Azure

    $ssAADKey = ConvertTo-SecureString $AADKey -AsPlainText -Force
    $psCredential = New-Object System.Management.Automation.PSCredential($AADAppID, $ssAADKey)

    Connect-AzureRmAccount -ServicePrincipal -Credential $psCredential -Subscription $SubscriptionName -TenantId $TenantId     
    write-host $WebAppName
    $WebAppName = $WebAppName.Split(',')
     write-host $WebAppName
    Foreach ($servicename in $WebAppName)
    {
        write-host $servicename

    }

在此处输入图片说明

Below works perfectly with VSTS powershell task : 下面与VSTS powershell任务完美配合:

Store app name in variable : 将应用名称存储在变量中: 在此输入图像描述

$WebAppName = '$(WebAppName)'
write-host $WebAppName
foreach($servicename in $WebAppName.Split(','))
{
        write-host $servicename
}

Output : 输出:

2019-04-22T11:02:02.7680996Z devopstestwebapp1,devopstestwebapp2,devopstestwebapp3
2019-04-22T11:02:02.7737101Z devopstestwebapp1
2019-04-22T11:02:02.7750490Z devopstestwebapp2
2019-04-22T11:02:02.7765756Z devopstestwebapp3

The problematic line is this one:有问题的线路是这样的:

$WebAppName = $WebAppName.Split(',') $WebAppName = $WebAppName.Split(',')

You are reassigning the result of split to the same variable $WebAppName which has been declared as a string in the parameter list.您正在将 split 的结果重新分配给已在参数列表中声明为字符串的同一变量 $WebAppName。 So the array result of Split will be cast to a string, not an array anymore.所以 Split 的数组结果将被转换为字符串,而不是数组。

The solution is to assign the result of split to a new variable:解决办法是将split的结果赋值给一个新的变量:

$WebAppNameSplit = $WebAppName.Split(',') $WebAppNameSplit = $WebAppName.Split(',')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 VSTS - 用于添加变量以释放环境的 Powershell 脚本不起作用 - VSTS - Powershell script to add variable to release environment not working VSTS发布AzureRM Powershell版本 - VSTS Release AzureRM Powershell Version VSTS管道中的PowerShell远程 - PowerShell Remote from VSTS Pipeline 如何使用PowerShell将服务主体分配给托管的VSTS发布管道中的AAD组? - How might I assign a service principal to an AAD group in my hosted VSTS release pipeline using PowerShell? 如何在VSTS发布管道中修改应用程序清单? - How to modify the application manifest in VSTS release pipeline? VSTS版本定义中PowerShell脚本的硬编码参数 - Hardcode arguments to the PowerShell Script in VSTS Release definition VSTS版本,带有Powershell引号语法内的变量的CMD Webdeploy - VSTS release, CMD webdeploy with variables inside Powershell quotation syntax 仅在脚本存在时如何在VSTS版本中运行远程Powershell? - How to run a remote Powershell in VSTS release only if script exists? 在vsts中运行的Powershell脚本不会识别环境变量 - Powershell script running in vsts release not recognizing environment variables VSTS托管代理中的运行空间和作业发布了Azure Powershell脚本 - Runspaces and Jobs in VSTS hosted agent release Azure Powershell script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM