因此,我必须创建注册表令牌并更新每个虚拟机上的值,就像在 azure 门户中手动那样

问题描述 投票:0回答:1
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

我无法更新虚拟机中的注册表值,但更新时无法更新主机池中的注册表值。因为我是天蓝色的新手,所以我在这部分遇到了麻烦。这是来自门户中的 azure 自动化运行手册。

azure automation azure-powershell azure-runbook
1个回答
0
投票

无法更新虚拟机中的注册表值,但更新时主机池中的注册表值:-

要从自动化帐户更新虚拟机中的注册表值,您需要将

Virtual Machine Contributor
角色分配给自动化帐户的托管标识,如下所示。

分配它会授予您对虚拟机的写入权限,以便您可以根据您的要求执行更新或任何修改操作。

enter image description here

所以一旦我在路径下添加了下面显示的角色

Automation account -> Identity -> Enable system Managed Identity -> Azure role assignments
,您给出的相同代码已成功执行。

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

输出:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.