gpt4 book ai didi

azure - 导出互联网络 |子网

转载 作者:行者123 更新时间:2023-12-03 03:39:10 26 4
gpt4 key购买 nike

我想导出一些内容,例如 VNET 中存在的子网 View (如门户中显示的那样)。不幸的是,没有选项可以将其导出为 CSV。我在网上找到了可以导出子网路由表和关联子网的 powershell 脚本。我还找到了用于导出 vnet 子网详细信息的 powershell 脚本。但是我还没有找到结合两者的脚本

Script for Route tables by Aman Sharma

忽略我认为他从以前的脚本中留下的概要和描述

因此,我尝试反转逻辑,即获取子网详细信息并添加每个子网的路由表(如果存在)。但我不确定此时我在做什么!脚本出错:

Line |
47 | … $routeTables = Get-AzRouteTable -Name $routeTableName -Resour …
| ~~~~~~~~~~~~~~~
| Cannot validate argument on parameter 'Name'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.

脚本结束,CSV 包含除路由表详细信息之外的所有内容。因此,如果您能帮助菜鸟,我将非常感激,这就是我所拥有的:

$PathToOutputCSVReport = "/path/output.csv"

$subs = Get-AzSubscription

#Checking if the subscriptions are found or not
if(($subs -ne $null) -or ($subs.Count -gt 0))
{
#Creating Output Object
$results = @()

#Iterating over various subscriptions
foreach($sub in $subs)
{
$SubscriptionId = $sub.SubscriptionId
Write-Output $SubscriptionName

#Selecting the Azure Subscription
Select-AzSubscription -SubscriptionName $SubscriptionId

#Getting all Azure Route Tables
$vnets = Get-AzVirtualNetwork

foreach($vnet in $vnets)
{
$vnetName = $vnet.Name
$vnetResourceGroup = $vnet.ResourceGroupName
Write-Output $vnetName

#Fetch Route Subnets
$vnetSubnets = $vnet.Subnets

foreach($vnetSubnet in $vnetSubnets)
{
$subnetName = $vnetSubnet.Name
Write-Output $subnetName

$subnetId = $vnetSubnet.Id

###Getting information
$splitarray = $subnetId.Split('/')
$subscriptionId = $splitarray[2]
$vNetResourceGroupName = $splitarray[4]
$virtualNetworkName = $splitarray[8]
$subnetName = $splitarray[10]

#Fetch the route table details
$routeTables = Get-AzRouteTable -Name $routeTableName -ResourceGroupName $routeResourceGroup

#Fetching the vNet and Subnet details
#$vnet = Get-AzVirtualNetwork -Name $virtualNetworkName -ResourceGroupName $vNetResourceGroupName
$subnet = Get-AzVirtualNetworkSubnetConfig -Name $subnetName -VirtualNetwork $vnet

$subnetAddressPrefix = $subnet.AddressPrefix[0]

$details = @{
virtualNetworkName=$virtualNetworkName
subnetAddressPrefix=$subnetAddressPrefix
subnetName=$subnetName
routeTableName=$routeTableName
routeResourceGroup=$routeResourceGroup
subscriptionId=$subscriptionId
vNetResourceGroupName=$vNetResourceGroupName

}
$results += New-Object PSObject -Property $details


}

}
}
$results | export-csv -Path $PathToOutputCSVReport -NoTypeInformation
}
else
{
Write-Host -ForegroundColor Red "No Subscription Found"
}

最佳答案

您收到的此错误清楚地表明变量 routeTableName 对于 cmdlet Get-AzRouteTable-Name 参数来说是无效值>.

查看您的脚本,该变量似乎没有在任何地方定义,这解释了为什么它是空的,因此无法与 cmdlet 一起使用。

要使用与子网关联的路由表名称定义变量,您可以使用以下命令:

$routeTableName = $subnet.RouteTable.Id.Split('/')[8]

您似乎根本没有使用以下内容,可以将其删除:

$routeTables = Get-AzRouteTable -Name $routeTableName -ResourceGroupName $routeResourceGroup

整个代码如下所示:

$PathToOutputCSVReport = "/path/output.csv"

$subs = Get-AzSubscription

#Checking if the subscriptions are found or not
if(($subs -ne $null) -or ($subs.Count -gt 0))
{
#Creating Output Object
$results = @()

#Iterating over various subscriptions
foreach($sub in $subs)
{
$SubscriptionId = $sub.SubscriptionId
Write-Output $SubscriptionName

#Selecting the Azure Subscription
Select-AzSubscription -SubscriptionName $SubscriptionId

#Getting all Azure Route Tables
$vnets = Get-AzVirtualNetwork

foreach($vnet in $vnets)
{
$vnetName = $vnet.Name
$vnetResourceGroup = $vnet.ResourceGroupName
Write-Output $vnetName

#Fetch Route Subnets
$vnetSubnets = $vnet.Subnets

foreach($vnetSubnet in $vnetSubnets)
{
$subnetName = $vnetSubnet.Name
Write-Output $subnetName

$subnetId = $vnetSubnet.Id

###Getting information
$splitarray = $subnetId.Split('/')
$subscriptionId = $splitarray[2]
$vNetResourceGroupName = $splitarray[4]
$virtualNetworkName = $splitarray[8]
$subnetName = $splitarray[10]

#Fetch the route table details
#$routeTables = Get-AzRouteTable -Name $routeTableName -ResourceGroupName $routeResourceGroup

#Fetching the vNet and Subnet details
#$vnet = Get-AzVirtualNetwork -Name $virtualNetworkName -ResourceGroupName $vNetResourceGroupName
$subnet = Get-AzVirtualNetworkSubnetConfig -Name $subnetName -VirtualNetwork $vnet
$routeTableName = $subnet.RouteTable.Id.Split('/')[8]

$subnetAddressPrefix = $subnet.AddressPrefix[0]

$details = @{
virtualNetworkName=$virtualNetworkName
subnetAddressPrefix=$subnetAddressPrefix
subnetName=$subnetName
routeTableName=$routeTableName
routeResourceGroup=$routeResourceGroup
subscriptionId=$subscriptionId
vNetResourceGroupName=$vNetResourceGroupName

}
$results += New-Object PSObject -Property $details


}

}
}
$results | export-csv -Path $PathToOutputCSVReport -NoTypeInformation
}
else
{
Write-Host -ForegroundColor Red "No Subscription Found"
}

关于azure - 导出互联网络 |子网,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72020042/

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