首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Azure Peerings的输出

Azure Peerings的输出
EN

Stack Overflow用户
提问于 2020-12-24 21:39:28
回答 1查看 70关注 0票数 0

我目前正在编写一个脚本,它可以给我提供Azure订阅中的每个Peering的输出,其中列出了VNets和一些选项,如AllowVirtualNetworkAccess、AllowForwardedTraffic、UseRemoteGateways和AllowGatewayTransit。输出应如下所示:

代码语言:javascript
复制
Peering Name     VNet 1          VNet 2          AllowVirtualNetworkAccess AllowForwardedTraffic UseRemoteGateways AllowGatewayTransit                                    
------------     ------          ------          ------------------------- --------------------- ----------------- -------------------
test-peering     vnet-ine-test   vnet-chn-docker False                     True                  False             False
vnet-docker-test vnet-chn-docker vnet-ine-test   True                      True                  False             False

这是我的脚本当前的样子:

代码语言:javascript
复制
$VNets = Get-AzVirtualNetwork
$peerings=@()
$peeringInfo = @{ "Peering Name"="" ; "VNet 1"="" ; "VNet 2"="" ; "AllowVirtualNetworkAccess"="" ; "AllowForwardedTraffic"="" ; "UseRemoteGateways"="" ; "AllowGatewayTransit"=""}
foreach($VNet in $VNets){
   $peeringInfo.'Peering Name'=$VNet.VirtualNetworkPeerings.Name
   $peeringInfo.'AllowVirtualNetworkAccess'=$VNet.VirtualNetworkPeerings.Name
   $peeringInfo.'AllowForwardedTraffic'=$VNet.VirtualNetworkPeerings.Name
   $peeringInfo.'UseRemoteGateways'=$VNet.VirtualNetworkPeerings.Name
   $peeringInfo.'AllowGatewayTransit'=$VNet.VirtualNetworkPeerings.Name
   $peering = Get-AzVirtualNetworkPeering -VirtualNetworkName $VNet.Name -ResourceGroupName $VNet.ResourceGroupName
   foreach($peer in $peering){
        $peeringInfo.'VNet 1' =$peer.VirtualNetworkName
        $peeringInfo.'VNet 2' =(Get-AzVirtualNetwork -Name $peer.RemoteVirtualNetwork.Id).Name
        $obj=New-Object PSObject -Property $peeringInfo
        $peerings +=$obj
   }

  
}
$peerings | Format-Table "Peering Name","VNet 1","VNet 2",AllowVirtualNetworkAccess,AllowForwardedTraffic,UseRemoteGateways,AllowGatewayTransit

这是输出:

代码语言:javascript
复制
Peering Name     VNet 1          VNet 2                           AllowVirtualNetworkAccess AllowForwardedTraffic UseRemoteGateways AllowGatewayTransit
------------     ------          ------                           ------------------------- --------------------- ----------------- -------------------
test-peering     vnet-ine-test   {vnet-ine-test, vnet-chn-docker} test-peering              test-peering          test-peering      test-peering       
vnet-docker-test vnet-chn-docker {vnet-ine-test, vnet-chn-docker} vnet-docker-test          vnet-docker-test      vnet-docker-test  vnet-docker-test  

我知道这个问题与行$peeringInfo.'VNet 2' =(Get-AzVirtualNetwork -Name $peer.RemoteVirtualNetwork.Id).Name有关,因为当我只运行(Get-AzVirtualNetwork -Name $peer.RemoteVirtualNetwork.Id).Name时会有两个输出,这很奇怪,因为$peer.RemoteVirtualNetwork.Id应该只有一个值。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-24 23:28:50

所以我找到了答案。实际上,它是$peeringInfo.'VNet 2' =(Get-AzVirtualNetwork | Where-Object ID -EQ $peer.RemoteVirtualNetwork.Id).Name这一行。现在已将其更改为Where-Object,以比较ID,而不是搜索名称。下面是完整的脚本:

代码语言:javascript
复制
$VNets = Get-AzVirtualNetwork
$peerings=@()
$peeringInfo = @{ "Peering Name"="" ; "VNet 1"="" ; "VNet 2"="" ; "AllowVirtualNetworkAccess"="" ; "AllowForwardedTraffic"="" ; "UseRemoteGateways"="" ; "AllowGatewayTransit"=""}
foreach($VNet in $VNets){
    $peeringInfo.'Peering Name'=$VNet.VirtualNetworkPeerings.Name
    $peeringInfo.'AllowVirtualNetworkAccess'=$VNet.VirtualNetworkPeerings.AllowVirtualNetworkAccess
    $peeringInfo.'AllowForwardedTraffic'=$VNet.VirtualNetworkPeerings.AllowForwardedTraffic
    $peeringInfo.'UseRemoteGateways'=$VNet.VirtualNetworkPeerings.UseRemoteGateways
    $peeringInfo.'AllowGatewayTransit'=$VNet.VirtualNetworkPeerings.AllowGatewayTransit
    $peering = Get-AzVirtualNetworkPeering -VirtualNetworkName $VNet.Name -ResourceGroupName $VNet.ResourceGroupName
    foreach($peer in $peering){
        $peeringInfo.'VNet 1' =$peer.VirtualNetworkName
        $peeringInfo.'VNet 2' =(Get-AzVirtualNetwork | Where-Object ID -EQ $peer.RemoteVirtualNetwork.Id).Name
        $obj=New-Object PSObject -Property $peeringInfo
        $peerings +=$obj
   }

  
}
$peerings | Format-Table "Peering Name","VNet 1","VNet 2",AllowVirtualNetworkAccess,AllowForwardedTraffic,UseRemoteGateways,AllowGatewayTransit

下面是输出:

代码语言:javascript
复制
Peering Name     VNet 1          VNet 2          AllowVirtualNetworkAccess AllowForwardedTraffic UseRemoteGateways AllowGatewayTransit
------------     ------          ------          ------------------------- --------------------- ----------------- -------------------
test-peering     vnet-ine-test   vnet-chn-docker                     False                  True             False               False
vnet-docker-test vnet-chn-docker vnet-ine-test                        True                  True             False               False 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65439094

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档