我正在使用Az PowerShell模块。我想做以下事情。
验证VNET是否正在使用的最简单方法是在PowerShell中使用Remove-AzVirtualNetwork
,然后查看它是否引发错误。我想知道是否有更好的方法可以做到这一点。
答案 0 :(得分:1)
答案 1 :(得分:1)
这是从工作脚本中创建的一小段代码,该代码可以成功完成
$vnetname = ""
$vnetrgname = ""
$VNet = Get-AzVirtualNetwork -Name $vnetname -ResourceGroupName $vnetrgname
$ips = $VNet.Subnets | % {($_.IpConfigurations).Count}
$total = ($ips | Measure-Object -Sum).Sum
if ($total -eq "0")
{
Write-Host -ForegroundColor Green "Virtual Network '$vnetname' not in use, deleting Virtual Network"
Remove-AzVirtualNetwork -Name $vnetname -ResourceGroupName $vnetrgname -Force
}
Else
{
Write-Host -ForegroundColor Yellow -BackgroundColor Black "Virtual Network '$vnetname' in use, Skipping deletion of Virtual Network"
}
原始脚本:
答案 2 :(得分:1)
#Fetch the VNet Configuration
$VNetDetails=Get-AzVirtualNetwork -Name "<VirtualNetworkName>"-ResourceGroupName "<ResourceGroupName>"
#Fetch the SubnetConfig from the VNETConfig
$VnetSubnetConfig=Get-AzVirtualNetworkSubnetConfig -Name "<SubnetName>" -VirtualNetwork $VNetDetails
#Fetch the IPUsage from the SubnetID.
$PrivateIPUsage=Get-AzVirtualNetworkUsageList -ResourceGroupName "<ResoruceGroupName>" -Name "<VirtualNetworkName>" | where ID -eq $VnetSubnetConfig.id
[int] $TotalIPLimit=$PrivateIPUsage.Limit
[int] $TotalIPUsed=$PrivateIPUsage.CurrentValue
if($TotalIPUsed -lt $TotalIPLimit)
{
Write-Host "Private IP's are available in this Subnet for Usage."
}
else
{
Write-Host "Private IP's are not available in this Subnet for Usage."
}