gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 02:53:24 25 4
gpt4 key购买 nike

我已经能够使用以下脚本从单个 Azure 订阅中导出虚拟机名称和 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"

有人可以帮忙吗?

最佳答案

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

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

关于azure - 如何从多个 Azure 订阅中导出虚拟机名称和 IP 地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54647012/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com