'Why Get-AzPublicIPAddress works with name of resource but dont work with parameter

In my script i'm using the Get-AzPublicIpAddress cmdlet, and it's working perfectly with a VM name:

(Get-AzPublicIpAddress -ResourceName "vm-ubuntu-test2311").IpAddress

But it doesn't work with a variable as the parameter argument:

$vmname = "vm-ubuntu-test2311"
(Get-AzPublicIpAddress -ResourceName $vmname ).IpAddress

It passes but the value is empty



Solution 1:[1]

The way to access and get the VM public IP you can follow the below way:

Here, I am using Resource Group Name and VM name to fetch the exact VM Resource Public IP to Avoid the conflict.

$name = "<Your VM Name>"
$rname = "<Your Resource Group Name>"
Get-AzPublicIpAddress -ResourceGroupName $rname -Name $name | Select-Object {$_.IpAddress}

Results:

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 DelliganeshS-MT