Question about Exercise - Create an Azure Resource using scripts in Azure PowerShell

Chung Li Lam (MLCSU) 21 Reputation points

I have been following all the steps of the exercise but I am stuck at getting the IP address by $vm | Get-AzPublicIpAddress How may I get the IPAddress successfully?
The following is how I got the error, what did I do wrong? Thank you.

   PS /home/chung> New-AzVm -ResourceGroupName learn-e69c78ce-6aa1-4cab-bcd3-2e984a6ad302 -Name "testvm-eus-01" -Credential (Get-Credential) -Location "East US" -Image UbuntuLTS -OpenPorts 22  
   PowerShell credential request  
   Enter your credentials.  
   User: VMUser1  
   Password for user VMUser1: ********  
   No Size value has been provided. The VM will be created with the default size Standard_D2s_v3.  
   ResourceGroupName        : learn-e69c78ce-6aa1-4cab-bcd3-2e984a6ad302  
   Id                       : /subscriptions/4d43e8c9-4583-4435-a75d-5fa0b9865f65/resourceGroups/learn-e69c78ce-6aa1-4cab-bcd3-2e984a6ad302/providers/Microsoft.Compute/virtualMachin  
   VmId                     : 7668d72a-b308-4d88-9027-606c2b2affe3  
   Name                     : testvm-eus-01  
   Type                     : Microsoft.Compute/virtualMachines  
   Location                 : eastus  
   Tags                     : {}  
   HardwareProfile          : {VmSize}  
   NetworkProfile           : {NetworkInterfaces}  
   OSProfile                : {ComputerName, AdminUsername, LinuxConfiguration, Secrets,  
   AllowExtensionOperations, RequireGuestProvisionSignal}  
   ProvisioningState        : Succeeded  
   StorageProfile           : {ImageReference, OsDisk, DataDisks}  
   FullyQualifiedDomainName : testvm-eus-01-298d32.East  
   PS /home/chung> $vm = (Get-AzVM -Name "testvm-eus-01" -ResourceGroupName learn-e69c78ce-6aa1-4cab-bcd3-2e984a6ad302)  
   PS /home/chung> $vm | Get-AzPublicIpAddress  
   Get-AzPublicIpAddress: The Resource 'Microsoft.Network/publicIPAddresses/testvm-eus-01' under resource group 'learn-e69c78ce-6aa1-4cab-bcd3-2e984a6ad302' was not found. For more details please go to  
   StatusCode: 404  
   ReasonPhrase: Not Found  
   ErrorCode: ResourceNotFound  
   ErrorMessage: The Resource 'Microsoft.Network/publicIPAddresses/testvm-eus-01' under resource group 'learn-e69c78ce-6aa1-4cab-bcd3-2e984a6ad302' was not found. For more details please go to  
   OperationID : 7f32c413-1a9e-49b1-bc16-7f35634a7845  
Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
5,970 questions
{count} votes

Accepted answer
  1. srbhatta-MSFT 8,526 Reputation points Microsoft Employee

    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.



    Please don't forget to Accept as Answer and Upvote and if you think my response was helpful.

    2 people found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful