简体   繁体   English

如何从多个Azure订阅中导出虚拟机名称和IP地址

[英]How to export Virtual Machine Names and IP Addresses from within multiple azure subscriptions

I have been able to export out VM Names and IP Addresses from within a single azure subscription using the following script. 我已经可以使用以下脚本从单个Azure订阅中导出VM名称和IP地址。

$report = @()
$vms = get-azvm
$nics = get-aznetworkinterface | ?{ $_.VirtualMachine -NE $null}

foreach($nic in $nics)
{
    $info = "" | Select VmName, ResourceGroupName, HostName, IpAddress
    $vm = $vms | ? -Property Id -eq $nic.VirtualMachine.id
    $info.VMName = $vm.Name
    $info.ResourceGroupName = $vm.ResourceGroupName
    $info.IpAddress = $nic.IpConfigurations.PrivateIpAddress
    $info.HostName = $vm.OSProfile.ComputerName
    $report+=$info
}
$report | Export-Csv -Path "c:\Azure\VMIPs.csv"

However I have tried using the below to export the same data from an azure account that has multiple subscriptions but all I get is a blank CSV file. 但是我尝试使用下面的方法从具有多个订阅的azure帐户中导出相同的数据,但是我得到的只是一个空白CSV文件。

$report = @()
$vms = get-azvm
$azureSubs = get-azsubscription
$azureSubs.foreach{
    Select-AzSubscription $_ # << change active subscription
$nics = get-aznetworkinterface | ?{ $_.VirtualMachine -NE $null}

foreach($nic in $nics)
{
    $info = "" | Select VmName, ResourceGroupName, HostName, IpAddress
    $vm = $vms | ? -Property Id -eq $nic.VirtualMachine.id
    $info.VMName = $vm.Name
    $info.ResourceGroupName = $vm.ResourceGroupName
    $info.IpAddress = $nic.IpConfigurations.PrivateIpAddress
    $info.HostName = $vm.OSProfile.ComputerName
    $report+=$info
    }
}
$report | Export-Csv -Path "c:\Azure\VMIPs.csv"

Can anyone assist? 有人可以协助吗?

I think you need to move Get-AzVm right after the Select-AzSubscription? 我认为您需要在Select-AzSubscription之后立即移动Get-AzVm吗? because right now you only get vms once (in default subscription) 因为现在您只获得一次虚拟机(默认订阅)

$azureSubs.foreach{
    Select-AzSubscription $_ # << change active subscription
    $nics = get-aznetworkinterface | ?{ $_.VirtualMachine -NE $null}
    $vms = get-azvm
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Microsoft Azure 是否保留虚拟机访问的 IP 地址的日志? - Does Microsoft Azure keep a log of IP Addresses visited by a Virtual Machine? 如何从多个订阅的Azure中导出VMData - How to export VMData from azure across multiple subscriptions 如何从 azure 中的虚拟机 ping 负载均衡器 ip 地址 - How to ping the load balancer ip address from Virtual machine in azure 无法从某个 IP SSH 到 Azure 虚拟机 - Cannot SSH to an Azure Virtual Machine from a certain IP 如何在 Terraform 中为 Azure 负载均衡器添加具有虚拟机 IP 的后端 - How to add a backend with virtual machine IP for Azure Load Balancer in Terraform 如何针对特定范围的公共IP限制Azure虚拟机 - How to Restrict Azure Virtual Machine for Specific Range of Public IP 如何在Windows Azure虚拟机上强制进行IP更改? - How to force ip change on a Windows Azure Virtual machine? Azure上的Linux虚拟机 - 如何应用IP限制 - Linux Virtual Machine at Azure - how to apply IP restriction 无法从工作流中检索 Azure 虚拟机状态 - Unable to retrieve Azure Virtual Machine status from within a workflow 如何将文件从本地计算机传输到Azure容器内的虚拟文件夹(使用blobxfer) - How to transfer files from local machine to virtual folders within an Azure container (using blobxfer)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM