我目前正在编写一个脚本,它可以给我提供Azure订阅中的每个Peering的输出,其中列出了VNets和一些选项,如AllowVirtualNetworkAccess、AllowForwardedTraffic、UseRemoteGateways和AllowGatewayTransit。输出应如下所示:
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这是我的脚本当前的样子:
$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这是输出:
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应该只有一个值。
发布于 2020-12-24 23:28:50
所以我找到了答案。实际上,它是$peeringInfo.'VNet 2' =(Get-AzVirtualNetwork | Where-Object ID -EQ $peer.RemoteVirtualNetwork.Id).Name这一行。现在已将其更改为Where-Object,以比较ID,而不是搜索名称。下面是完整的脚本:
$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下面是输出:
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 https://stackoverflow.com/questions/65439094
复制相似问题