【问题标题】:How to export Virtual Machine Names and IP Addresses from within multiple azure subscriptions如何从多个 Azure 订阅中导出虚拟机名称和 IP 地址
【发布时间】:2019-07-05 22:02:49
【问题描述】:

我已经能够使用以下脚本从单个 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"

但是,我尝试使用以下方法从具有多个订阅的 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"

有人可以帮忙吗?

【问题讨论】:

    标签: azure virtual-machine ip-address subscription


    【解决方案1】:

    我认为您需要在 Select-AzSubscription 之后立即移动 Get-AzVm?因为现在你只能获得一次虚拟机(默认订阅)

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

    【讨论】:

    • 我有另一个类似性质的问题,我认为我的语法不正确
    猜你喜欢
    • 1970-01-01
    • 2023-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-26
    相关资源
    最近更新 更多