1
votes

I have written a powershell script which takes multiple webapps(comma separated) as input. I am splitting these webapps using powershell split function and configuring webapps by traversing each one of them using for-each loop. 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.

Input : devopstestwebapp1,devopstestwebapp2

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

Output (After Split) : devopstestwebapp1 devopstestwebapp2

Error : The Resource 'Microsoft.Web/sites/devopstestwebapp1 devopstestwebapp2' under resource group 'DevOpsResourseGroup' was not found.

Following is my powershell script

# 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

    }

enter image description here

2
how confident are you that the .Split() method is not working as opposed to something in the VSTS release pipeline is interfering with the split results? - Lee_Dailey
Since you are using the same variable name $WebAppName, you'll end up with an array where you expect it to be a string. Use something like foreach ($appName in $WebAppName.Split(',')) {...} - Theo
I tried foreach ($appName in $WebAppName.Split(',')) , but still not splitted the string - kartik iyer
@kartikiyer - if it really is not splitting on the comma ... then you don't have a comma in the string. [grin] confirm that it does have a comma ... and then confirm that something ELSE is forcing the resulting array into a string. ///// you likely will need to post the actual code being run to get any more detail. - Lee_Dailey
Added my powershell script , where i am trying to take webappname as input from release pipeline variables and splitting the same - kartik iyer

2 Answers

0
votes

Below works perfectly with VSTS powershell task :

Store app name in variable : enter image description here

$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
0
votes

The problematic line is this one:

$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. So the array result of Split will be cast to a string, not an array anymore.

The solution is to assign the result of split to a new variable:

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