【问题标题】:Powershell Split function not working in VSTS Release pipelinePowershell Split 功能在 VSTS 发布管道中不起作用
【发布时间】:2021-09-29 23:10:16
【问题描述】:

我编写了一个 powershell 脚本,它接受多个 webapps(逗号分隔)作为输入。 我正在使用 powershell 拆分功能拆分这些 webapps,并通过使用 for-each 循环遍历它们中的每一个来配置 webapps。 在 Powershell 编辑器中一切正常,但是当我为 VSTS 发布管道配置相同的脚本时,拆分功能不起作用并导致失败。

输入:devopstestwebapp1,devopstestwebapp2

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

输出(拆分后):devopstestwebapp1 devopstestwebapp2

错误:资源 'Microsoft.Web/sites/devopstestwebapp1 找不到资源组“DevOpsResourseGroup”下的 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

    }

【问题讨论】:

  • 您对 .Split() 方法不起作用的信心有多大,而不是 VSTS 发布管道中的某些东西干扰了拆分结果?
  • 由于您使用相同的变量名称$WebAppName,您最终会得到一个您希望它是字符串的数组。使用类似foreach ($appName in $WebAppName.Split(',')) {...}
  • 我尝试了 foreach ($appName in $WebAppName.Split(',')) ,但仍然没有拆分字符串
  • @kartikiyer - 如果它真的没有在逗号上分割......那么你在字符串中没有逗号。 [grin] 确认它确实有一个逗号...,然后确认某些 ELSE 将结果数组强制转换为字符串。 ///// 您可能需要发布正在运行的实际代码以获得更多详细信息。
  • 添加了我的 powershell 脚本,我试图将 webappname 作为发布管道变量的输入并拆分它们

标签: powershell


【解决方案1】:

以下与 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

【讨论】:

  • 如果我们从发布管道变量中获取输入而不是在内联 powershell 中对其进行硬编码,那么同样的方法将不起作用。尝试一次
  • 尝试 $WebAppName = '$(WebAppName)' 使用 VSTS 变量。更新了答案。
  • 我仍然没有得到正确的输出,而不是内联脚本,你可以通过在 azure powershell 任务中配置 powershell 脚本来尝试同样的操作
  • 我在 PowerShell 脚本和 Azure PowerShell 脚本任务中都进行了测试。它工作正常。
【解决方案2】:

有问题的行是这一行:

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

您正在将拆分结果重新分配给已在参数列表中声明为字符串的同一变量 $WebAppName。所以Split的数组结果会被转换成字符串,不再是数组了。

解决方法是将拆分的结果赋给一个新的变量:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-29
    • 1970-01-01
    • 2015-05-28
    相关资源
    最近更新 更多