0

I have created the PowerShell script for the renew registration key of the WVD host pool. And I have been uploaded the script to Azure Repo.

The script has been tested and it was work.

I have created the variable group already but I don't know how can apply them to the script.

My problem is how to change the script variable by the Azure DevOps Variable group?

Here is my PowerShell script renew_token.ps1 below:

$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

My Azure Pipeline:

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'
chris1126
  • 27
  • 1
  • 3
  • 9
  • 2
    Oh, I have fixed by myself. i just using CmdletBinding() inside PowerShell script and config ScriptArguments from azure yaml pipeline – chris1126 Mar 03 '21 at 07:43
  • Consider adding an answer with what you've discovered - formatted changes to the script and the pipeline, and then marking it answered! – WaitingForGuacamole Mar 03 '21 at 15:50

1 Answers1

0

Thanks for WaitingForGuacamole reminded me

I'm posting the solution here!

You can reference here if you have the PowerShell script run on Azure DevOps.

my PowerShell script renew_token.ps1 below:

[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

My Azure Pipeline:

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
chris1126
  • 27
  • 1
  • 3
  • 9