Madhu
Madhu

Reputation: 11

So, I have to create registry token and update the values on each vm like manually in the azure portal

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:\SOFTWARE\Microsoft\RDInfraAgent"
    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 host Pool when I do it's updating. I'm having trouble in this part as I'm new to azure. This is from azure automation runbook in the portal.

Upvotes: 0

Views: 130

Answers (1)

Jahnavi
Jahnavi

Reputation: 8018

Unable to update the registry values in the vm but in the host Pool when I do it's updating: -

To update the registry values in the virtual machine from an automation account, you need to assign the Virtual Machine Contributor role to the managed identity of an automation account as shown below.

Assigning it grants you a write access to the virtual machine and so that you can do update or any modification operations according to your requirement.

enter image description here

So once I have added the below shown roles under the path Automation account -> Identity -> Enable system Managed Identity -> Azure role assignments, the same code given by you was successfully executed.

enter image description here

connect-Azaccount -identity
$ResourceGroupName = "Jahnavi"
$HostPoolName = "enewpool"
$hostpool = Get-AzWvdHostPool -Name "enewpool" -ResourceGroupName "Jahnavi"
 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
}

    }
Write-Output $RegistrationToken.Token
$VMName = "newwvm"
$Params = @{
    RegistryPath = "HKLM:\SOFTWARE\Microsoft\RDInfraAgent"
    RegistryName = $RegistrationToken
    NewRegistryValueData = $RegistrationToken.Token
}
# 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
            Write-Output "Registry key '$RegistryPath' created successfully."
        } catch {
            Write-Error "Failed to create registry key '$RegistryPath': $_"
        }
    }

    # Update registry value
    Set-ItemProperty -Path $RegistryPath -Name $RegistryName -Value $NewRegistryValueData
    Write-Output '', "Registry value updated"
}
Invoke-AzVMRunCommand -ResourceGroupName $ResourceGroupName -VMName $VMName -CommandId 'RunPowerShellScript' -ScriptPath $ScriptBlock -Parameter $Params

Output:

enter image description here

Upvotes: 0

Related Questions