简体   繁体   English

Azure Powershell (Az模块) 公开IP地址

[英]Azure Powershell (Az module) get public IP address

With the new Az module for Azure, does anyone have the syntax for getting the public IP address of an Azure VM using the name?对于 Azure 的新 Az 模块,是否有人知道使用该名称获取 Azure VM 的公共 IP 地址的语法? The commandlet Get-AzPublicIpAddress has no argument for the VM name, only the IP object name commandlet Get-AzPublicIpAddress没有 VM 名称参数,只有 IP object 名称

This works, but I'm not using the machine name here, it's the name of the IP object itself:这有效,但我在这里没有使用机器名称,它是 IP object 本身的名称:

$CurrentIp = (Get-AzPublicIpAddress -ResourceGroupName 'RG1' -Name 'MyVMname-ip').IpAddress

I can't figure out how to just get it from the VM object ie this doesn't work:我不知道如何从 VM object 获取它,即这不起作用:

Get-AzVM -ResourceGroupName 'RG1' -Name 'MyVMname' | Get-AzPublicIpAddress

As I know, it's impossible to get the VM public IP through just one PowerShell Get-AzPublicIpAddress with the VM name.据我所知,仅通过一个带有 VM 名称的 PowerShell Get-AzPublicIpAddress VM 公共 IP 是不可能的。 And the public IP in Azure is an individual resource associated with the network interface, not the VM. Azure 中的公共 IP 是与网络接口关联的单个资源,而不是 VM。

As you see, there no parameter for VM name to get the public IP in that document.如您所见,该文档中没有用于获取公共 IP 的 VM 名称参数。 But you can get the public IP through a PowerShell script just with the VM name and resource group name.但是您可以通过 PowerShell 脚本获取公共 IP,只需使用 VM 名称和资源组名称。 The script shows below:该脚本显示如下:

$vm = Get-AzureRmVM -ResourceGroupName yourRG -Name vmNamme
$nic = $vm.NetworkProfile.NetworkInterfaces[0].Id.Split('/') | select -Last 1
$publicIpName =  (Get-AzureRmNetworkInterface -ResourceGroupName yourRG -Name $nic).IpConfigurations.PublicIpAddress.Id.Split('/') | select -Last 1
$publicIpAddress = (Get-AzureRmPublicIpAddress -ResourceGroupName yourRG -Name $publicIpName).IpAddress
Write-Output $vmName $publicIpAddress

Or just one CLI command to get the public IP like this:或者只是一个 CLI 命令来获取公共 IP,如下所示:

az vm show -d -g yourRG -n vmName --query publicIps

I think this is a more thorough answer, as this uses PowerShell Az as the original question intended to use.我认为这是一个更彻底的答案,因为它使用 PowerShell Az 作为打算使用的原始问题。 In addition, it leverages Generic.List[psobject] which is useful for later playing with the data.此外,它还利用了 Generic.List[psobject],这对于以后处理数据很有用。

$rg = 'RgName'
$Ips = Get-AzNetworkInterface -ResourceGroupName $rg
$vmDetails = New-Object "System.Collections.Generic.List[psobject]"
foreach ($instance in $Ips){
    $Vm = ($instance.VirtualMachine).Id.Split('/') | select -Last 1
    $PrivateIp = $instance.IpConfigurations.PrivateIpAddress
    $PublicIp = (Get-AzPublicIpAddress -ResourceGroupName $rg -Name ($instance.IpConfigurations.publicIpAddress.Id.Split('/') | select -Last 1)).IpAddress
    $obj = New-Object psobject -Property @{
        ResourceGroupName = $rg
        VmName = $vm
        PrivateIp = $PrivateIp
        PublicIp = $PublicIp
    }
    $vmDetails.Add($obj)
}
Write-Output $vmDetails

This isn't as straight forward as Az CLI unfortunately but a good script to have regardless for Az modules.不幸的是,这不像 Az CLI 那样直接,但无论 Az 模块如何,它都是一个很好的脚本。

Here's my take on Andrew Harris' answer, it filters out network interfaces not attached to machines and accounts for VMs that don't have a public IP:这是我对 Andrew Harris 的回答的看法,它过滤掉了未连接到机器的网络接口和没有公共 IP 的 VM 的帐户:

function Get-VmIP {
    <#
    .SYNOPSIS
        Returns the IP addresses for all VMs in the current subscription.
    #>
    [cmdletbinding()]
    param()

    $Interfaces = Get-AzNetworkInterface

    foreach ($Interface in $Interfaces) {

        if ($Interface.VirtualMachine) {
            $VMName = $Interface.VirtualMachine.Id.split('/')[-1]
            $PrivateIP = $Interface.IpConfigurations.PrivateIpAddress

            $PublicIP = if ($Interface.IpConfigurations.publicIpAddress) {
                Get-AzPublicIpAddress -Name ($instance.IpConfigurations.publicIpAddress.Id.Split('/')[-1]).IpAddress
            }     
        
            [PSCustomObject]@{
                VMName    = $VMName
                RGName    = $Interface.ResourceGroupName
                PrivateIP = $PrivateIP
                PublicIP  = $PublicIP
            }
        }
    }
}

This is a corrected version of the Mark Wragg's script earlier in this thread:这是该线程前面 Mark Wragg 脚本的更正版本:

function Get-VmIP {
<#
.SYNOPSIS
    Returns the IP addresses for all VMs in the current subscription.
#>
[cmdletbinding()]
param()

$Interfaces = Get-AzNetworkInterface

foreach ($Interface in $Interfaces) {

    if ($Interface.VirtualMachine) {
        $VMName = $Interface.VirtualMachine.Id.split('/')[-1]

        $PrivateIP = $Interface.IpConfigurations.PrivateIpAddress
        $PublicIpAddressConfig = $Interface.IpConfigurations.publicIpAddress
        
        $PublicIP = $null
        $pconfigname = $null
        if ($PublicIpAddressConfig) {
            $pconfigname = $PublicIpAddressConfig.Id.Split('/')[-1]
            $PublicIP = (Get-AzPublicIpAddress -Name $pconfigname).IpAddress
        }     
    
        [PSCustomObject]@{
            VMName    = $VMName
            RGName    = $Interface.ResourceGroupName
            PrivateIP = $PrivateIP
            PublicIP  = $PublicIP
        }
    }
}

} }

The accepted answer uses AzureRM PowerShell module which is now obsoleted by Az module:接受的答案使用 AzureRM PowerShell 模块,该模块现已被 Az 模块废弃:

$VM = Get-AzVM -ResourceGroupName $ResourceGroupName -VMName $VMName
$NetworkInterfaceName = $VM.NetworkProfile.NetworkInterfaces[0].Id.Split('/')[-1]
$NetworkInterface = Get-AzNetworkInterface -ResourceGroupName $VM.ResourceGroupName -Name $NetworkInterfaceName
$PublicIpAddressName = $NetworkInterface.IpConfigurations.PublicIpAddress.Id.Split('/')[-1]
$PublicIpAddress = Get-AzPublicIpAddress -ResourceGroupName $VM.ResourceGroupName -Name $PublicIpAddressName
Write-Host "IP: $($PublicIpAddress.IpAddress), FQDN: $($PublicIpAddress.DnsSettings.Fqdn)"

Scope of the script is within an Azure subscription.脚本的范围在 Azure 订阅内。

Below is the one-liner script which returns Name, PublicIpAllocaitonMethod(It's basically the type of the IP address whether it's a Static or Public IP) and the IpAddress properties of all the Network interfaces in a subscription.下面是一个单行脚本,它返回 Name、PublicIpAllocaitonMethod(它基本上是 IP 地址的类型,无论是静态 IP 还是公共 IP)和订阅中所有网络接口的 IpAddress 属性。

(Get-AzNetworkInterface ).IpConfigurations.PublicIpAddress.Id | Foreach-Object -Process {$_.Split('/')| select -Last 1} | Foreach-Object -Process {Get-AzPublicIpAddress -Name $_} | Format-List Name, PublicIpAllocationMethod,IpAddress

If we remove the last statement Format-List it will display all the properties of the network interfaces that are having public IP addresses.如果我们删除最后一条语句 Format-List,它将显示具有公共 IP 地址的网络接口的所有属性。

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

相关问题 通过公共 IP 地址获取 Azure FQDN(DNS 名称) - Get Azure FQDN (DNS Name) by Public IP Address Update-AzADApplication:找不到接受参数 $True 的位置参数 - Azure Az Powershell 模块 - Update-AzADApplication: A positional parameter cannot be found that accepts argument $True - Azure Az Powershell Module Azure Powershell - 将私有 IP 地址分配给 NIC 时出现问题 - Azure Powershell - Problem with assigning private IP address to an NIC terraform azure 公共 ip 和公共 ip 前缀 - terraform azure public ip and public ip prefix 数据融合公共IP地址 - Data Fusion Public IP Address 有没有办法为 Azure 数据工厂获取 static ip 地址 - Is there a way to get static ip address for Azure Data Factory 查找unassociate public IP Azure - Find unassociate public IP Azure Powershell Az 在接受数组参数的计划中创建 Azure 策略 - Powershell Az to create Azure Policy in an Initiative That Accepts An Array Parameters 无法在 PowerShell 7.3.x 中安装“Az”模块 - Can't Install `Az` module in PowerShell 7.3.x 需要 Powershell 脚本来获取所有公共 IP 地址,并在所有订阅中附加资源名称、资源组 - Need Powershell script to get all public IP addresses with attached resource name, Resource group, in all subcriptions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM