![](/img/trans.png)
[英]VSTS - Powershell script to add variable to release environment not working
[英]Powershell Split function not working in VSTS Release pipeline
我編寫了一個 powershell 腳本,它將多個 webapps(逗號分隔)作為輸入。 我正在使用 powershell 拆分功能拆分這些 webapp,並通過使用 for-each 循環遍歷它們中的每一個來配置 webapp。 在 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
有問題的線路是這樣的:
$WebAppName = $WebAppName.Split(',')
您正在將 split 的結果重新分配給已在參數列表中聲明為字符串的同一變量 $WebAppName。 所以 Split 的數組結果將被轉換為字符串,而不是數組。
解決辦法是將split的結果賦值給一個新的變量:
$WebAppNameSplit = $WebAppName.Split(',')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.