get list of available ip addresses in subnet

南笙酒味 提交于 2019-12-22 18:06:08

问题


How can i get the list of available ip address in a subnet? I was able to get the list of all ip addresses but not able to check if the ip address is already utilized. for some reason the below command doesn't work. Test-AzureRmPrivateIPAddressAvailability

Thanks in adavance.


回答1:


I have the following example and it is working as expected. I am using ` backtick character to separate my commands into multiple lines.

$vNetName = "myvirtualnetworkname"
$resourceGroupName = "myresourcegroup"
Get-AzureRmVirtualNetwork `
-Name $vNetName `
-ResourceGroupName $resourceGroupName `
| Test-AzureRmPrivateIPAddressAvailability -IPAddress "10.0.0.10"

I am piping into Test-AzureRMPrivateIPAddressAvailability command so it didn't provide resource group or vnetname in that command. If you use that command by its own then you will have to provide resource group name and vnet name.




回答2:


For some reason the below command doesn't work. Test-AzureRmPrivateIPAddressAvailability

We can use Test-AzureRmPrivateIPAddressAvailability like this:

PS C:\> Get-AzureRmVirtualNetwork -Name $vnetName -ResourceGroupName $rgname | Test-AzureRmPrivateIPAddressAvailability -IPAddress "10.0.1.10"

Also we can use PowerShell to list available ip address in a subnet, here is my script:

PS C:\> $vnet = Get-AzureRmVirtualNetwork -Name "vnet" -ResourceGroupName "jason"
PS C:\> $networkID = "10.0.0."
PS C:\> For ($i=1; $i -lt 255; $i++)
>> {
>>     $IP = $networkID + $i
>>     $Address = Test-AzureRmPrivateIPAddressAvailability -VirtualNetwork $vnet -IPAddress $IP
>>     If ($Address.Available –eq $False) { Write-Host "$IP is not available" -ForegroundColor Red }
>>     else { Write-Host "$IP is available" -ForegroundColor Green}
>> }
10.0.0.1 is not available
10.0.0.2 is not available
10.0.0.3 is not available
10.0.0.4 is not available
10.0.0.5 is available
10.0.0.6 is available
10.0.0.7 is available
10.0.0.8 is available
10.0.0.9 is available
10.0.0.10 is available



来源:https://stackoverflow.com/questions/41774085/get-list-of-available-ip-addresses-in-subnet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!