Ayanda ezekiel
Ayanda ezekiel

Reputation: 43

I need to get the ResourceGroup and ResourceID of the Associated Azure VM of my DSC node

I have two nodes registered in resourcegroup1 when I run Get-AzAutomationDSCNode.

Both nodes have the same name "Node1" but belong to different resource groups.

Node1 - resourcegroup1
Node1 - resourcegroup2

since they are both registered under the same resourcegroup, how can I get the associated vm of the nodes using powershell so i can know the right one to delete.

The properties of the Get-AzAutomationDscNode does not contain VmResourceID

Upvotes: 0

Views: 371

Answers (1)

Venkat V
Venkat V

Reputation: 7820

how can I get the associated vm of the nodes using powershell

Here is the PowerShell script to get the VM names by using automation account nodes.

    $resourceGroupNames = @("RG1", "RG2")
    $automationAccountNames = @("Automate-WUS2", "MM")
    for ($i = 0; $i -lt $resourceGroupNames.Length; $i++) {
        $resourceGroupName = $resourceGroupNames[$i]
        $automationAccountName = $automationAccountNames[$i]
    
        $dscNodes = Get-AzAutomationDscNode -ResourceGroupName $resourceGroupName -AutomationAccountName $automationAccountName
    
        foreach ($dscNode in $dscNodes) {
            Write-Host "DSC Node Name: $($dscNode.Name), Resource Group: $resourceGroupName, Automation Account: $automationAccountName"
        }
    }

Output:

enter image description here

Updated Script:

You can use below PowerShell script to retrieve the VM resource ID from the DSC nodes configuration.

    $resourceGroupName = "RG_Name"
    $automationAccountName = "Automation_Account"
    
    $dscNodes = Get-AzAutomationDscNode -ResourceGroupName $resourceGroupName -AutomationAccountName $automationAccountName | Select-Object -Unique
    
    foreach ($dscNode in $dscNodes) {
        Write-Host "DSC Node Name in Automation Account : $($dscNode.Name)"
        
        $VMs = Get-AzResource -ResourceType "Microsoft.Compute/virtualMachines" -Name $dscNode.Name
        
        foreach ($VM in $VMs) {
            Write-Host "Name              : $($VM.Name)"
            Write-Host "ResourceGroupName : $($VM.ResourceGroupName)"
            Write-Host "ResourceId        : $($VM.ResourceId)"
            Write-Host "Location          : $($VM.Location)"
            Write-Host ""
        }
    }

Output:

enter image description here

Upvotes: 0

Related Questions