Hello @Chung Li Lam (MLCSU) ,
Thanks for reaching out to Microsoft QnA. Happy to answer your question.
As you mentioned, you want to fetch the public ip address of the VM. However, you will not be able to directly fetch it with the Get-AzPublicIpAddress cmdlet. Please run the below powershell script to do the same.
$vm = Get-AzVM -ResourceGroupName lab -Name win-lab
$nic = $vm.NetworkProfile.NetworkInterfaces[0].Id.Split('/') | select -Last 1
$publicIpName = (Get-AzNetworkInterface -ResourceGroupName lab -Name $nic).IpConfigurations.PublicIpAddress.Id.Split('/') | select -Last 1
$publicIpAddress = (Get-AzPublicIpAddress -ResourceGroupName lab -Name $publicIpName).IpAddress
Write-Output $publicIpAddress
I hope this helps.
References:
https://learn.microsoft.com/en-us/powershell/module/az.network/get-azpublicipaddress?view=azps-7.2.0
azure-powershell-az-module-get-public-ip-address
-----------------------------------
Please don't forget to Accept as Answer and Upvote and if you think my response was helpful.