问题描述
我想检索其中所有名称为" server "的所有虚拟机以及这些计算机的IP地址的列表.下面的方法非常适合获取名称,但是如何将每个虚拟机的私有IP地址添加到变量中?
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. The below works great for getting the names, however how can i add the private ip address of each vm into the variable?
$machines = Get-AzureRmVM -ResourceGroupName $resourcegroup | where {$_.Name -like server" + "*"}
推荐答案
Get-AzureRmVM
Cmdlet无法获取任何VM的IP地址信息.
Get-AzureRmVM
Cmdlet doesn't get the IP Address info of any VM.
您必须从 Get-AzureRmVM
获取NetworkInterface名称,然后将该值传递给 Get-AzureRmNetworkInterface
Cmdlet以获得专用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 -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}
}
或者您可以直接调用 Get-AzureRmNetworkInterface
并使用VirtualMachine.ID属性过滤VM
Or You can directly call the Get-AzureRmNetworkInterface
and filter VM with the VirtualMachine.ID property
$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}}
这篇关于我如何获取变量的ip的ip地址和vm的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!