gpt4 book ai didi

powershell - 通过 Powershell 获取与 Azure ARM VM 的 NIC 组关联的当前 IP 地址

转载 作者:行者123 更新时间:2023-12-02 22:10:16 24 4
gpt4 key购买 nike

我正在尝试编写一些 Powershell 来获取 Azure ARM 虚拟机(非经典)及其 NIC 当前关联的 IP 地址的列表。

在经典中,这是 VM 对象的一部分,但在 ARM 中,它是一个单独的对象,我正在努力让 Powershell 以我想要的方式工作。

我有以下代码段:

$nic = (((Get-AzureRmVM).NetworkProfile).NetworkInterfaces).Id
ForEach ($i in $nic) {
$nicname = $i.substring($i.LastIndexOf("/")+1)
Get-AzureRmNetworkInterface -Name $nicname -ResourceGroupName RGTEST | Get-AzureRmNetworkInterfaceIpConfig | select-object PrivateIpAddress,PrivateIpAllocationMethod
}

这有效,但仅适用于指定资源组“RGTEST”中的虚拟机。

似乎 Get-AzureRmNetworkInterface 只能在传入 NIC 名称和 ResourceGroupName 时才能工作,但我似乎无法从要传入的 VM 获取 RGname。

可能真的很简单,但我正在努力解决它!

最佳答案

我使用此代码获取我的所有 ARM VM、它们的私有(private) IP 地址和分配方法,它适用于跨资源组。

$vms = get-azurermvm
$nics = get-azurermnetworkinterface | where VirtualMachine -NE $null #skip Nics with no VM

foreach($nic in $nics)
{
$vm = $vms | where-object -Property Id -EQ $nic.VirtualMachine.id
$prv = $nic.IpConfigurations | select-object -ExpandProperty PrivateIpAddress
$alloc = $nic.IpConfigurations | select-object -ExpandProperty PrivateIpAllocationMethod
Write-Output "$($vm.Name) : $prv , $alloc"
}

示例输出:
产品:10.0.0.4,静态
stagedc:10.1.0.4,静态

关于powershell - 通过 Powershell 获取与 Azure ARM VM 的 NIC 组关联的当前 IP 地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38054573/

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