[英]how do i get the ip addresses and names of vm in avariable
I want to retrieve a list of all the vms which have the name " server " in them and also the ip addresses of these machines. 我想检索所有名称为“ server ”的虚拟机以及这些计算机的IP地址的列表。 The below works great for getting the names, however how can i add the private ip address of each vm into the variable? 下面非常适合获取名称,但是如何将每个虚拟机的私有IP地址添加到变量中?
$machines = Get-AzureRmVM -ResourceGroupName $resourcegroup | where {$_.Name -like server" + "*"}
Get-AzureRmVM
Cmdlet doesn't get the IP Address info of any VM. Get-AzureRmVM
Cmdlet无法获取任何VM的IP地址信息。
You have to get the NetworkInterface Name from the Get-AzureRmVM
and then pass the value to Get-AzureRmNetworkInterface
Cmdlet to get the Private IP . 您必须从Get-AzureRmVM
NetworkInterface名称,然后将该值传递给Get-AzureRmNetworkInterface
Cmdlet以获得专用IP。
Get-AzureRmVM -ResourceGroupName TestRG | Where-Object {$_.Name -like '*server*'} | ForEach-Object {
$NIC = $_.NetworkProfile.NetworkInterfaces.id -replace '^.*/'
$RGName = $_.NetworkProfile.NetworkInterfaces.id -replace '^.*resourceGroups/(.*)/providers.*','$1'
$IP = (Get-AzureRmNetworkInterface -Name $NIC -ResourceGroupName $RGName).IpConfigurations.PrivateIpAddress
[PSCustomObject]@{VMName = $_.Name ; PrivateIpAddress = $IP}
}
Or You can directly call the Get-AzureRmNetworkInterface
and filter VM with the VirtualMachine.ID property 或者,您可以直接调用Get-AzureRmNetworkInterface
并使用VirtualMachine.ID属性过滤VM。
$Resourcegroup = 'TestRG'; $VmName = 'server'
Get-AzureRmNetworkInterface | Where-Object { $_.VirtualMachine.ID -match "^.*resourceGroups/$Resourcegroup.*virtualMachines/.*$VmName.*" } |
Select-Object @{L='VMName';ex = {$_.VirtualMachine.Id -replace '^.*/'}}, @{L='PrivateIpAddress';ex = {$_.IpConfigurations.PrivateIpAddress}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.