Need to get Vm names and Ip addresses using powershell and the output should come in notepad

Mokireddy, Reethika 0 Reputation points
2023-03-16T10:12:21.26+00:00

I need to get Virtual machine names and ip addresses using powershell and the output should come in notepad

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
{count} votes

1 answer

Sort by: Most helpful
  1. shiva patpi 13,376 Reputation points Microsoft Employee Moderator
    2023-03-16T17:55:13.0733333+00:00

    Hello @Reethika Mokireddy ,

    Please use below powershell script to display the VMName & Corresponding PublicIP address:

    $allvms = Get-AzureRMVM
    foreach($vm in $allvms){
        $vm = Get-AzureRmVM -ResourceGroupName $vm.ResourceGroupName -Name $vm.Name
        $nic = $vm.NetworkProfile.NetworkInterfaces[0].Id.Split('/') | select -Last 1
        $publicIpName =  (Get-AzureRmNetworkInterface -ResourceGroupName $vm.ResourceGroupName
     -Name $nic).IpConfigurations.PublicIpAddress.Id.Split('/') | select -Last 1
        $publicIpAddress = (Get-AzureRmPublicIpAddress -ResourceGroupName $vm.ResourceGroupName
     -Name $publicIpName).IpAddress
        Write-Output $vm.Name $publicIpAddress >> "data.txt"
    }
    
    

    Sample test output:

    VM Name

    PublicIP

    User's image

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.