如何检查Azure Vnet是否正在使用

时间:2019-12-11 05:54:27

标签: azure powershell azure-powershell vnet

我正在使用Az PowerShell模块。我想做以下事情。

  1. 确认未使用指定的天蓝色vnet。
  2. 如果不使用它,请删除VNET。

验证VNET是否正在使用的最简单方法是在PowerShell中使用Remove-AzVirtualNetwork,然后查看它是否引发错误。我想知道是否有更好的方法可以做到这一点。

3 个答案:

答案 0 :(得分:1)

我建议您使用Get-AzVirtualNetworkUsageList获取每个子网的使用情况。

enter image description here

CurrentValue的值显示分配的专用IP使用情况。如果大于0,则应使用网络。

答案 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"
}

原始脚本:

https://raw.githubusercontent.com/hhazeley/HannelsToolBox/master/Functions/Remove-AzureV2VMandResources.ps1

答案 2 :(得分:1)

参考链接:https://docs.microsoft.com/en-us/powershell/module/az.network/get-azvirtualnetworkusagelist?view=azps-5.5.0

#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."

}