Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell get Azure VM name from private IP

I want to get azure VM name from IP address, I tried following to get NIC name from IP and find to which VM that NIC is attached.

Get-AzureRmNetworkInterface | ForEach { $Interface = $_.Name; $IPs = $_ | Get-AzureRmNetworkInterfaceIpConfig | Select PrivateIPAddress; Write-Host $Interface $IPs.PrivateIPAddress }

Is there a better way to get VM name directly using VM private ?

like image 476
roy Avatar asked Jan 30 '17 22:01

roy


1 Answers

Is there a better way to get VM name directly using VM private ?

Do you mean use VM's private IP address to get the VM's name? If I understand it correctly, we can use this PowerShell to get the VM's name:

PS C:> get-azurermvm

ResourceGroupName  Name Location      VmSize OsType     NIC ProvisioningState
-----------------  ---- --------      ------ ------     --- -----------------
RGNAME            jason   eastus Standard_A1  Linux jason66         Succeeded

PS C:\> $a = ((Get-AzureRmNetworkInterface | ?{$_.IpConfigurations.PrivateIpAddress -eq '10.0.0.4'}).VirtualMachine).ID
PS C:\> $vmname = ($a -split '/') | select -Last 1
PS C:\> $vmname
jason
like image 140
Jason Ye Avatar answered Sep 17 '22 19:09

Jason Ye