powershell - Powershell Split 功能在 VSTS 发布管道中不起作用
问题描述
我编写了一个 powershell 脚本,它将多个 webapps(逗号分隔)作为输入。我正在使用 powershell 拆分功能拆分这些 webapps,并通过使用 for-each 循环遍历它们中的每一个来配置 webapps。在 Powershell 编辑器中一切正常,但是当我将相同的脚本配置到 VSTS 发布管道时,拆分功能不起作用并导致失败。
输入:devopstestwebapp1,devopstestwebapp2
代码:$WebAppName = $WebAppName.Split(',')
输出(拆分后):devopstestwebapp1 devopstestwebapp2
错误:未找到资源组“DevOpsResourseGroup”下的资源“Microsoft.Web/sites/devopstestwebapp1 devopstestwebapp2”。
以下是我的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
}
解决方案
下面与 VSTS powershell 任务完美配合:
$WebAppName = '$(WebAppName)'
write-host $WebAppName
foreach($servicename in $WebAppName.Split(','))
{
write-host $servicename
}
输出 :
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