try {
"Logging in to Azure..."
Connect-AzAccount -Identity
}
catch {
Write-Error -Message $_.Exception
throw $_.Exception
}
# Set the subscription (if not already set)
Set-AzContext -SubscriptionId "xxxxxxxxxxxxxxxx"
$ResourceGroupName = "xxxxx1"
$HostPoolName = "wpxxxxxxxxx"
# Check if the host pool exists
$Hostpool = Get-AzWvdHostPool -Name $HostPoolName -ResourceGroupName $ResourceGroupName -ErrorAction SilentlyContinue
if (!$Hostpool) {
Write-Host '', "Hostpool '$($HostPoolName)' doesn't exist. It will be created by TF"
} else {
try {
# Create token
$RegistrationToken = New-AzWvdRegistrationInfo -ResourceGroupName $ResourceGroupName `
-HostPoolName $HostPoolName `
-ExpirationTime (Get-Date).ToUniversalTime().AddHours(2).ToString('yyyy-MM-ddTHH:mm:ss.fffffffZ')
Write-Host '', "Token created"
Write-Output $RegistrationToken
} catch {
throw $_.Exception
}
}
$VMName = "001"
# Define the script block for registry update
$ScriptBlock = {
param (
[string]$RegistryPath,
[string]$RegistryName,
[string]$NewRegistryValueData
)
# Check if registry key exists, create if not
if (-not (Test-Path $RegistryPath)) {
try {
New-Item -Path $RegistryPath -Force -ErrorAction Stop
Write-Output "Registry key '$RegistryPath' created successfully."
} catch {
Write-Error "Failed to create registry key '$RegistryPath': $_"
}
}
# Update registry value
try {
Set-ItemProperty -Path $RegistryPath -Name $RegistryName -Value $NewRegistryValueData -ErrorAction Stop
Write-Output '', "Registry value updated"
} catch {
Write-Error -Message "Failed to update registry value: $_"
}
}
# Define parameters for the script block
$Params = @{
RegistryPath = "HKLM:SOFTWAREMicrosoftRDInfraAgent"
RegistryName = $RegistrationToken
NewRegistryValueData = $RegistrationToken.Token
}
# Invoke the script block on the target VM
Invoke-AzVMRunCommand -ResourceGroupName $ResourceGroupName -VMName $VMName -CommandId 'RunPowerShellScript' -ScriptPath $ScriptBlock -Parameter $Params
I’m unable to update the registry values in the vm but in the hostpool when I do it’s updating. Im having trouble in this part as i’m new to azure. This is from azure automation runbook in the portal
I’m unable to update the registry values in the vm but in the hostpool when I do it’s updating. Im having trouble in this part as i’m new to azure. This is from azure automation runbook in the portal.