Subnets are not getting created while using Powershell Az commands - azure

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

Related

Fix for the Subnet Missing Required Delegation

I have a Azure Resource Group and it has a vnet. The vnet has a subnet which has two service endpoints configured namely Microsoft.keyVault and Microsoft.Storage and the subnet also has a subnet delegation to Microsoft.Web/serverFarms.
Now I want to add another service endpoint Microsoft.ServiceBus to the same vnet using Azure Powershell. I executed the below code for that.
$virtualNetwork = Get-AzVirtualNetwork -ResourceGroupName $ResourceGroupName
Set-AzVirtualNetworkSubnetConfig -Name $virtualNetwork.Subnets.Name -VirtualNetwork $virtualNetwork -AddressPrefix $virtualNetwork.Subnets.AddressPrefix -ServiceEndpoint "Microsoft.ServiceBus"
$virtualNetwork | Set-AzVirtualNetwork
But the above code is throwing error at the last line of code saying that the Subnet is missing Required Delegation
Subnet requires any of the following delegation(s) [Microsoft.Web/serverFarms] to reference service association link
StatusCode: 400
ReasonPhrase: Bad Request
ErrorCode: SubnetMissingRequiredDelegation
But in portal I see it has the required delegation. How to fix this error?
Even though i have already added these service endpoint in my specific subnet and subnet also has a subnet delegation to Microsoft.Web/serverFarms able to add another service endpoint Microsoft.ServiceBus.
I would suggest you to use the following PowerShell script:
PowerShell Script:
$subscription = "b83c1edXXXXXXX-XXX"
$subnets = #('TestSubnet')
$vnetName = "Vnet1"
$vnetRgName = "X-rasXXXX-XX"
$newEndpoint = "Microsoft.ServiceBus"
Set-AzContext -Subscription $subscription
foreach($snet in $subnets){
Write-Host "Modifying Service Endpoints for subnet: $snet" -fore red -back white
$virtualNetwork = Get-AzVirtualNetwork -Name $vnetName -ResourceGroupName $vnetRgName | Get-AzVirtualNetworkSubnetConfig -Name $snet
$addrPrefix = $virtualNetwork.AddressPrefix
#Get existing service endpoints
$ServiceEndPoint = New-Object 'System.Collections.Generic.List[String]'
$virtualNetwork.ServiceEndpoints | ForEach-Object { $ServiceEndPoint.Add($_.service) }
if ($ServiceEndPoint -notcontains $newEndPoint){
$ServiceEndPoint.Add($newEndpoint)
}
$delegation=$virtualNetwork.Delegations
#Add new service endpoint
Get-AzVirtualNetwork -Name $vnetName -ResourceGroupName $vnetRgName | Set-AzVirtualNetworkSubnetConfig -Name $snet -AddressPrefix $addrPrefix -ServiceEndpoint $ServiceEndPoint -Delegation $delegation | Set-AzVirtualNetwork
}
Reference : Azure Powershell - Applying multiple service endpoints to a subnet

Error while creating NIC in Azure Powershell, just because of invalid Private IP address,

This was what I wrote:
$server1nic = New-AzNetworkInterface -ResourceGroupName $rgname1 -Name "L-firstyear-ec-nic-02" -Location $location -Subnet $vnet1.subnets[0] -PublicIpAddress $server1pip -PrivateIpAddress "192.168.0.0/16"
Here, $rgname1 is resource group name; $location is chosen as eastasia; $vnet1 is a virtual network which has 4 subnets; $server1pip is public IP address.
And the error it showed:
New-AzNetworkInterface: Private static IP address value 192.168.0.0/16 is invalid.
StatusCode: 400
ReasonPhrase: Bad Request
ErrorCode: InvalidPrivateIPAddressFormat
ErrorMessage: Private static IP address value 192.168.0.0/16 is invalid.
OperationID : 2f4a9969-53a1-4ec2-984f-ffe165877c93
The value for the parameter -PrivateIpAddress should be like 192.168.0.x in the command you use. It must be a certain IP address in the subnet that you use. For example:
$server1nic = New-AzNetworkInterface -ResourceGroupName $rgname1 -Name "L-firstyear-ec-nic-02" -Location $location -Subnet $vnet1.subnets[0] -PublicIpAddress $server1pip -PrivateIpAddress "192.168.0.10"

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.

Number of avaliable ips in azure subnet

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

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