如何将 Azure DevOps 的变量替换为 PowerShell 的变量?
How can instead of the variable of Azure DevOps to variable of PowerShell?
我已经为 WVD 主机池的更新注册密钥创建了 PowerShell 脚本。我已经将脚本上传到 Azure Repo。
脚本已经过测试,可以正常工作。
我已经创建了变量组,但我不知道如何将它们应用到脚本中。
我的问题是如何通过 Azure DevOps 变量组更改脚本变量?
下面是我的 PowerShell 脚本 renew_token.ps1:
$subscriptionID = "xxx-xxxx-xxx"
$resourceGroupName = "rg-wvd"
$hostPoolName = "hostpool-01"
$Registered = Get-AzWvdRegistrationInfo -SubscriptionId "$subscriptionID" -ResourceGroupName "$resourceGroupName" -HostPoolName $hostPoolName
$registrationTokenValidFor = "null"
if (-not(-Not $Registered.Token)){$registrationTokenValidFor = (NEW-TIMESPAN -Start (get-date) -End $Registered.ExpirationTime | select Days,Hours,Minutes,Seconds)}
Write-Host "Token is valid for:$registrationTokenValidFor"
if ((-Not $Registered.Token) -or ($Registered.ExpirationTime -le (get-date)))
{
$Registered = New-AzWvdRegistrationInfo -SubscriptionId $subscriptionID -ResourceGroupName $resourceGroupName -HostPoolName $hostPoolName -ExpirationTime (Get-Date).AddHours(4) -ErrorAction SilentlyContinue
$token = $Registered.Token
$RdsRegistrationInfotoken = "Create new Token:$token"
}
Write-Host $RdsRegistrationInfotoken
我的 Azure 管道:
trigger:
- main
variables:
- group: Renew-Registration-Key
pool:
vmImage: windows-latest
steps:
- task: AzurePowerShell@5
inputs:
azureSubscription: 'xxxxx-xxxxx-xxxx-xxxx'
ScriptType: 'FilePath'
ScriptPath: './renew_token.ps1'
azurePowerShellVersion: 'LatestVersion'
感谢 WaitingForGuacamole 提醒我
我在这里发布解决方案!
如果您在 Azure DevOps 上有 PowerShell 脚本运行,您可以参考这里。
我的 PowerShell 脚本 renew_token.ps1 下面:
[CmdletBinding()]
param (
$subscriptionID,
$resourceGroupName,
$hostPoolName
)
$Registered = Get-AzWvdRegistrationInfo -SubscriptionId "$subscriptionID" -ResourceGroupName "$resourceGroupName" -HostPoolName $hostPoolName
$registrationTokenValidFor = "null"
if (-not(-Not $Registered.Token)){$registrationTokenValidFor = (NEW-TIMESPAN -Start (get-date) -End $Registered.ExpirationTime | select Days,Hours,Minutes,Seconds)}
Write-Host "Token is valid for:$registrationTokenValidFor"
if ((-Not $Registered.Token) -or ($Registered.ExpirationTime -le (get-date)))
{
$Registered = New-AzWvdRegistrationInfo -SubscriptionId $subscriptionID -ResourceGroupName $resourceGroupName -HostPoolName $hostPoolName -ExpirationTime (Get-Date).AddHours(648) -ErrorAction SilentlyContinue
$token = $Registered.Token
$RdsRegistrationInfotoken = "Create new Token:$token"
}
Write-Host $RdsRegistrationInfotoken
我的 Azure 管道:
trigger:
- main
variables:
- group: Renew-Registration-Key
pool:
vmImage: windows-latest
steps:
- task: AzurePowerShell@5
inputs:
azureSubscription: 'xxxx-xxxx-xxxx-xxxx'
ScriptType: 'InlineScript'
Inline: 'Remove-AzWvdRegistrationInfo -ResourceGroupName $(resourceGroup) -HostPoolName $(hostPoolName)'
azurePowerShellVersion: 'LatestVersion'
- task: AzurePowerShell@5
inputs:
azureSubscription: 'xxxx-xxxx-xxxx-xxxx'
ScriptType: 'FilePath'
ScriptPath: './renew_token.ps1'
ScriptArguments: '-subscriptionID $(subscriptionID) -resourceGroupName $(resourceGroup) -hostPoolName $(hostPoolName)'
azurePowerShellVersion: 'LatestVersion'
schedules:
- cron: "0 0 27 * *"
displayName: Mothnly midnight build
branches:
include:
- main
我已经为 WVD 主机池的更新注册密钥创建了 PowerShell 脚本。我已经将脚本上传到 Azure Repo。
脚本已经过测试,可以正常工作。
我已经创建了变量组,但我不知道如何将它们应用到脚本中。
我的问题是如何通过 Azure DevOps 变量组更改脚本变量?
下面是我的 PowerShell 脚本 renew_token.ps1:
$subscriptionID = "xxx-xxxx-xxx"
$resourceGroupName = "rg-wvd"
$hostPoolName = "hostpool-01"
$Registered = Get-AzWvdRegistrationInfo -SubscriptionId "$subscriptionID" -ResourceGroupName "$resourceGroupName" -HostPoolName $hostPoolName
$registrationTokenValidFor = "null"
if (-not(-Not $Registered.Token)){$registrationTokenValidFor = (NEW-TIMESPAN -Start (get-date) -End $Registered.ExpirationTime | select Days,Hours,Minutes,Seconds)}
Write-Host "Token is valid for:$registrationTokenValidFor"
if ((-Not $Registered.Token) -or ($Registered.ExpirationTime -le (get-date)))
{
$Registered = New-AzWvdRegistrationInfo -SubscriptionId $subscriptionID -ResourceGroupName $resourceGroupName -HostPoolName $hostPoolName -ExpirationTime (Get-Date).AddHours(4) -ErrorAction SilentlyContinue
$token = $Registered.Token
$RdsRegistrationInfotoken = "Create new Token:$token"
}
Write-Host $RdsRegistrationInfotoken
我的 Azure 管道:
trigger:
- main
variables:
- group: Renew-Registration-Key
pool:
vmImage: windows-latest
steps:
- task: AzurePowerShell@5
inputs:
azureSubscription: 'xxxxx-xxxxx-xxxx-xxxx'
ScriptType: 'FilePath'
ScriptPath: './renew_token.ps1'
azurePowerShellVersion: 'LatestVersion'
感谢 WaitingForGuacamole 提醒我
我在这里发布解决方案!
如果您在 Azure DevOps 上有 PowerShell 脚本运行,您可以参考这里。
我的 PowerShell 脚本 renew_token.ps1 下面:
[CmdletBinding()]
param (
$subscriptionID,
$resourceGroupName,
$hostPoolName
)
$Registered = Get-AzWvdRegistrationInfo -SubscriptionId "$subscriptionID" -ResourceGroupName "$resourceGroupName" -HostPoolName $hostPoolName
$registrationTokenValidFor = "null"
if (-not(-Not $Registered.Token)){$registrationTokenValidFor = (NEW-TIMESPAN -Start (get-date) -End $Registered.ExpirationTime | select Days,Hours,Minutes,Seconds)}
Write-Host "Token is valid for:$registrationTokenValidFor"
if ((-Not $Registered.Token) -or ($Registered.ExpirationTime -le (get-date)))
{
$Registered = New-AzWvdRegistrationInfo -SubscriptionId $subscriptionID -ResourceGroupName $resourceGroupName -HostPoolName $hostPoolName -ExpirationTime (Get-Date).AddHours(648) -ErrorAction SilentlyContinue
$token = $Registered.Token
$RdsRegistrationInfotoken = "Create new Token:$token"
}
Write-Host $RdsRegistrationInfotoken
我的 Azure 管道:
trigger:
- main
variables:
- group: Renew-Registration-Key
pool:
vmImage: windows-latest
steps:
- task: AzurePowerShell@5
inputs:
azureSubscription: 'xxxx-xxxx-xxxx-xxxx'
ScriptType: 'InlineScript'
Inline: 'Remove-AzWvdRegistrationInfo -ResourceGroupName $(resourceGroup) -HostPoolName $(hostPoolName)'
azurePowerShellVersion: 'LatestVersion'
- task: AzurePowerShell@5
inputs:
azureSubscription: 'xxxx-xxxx-xxxx-xxxx'
ScriptType: 'FilePath'
ScriptPath: './renew_token.ps1'
ScriptArguments: '-subscriptionID $(subscriptionID) -resourceGroupName $(resourceGroup) -hostPoolName $(hostPoolName)'
azurePowerShellVersion: 'LatestVersion'
schedules:
- cron: "0 0 27 * *"
displayName: Mothnly midnight build
branches:
include:
- main