Number of avaliable ips in azure subnet - azure

I am looking for number of available IP addresses in each subnet in azure vnet
so I am trying below script but I am not getting any option to get available IP.
$nic = Get-AzureRmVirtualNetwork -Name TST-VNET1 -ResourceGroupName TST-RG1
$nic.AddressSpace.AddressPrefixes
$subnets= $nic.Subnets
$subnets
foreach( $subnet in $subnets)
{
$subnet.Name
$subnet.IpConfigurations.Count
$subnet.AddressPrefix
}
Using $subnet.IpConfigurations.Count gets the usable IP addresses. $subnet.AddressPrefix gets the cidr.
I am looking here available or assigned host IP addresses in each subnet pool.

We can use Azure portal to get the number of available IP addresses:
PowerShell script:
$nic = Get-AzureRmVirtualNetwork -Name "jason" -ResourceGroupName "vnet"
$nic.AddressSpace.AddressPrefixes
$subnets = $nic.Subnets
$subnets
foreach( $subnet in $subnets)
{
$subnet.Name
$subnet.IpConfigurations.Count
$subnet.AddressPrefix
}
$splitAddress = $subnet.AddressPrefix.Split("/")
$output = [math]::Pow(2, (32 - $splitAddress[1])) - 5 - $subnet.IpConfigurations.Count

Related

Subnets are not getting created while using Powershell Az commands

I have below PowerShell script to create vnet and subnet in Azure
$virtualNetworkName = 'corp-northeurope-vnet'
$frontendSubnetName = 'frontendsubnet'
$vNetAddressPrefix = "10.0.0.0/26"
$SubnetAddressPrefix = "10.0.1.0/28"
$virtualNetwork = Get-AzVirtualNetwork -Name $virtualNetworkName -ResourceGroupName $rgName
if ($null -eq $virtualNetwork) {
$virtualNetwork = New-AzVirtualNetwork `
-Name $virtualNetworkName `
-ResourceGroupName $rgName `
-AddressPrefix $vNetAddressPrefix `
-Location $location
}
else {
Write-Log -Message "[$($virtualNetwork.Name)] already exists"
}
$fesubnet = Get-AzVirtualNetworkSubnetConfig -Name $frontendSubnetName -VirtualNetwork $virtualNetwork
if ($null -eq $fesubnet) {
$fesubnet = Add-AzVirtualNetworkSubnetConfig `
-Name $frontendSubnetName `
-AddressPrefix $subnetAddressPrefix `
-VirtualNetwork $virtualNetwork
$virtualNetwork | Set-AzVirtualNetwork
}
else {
Write-Log -Message "[$($fesubnet.Name)] already exists"
}
But It does not work.
Error throws here
$virtualNetwork | Set-AzVirtualNetwork
Subnet 'frontendsubnet' is not valid in virtual network 'corp-northeurope-vnet'. StatusCode: 400 ReasonPhrase: Bad Request ErrorCode: NetcfgInvalidSubnet ErrorMessage: Subnet 'frontendsubnet' is
| not valid in virtual network 'corp-northeurope-vnet'. OperationID : 06c1ed77-14f1-294d-a19a-41c2epbdd04f
Is it anything to do with IP Range ?
The IP Range of the subnet is not within the IP Range of the VNET this causes the Subnet configuration to be invalid.
So either you change the Subnet address prefix to be within the VNETs Address Prefix or you expand the VNETs address prefix so that it includes the range of the Subnet you are trying to create.
A good tool to use to plan your IP Address Prefixes is: http://jodies.de/ipcalc?host=10.0.0.0&mask1=26&mask2=
The error caused by your subnet address prefix. When your VNet address prefix is 10.0.0.0/26. Then your subnet addresses range should be less than 10.0.0.0/26. You can change the subnet prefix as 10.0.0.0/28. Then it will be no problem.
One problem is the second command line.
If you are saying you already have created a Vnet, a subnet, a Vnet IP CIDR prefix, and a Subnet IP CIDR prefix, the command for Subnet is Not $frontendSubnetName, "Frontend" is the name of the subnet included on Vnet "corp-northeurope-vnet".
$frontendSubnetName = 'frontendsubnet'
the command frontendSubnetName is the wrong, the correct should be:
$subnetConfig = 'frontendsubnet'
Try using these commands:
$virtualNetworkName = 'corp-northeurope-vnet'
$subnetConfig = 'frontendsubnet'
$vNetAddressPrefix = "10.0.0.0/26"
$SubnetAddressPrefix = "10.0.1.0/28"
Here the official documentation: https://learn.microsoft.com/en-us/azure/virtual-network/quick-create-powershell

Azure Powershell (Az module) get public IP address

With the new Az module for Azure, does anyone have the syntax for getting the public IP address of an Azure VM using the name?
The commandlet Get-AzPublicIpAddress has no argument for the VM name, only the IP object name
This works, but I'm not using the machine name here, it's the name of the IP object itself:
$CurrentIp = (Get-AzPublicIpAddress -ResourceGroupName 'RG1' -Name 'MyVMname-ip').IpAddress
I can't figure out how to just get it from the VM object i.e. this doesn't work:
Get-AzVM -ResourceGroupName 'RG1' -Name 'MyVMname' | Get-AzPublicIpAddress
As I know, it's impossible to get the VM public IP through just one PowerShell Get-AzPublicIpAddress with the VM name. And the public IP in Azure is an individual resource associated with the network interface, not the VM.
As you see, there no parameter for VM name to get the public IP in that document. But you can get the public IP through a PowerShell script just with the VM name and resource group name. The script shows below:
$vm = Get-AzureRmVM -ResourceGroupName yourRG -Name vmNamme
$nic = $vm.NetworkProfile.NetworkInterfaces[0].Id.Split('/') | select -Last 1
$publicIpName = (Get-AzureRmNetworkInterface -ResourceGroupName yourRG -Name $nic).IpConfigurations.PublicIpAddress.Id.Split('/') | select -Last 1
$publicIpAddress = (Get-AzureRmPublicIpAddress -ResourceGroupName yourRG -Name $publicIpName).IpAddress
Write-Output $vmName $publicIpAddress
Or just one CLI command to get the public IP like this:
az vm show -d -g yourRG -n vmName --query publicIps
I think this is a more thorough answer, as this uses PowerShell Az as the original question intended to use. In addition, it leverages Generic.List[psobject] which is useful for later playing with the data.
$rg = 'RgName'
$Ips = Get-AzNetworkInterface -ResourceGroupName $rg
$vmDetails = New-Object "System.Collections.Generic.List[psobject]"
foreach ($instance in $Ips){
$Vm = ($instance.VirtualMachine).Id.Split('/') | select -Last 1
$PrivateIp = $instance.IpConfigurations.PrivateIpAddress
$PublicIp = (Get-AzPublicIpAddress -ResourceGroupName $rg -Name ($instance.IpConfigurations.publicIpAddress.Id.Split('/') | select -Last 1)).IpAddress
$obj = New-Object psobject -Property #{
ResourceGroupName = $rg
VmName = $vm
PrivateIp = $PrivateIp
PublicIp = $PublicIp
}
$vmDetails.Add($obj)
}
Write-Output $vmDetails
This isn't as straight forward as Az CLI unfortunately but a good script to have regardless for Az modules.
Here's my take on Andrew Harris' answer, it filters out network interfaces not attached to machines and accounts for VMs that don't have a public IP:
function Get-VmIP {
<#
.SYNOPSIS
Returns the IP addresses for all VMs in the current subscription.
#>
[cmdletbinding()]
param()
$Interfaces = Get-AzNetworkInterface
foreach ($Interface in $Interfaces) {
if ($Interface.VirtualMachine) {
$VMName = $Interface.VirtualMachine.Id.split('/')[-1]
$PrivateIP = $Interface.IpConfigurations.PrivateIpAddress
$PublicIP = if ($Interface.IpConfigurations.publicIpAddress) {
Get-AzPublicIpAddress -Name ($instance.IpConfigurations.publicIpAddress.Id.Split('/')[-1]).IpAddress
}
[PSCustomObject]#{
VMName = $VMName
RGName = $Interface.ResourceGroupName
PrivateIP = $PrivateIP
PublicIP = $PublicIP
}
}
}
}
This is a corrected version of the Mark Wragg's script earlier in this thread:
function Get-VmIP {
<#
.SYNOPSIS
Returns the IP addresses for all VMs in the current subscription.
#>
[cmdletbinding()]
param()
$Interfaces = Get-AzNetworkInterface
foreach ($Interface in $Interfaces) {
if ($Interface.VirtualMachine) {
$VMName = $Interface.VirtualMachine.Id.split('/')[-1]
$PrivateIP = $Interface.IpConfigurations.PrivateIpAddress
$PublicIpAddressConfig = $Interface.IpConfigurations.publicIpAddress
$PublicIP = $null
$pconfigname = $null
if ($PublicIpAddressConfig) {
$pconfigname = $PublicIpAddressConfig.Id.Split('/')[-1]
$PublicIP = (Get-AzPublicIpAddress -Name $pconfigname).IpAddress
}
[PSCustomObject]#{
VMName = $VMName
RGName = $Interface.ResourceGroupName
PrivateIP = $PrivateIP
PublicIP = $PublicIP
}
}
}
}
The accepted answer uses AzureRM PowerShell module which is now obsoleted by Az module:
$VM = Get-AzVM -ResourceGroupName $ResourceGroupName -VMName $VMName
$NetworkInterfaceName = $VM.NetworkProfile.NetworkInterfaces[0].Id.Split('/')[-1]
$NetworkInterface = Get-AzNetworkInterface -ResourceGroupName $VM.ResourceGroupName -Name $NetworkInterfaceName
$PublicIpAddressName = $NetworkInterface.IpConfigurations.PublicIpAddress.Id.Split('/')[-1]
$PublicIpAddress = Get-AzPublicIpAddress -ResourceGroupName $VM.ResourceGroupName -Name $PublicIpAddressName
Write-Host "IP: $($PublicIpAddress.IpAddress), FQDN: $($PublicIpAddress.DnsSettings.Fqdn)"
Scope of the script is within an Azure subscription.
Below is the one-liner script which returns Name, PublicIpAllocaitonMethod(It's basically the type of the IP address whether it's a Static or Public IP) and the IpAddress properties of all the Network interfaces in a subscription.
(Get-AzNetworkInterface ).IpConfigurations.PublicIpAddress.Id | Foreach-Object -Process {$_.Split('/')| select -Last 1} | Foreach-Object -Process {Get-AzPublicIpAddress -Name $_} | Format-List Name, PublicIpAllocationMethod,IpAddress
If we remove the last statement Format-List it will display all the properties of the network interfaces that are having public IP addresses.

Easier way of retrieving an Azure VM's Public IP address

Using the name/resource group of a specific VM, I'm trying to get the VM's public IP address.
This code works but it seems unwieldy in comparison to other AzureRM cmdlets.
$VM = Get-AzureRmVM -ResourceGroupName MyResourceGroup -Name MyVMName
$NIC = $VM.NetworkProfile.NetworkInterfaces[0].Id -replace '.*\/'
$NI = Get-AzureRmNetworkInterface -Name $NIC -ResourceGroupName MyResourceGroup
$NIIC = Get-AzureRmNetworkInterfaceIpConfig -NetworkInterface $NI
$PIP = $NIIC.PublicIpAddress.Id -replace '.*\/'
$PIP = Get-AzureRmPublicIpAddress -Name $PIP -ResourceGroupName MyResourceGroup
$PIP.IpAddress
Is there a quicker/easier/shorter way of accessing this information?
As far as i know, Not Yet for PowerShell. But you can use Azure CLI
az vm list-ip-addresses -n <VMName> -g <ResourceGroup> | grep publicIpAddresses
Try the Azure CLI command:
az vm list-ip-addresses -g groupName -n vmName --query "[].virtualMachine.network.publicIpAddresses[*].ipAddress" -o tsv
Or the PowerShell command just filter with your vm name:
$ipAddress= (Get-AzureRmPublicIpAddress -ResourceGroupName groupName | Where-Object { $_.IpConfiguration.Id -like "*vmName*" }
)
$ipAddress.IpAddress
It's possible. This script will list all VMs PIP in your Azure cloud.
OLD
$VM_int = Get-AzureRmResource -ODataQuery "`$filter=resourcetype 'Microsoft.Compute/virtualMachines'"
foreach($int in $VM_int){
$vmName = $int.Name
$ipAddress= (Get-AzureRmPublicIpAddress -ResourceGroupName $int.ResourceGroupName | Where-Object { $_.IpConfiguration.Id -like "*$vmName*" })
$vmName + ' --- ' + $ipAddress.IpAddress
}
UPDATE
Unfortunately, Get-AzVM doesn't provide the Public IP address of VM, but we can scrape its Network Interface Name and make a wildcard search of it through all assigned Public IPs which NIC name are matched.
It's not fast but will provide with correct results.
$array = #()
foreach ($vm in Get-AzVM) {
$vmNicName = $vm.NetworkProfile.NetworkInterfaces.Id.Split("/")[8]
$ipAddress = Get-AzPublicIpAddress | Where-Object {$_.IpConfiguration.Id -like "*$vmNicName*"}
if ($null -ne $ipAddress) {
$pipInput = New-Object psobject -Property #{
VM = $vm.Name
PublicIP = $ipAddress.IpAddress
}
$array += $pipInput
}
}
The way i got the value for my Linux VM's was using below code.
Get-AzureRmPublicIpAddress -ResourceGroupName <yourRG> -Name <yourVMName> | Select-Object {$_.IpAddress}
This will return something of this sort:
$_.IpAddress
------------
52.170.56.60
This outputs a bit of information however the public IP address is in there.
Get-AzPublicIpAddress -ResourceGroupName MyResourceGroup | Where-Object {$_.name -like "*MyVMName*" }
Or you can do this to just get the IP address:
Get-AzPublicIpAddress -ResourceGroupName MyResourceGroup | Where-Object {$_.name -like "*MyVMName*" } | Select-Object { $_.IpAddress }
Output is like:
$_.IpAddress
--------------
13.255.162.33
You can also match the AzPublicIpAddress IpConfiguration.Id with the VM's NetworkInterfaces.Id:
Get-AzPublicIpAddress | ?{$_.IpConfiguration.Id -match "$((Get-AzVM -Name $computername).NetworkProfile.NetworkInterfaces.Id).*" }
#Get the VM object
$vm = Get-AzVM -Name $vmName -Status
#Get name of network adapter object attached to VM
$NetworkInterfaceName = $vm.NetworkProfile.NetworkInterfaces.Id.Split("/") | Select -Last 1
#Get network adaptor object attached to VM
$NetworkInterfaceObject = Get-AzNetworkInterface -Name $NetworkInterfaceName
#Get public IP Address object name attached to network adaptor object
$ipObjectName = $NetworkInterfaceObject.IpConfigurations.PublicIpAddress.Id.Split("/") | Select -Last 1
#get publivc IP Address attached to public IP Address object
$ipObject = Get-AzPublicIpAddress -ResourceGroupName $resourceGroupName -Name $ipObjectName
Write-Output $ipObject.IpAddress
Yet another method from within a Linux VM.
First, install the Azure command-line tools in the VM, see Azure docs
Second, execute the following in a shell on the VM:
az network public-ip list --query "[?dnsSettings.domainNameLabel=='MY_VM']"
where MY_VM is (hopefully) the host name of your VM. The command returns a multiline JSON string which is a list. Example is shown below:
[
{
"dnsSettings": {
"domainNameLabel": "MY_VM",
"fqdn": "my_vm.westeurope.cloudapp.azure.com"
},
"etag": "W/\"some_uuid...\"",
...
"ipAddress": "AAA.BBB.CCC.DDD",
},
...
]
How to parse the FQDN and the public IP out of this is left as an exercise to the reader :-)

How to get the Virtual IP Address associated with a Azure App Service via Powershell

I'm trying to do an IP changeover and I'm looking to get the Virtual IP of all our app service instances.
So far I've put together the following powershell script:
$apps = Get-AzureRmWebApp
Foreach($app in $apps)
{
Write-Output "$($app.Name)|$($app.OutboundIpAddresses)"
($app | Get-AzureDeployment -Slot Production).VirtualIPs[0].Address
break;
}
But I'm stuck at the Get-AzureDeployment step - I think there should be a RM version of this, but I can't find it.
Related GitHub issue - this indicates it does exist: https://github.com/Azure/azure-powershell/issues/1648
I'm trying to get to the Virtual Ip Address as per the below screenshot:
If you want to get the VIRTUAL IP ADDRESS, you could use the command below.
$slot = Get-AzureRmWebAppSlot -ResourceGroupName "<ResourceGroupName>" -Name "<yourwebappname>" -Slot "<yourslotname>"
($slot.OutboundIpAddresses -split ",")[0]
Your complete command should be:
$apps = Get-AzureRmWebApp
Foreach($app in $apps)
{
Write-Output "$($app.Name)|$($app.OutboundIpAddresses)"
$slot = Get-AzureRmWebAppSlot -ResourceGroupName $app.ResourceGroup -Name $app.Name -Slot "<yourslotname>"
($slot.OutboundIpAddresses -split ",")[0]
break;
}

Create a VM with a reserved IP

I tried to create a VM with a reserved IP adress like this:
New-AzureQuickVM -ImageName a699494373c04fc0bc8f2bb1389d6106__Windows-Server-2012-R2-201409.01-en.us-127GB.vhd -ServiceName VmPIPBis3 -Windows -AdminUsername amethyste -Location "West Europe" -Password SuperMotDePasse12 -ReservedIPName 104.45.13.146
But all I get is this error message:
New-AzureQuickVM : BadRequest: The Reserved IP 104.45.13.146 does not exist.
The only thing created is the service cloud
Does anybody know what happened?
thanks
You need to first reserve the IP in your Azure Subscription and then pass the ReservedIPName (not the address) to the ReservedIPName parameter in your call to New-AzureQuickVM. Below is a script that creates a new reserved IP if one doesn't exist for the name given and then creates a new VM using the reserved IP.
$location = "West US"
$appVMName = "AppVM01"
$appVMServiceName = [Guid]::NewGuid().ToString();
$imageName = "a699494373c04fc0bc8f2bb1389d6106__Windows-Server-2012-R2-201409.01-en.us-127GB.vhd"
$adminUser = "AdminUser"
$adminPswd = "AdminPassw0rd"
$reservedIPName = $appVMName + "-resrvdIP"
# Get the reserved IP if it exists or create a new one.
$reservedIP = Get-AzureReservedIP -ReservedIPName $reservedIPName -ErrorAction SilentlyContinue
if ($reservedIP -eq $null)
{
Write-Host "Reserving IP in '$location' as '$reservedIPName'."
New-AzureReservedIP -ReservedIPName $reservedIPName -Location $location
$reservedIP = Get-AzureReservedIP -ReservedIPName $reservedIPName -ErrorAction Stop
}
# Create a new VM using the reserved IP
New-AzureQuickVM -Name $appVMName -ServiceName $appVMServiceName -Windows -ImageName $imageName `
-AdminUsername $adminUser -Password $adminPswd -Location $location -ReservedIPName $reservedIP.ReservedIPName
Write-Host "VM Created using the following reserved IP Address:... " + $reservedIP.Address

Resources