简体   繁体   English

我如何获取变量的vm的IP地址和名称

[英]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.

相关问题 如何获取LAN上的活动IP地址,MAC地址和NetBIOS名称列表? - How do I get a list of the active IP-addresses, MAC-addresses and NetBIOS names on the LAN? 如何获得我的Azure VM的公共IP? - How do I get a public IP for my Azure VM? 在列表中输出时,如何从 Get.NETTCPConnection 中排除 IP 地址? - How do i exclude IP addresses from Get-NETTCPConnection when outputting it in a list? 是否有任何 Powershell 脚本,或者如果虚拟机有多个 ip 地址,我如何修改此脚本以将多个 ip 作为 csv 文件导入? - Is there any Powershell script or how can i modify this script to import multiple ips as a csv file if a vm has multiple ip addresses? 通过 Powershell 获取与 Azure ARM VM 的一组 NIC 关联的当前 IP 地址 - Get current IP addresses associated with an Azure ARM VM's set of NICs via Powershell 如何在Powershell中从Azure获取VM映像? - How do I get a VM Image from Azure in Powershell? 如何获取资源组中的 Vm 列表 - How do i get a list of Vm in a recourse group 如何获得有关在Azure VM上执行DSC的反馈? - How do I get feedback about DSC execution on an Azure VM? 如何从防火墙规则中获取 IP 地址列表? - How to get the list of IP addresses from a firewall rule? 如何从SCVMM 2008 R2获取IP地址 - How to get IP addresses from SCVMM 2008 R2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM