Why is the ID for a subnet created using Azure Resource Manager Powershell cmdlets null? - azure

I am trying to provision some network resources in Azure and I am running into a blocker with a step that tries to reference a previously created subnet's Id property.
Here's the command to retrieve the subnet details:
$gwsubnet = Get-AzureRmVirtualNetworkSubnetConfig -Name 'GatewaySubnet' -VirtualNetwork $vnet
And then outputting the value of $gwsubnet in the console:
Name : GatewaySubnet
Id :
Etag :
ProvisioningState :
AddressPrefix : 10.1.1.0/24
IpConfigurations : null
NetworkSecurityGroup : null
RouteTable : null
Note that the Id property is null. This causes an error for a future step, e.g.:
$gwipconfig = New-AzureRmVirtualNetworkGatewayIpConfig -Name config1 -SubnetId $gwsubnet.Id -PublicIpAddressId $gwpip.Id
Gives the following error:
New-AzureRmVirtualNetworkGatewayIpConfig : Cannot validate argument on parameter 'SubnetId'. The argument is null or empty. Supply an argument that is not null or empty and then try the
command again.
At line:1 char:99
+ ... nfig -SubnetId $gwsubnet.Id -PublicIpAddressId $gwpip.Id
+ ~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [New-AzureRmVirtualNetworkGatewayIpConfig], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Azure.Commands.Network.NewAzureVirtualNetworkGatewayIpConfigCommand
I'm struggling with how to have my subnet have an Id assigned to it. Is this something that I need to logout/login again to see? thanks for the help.

The problem you are having is that the subnet Id isn't created until you have created the virtual network.
When you run Get-AzureRmVirtualNetworkSubnetConfig all you create is a configuration to attach to New-AzureRmVirtualNetwork Once you have run that command it will connect to Azure, deploy the Vnet, create the subnet and at that state it will create a subnet Id.
The following code will show you what I mean...
$DNSNameLabel = "mydnsname"
$NetworkName = "MyNet"
$NICName = "MyNIC"
$PublicIPAddressName = "MyPIP"
$SubnetName = "MySubnet"
$SubnetAddressPrefix = "10.0.0.0/24"
$VnetAddressPrefix = "10.0.0.0/16"
$SingleSubnet = New-AzureRmVirtualNetworkSubnetConfig `
-Name $SubnetName `
-AddressPrefix $SubnetAddressPrefix
#At this point nothing is created, so there is no valid subnet id
$Vnet = New-AzureRmVirtualNetwork -Name $NetworkName `
-ResourceGroupName $ResourceGroupName `
-Location $LocationName `
-AddressPrefix $VnetAddressPrefix `
-Subnet $SingleSubnet
#vnet and subnet are created, both get an id and etag
$vnet = Get-AzureRmVirtualNetwork -Name $NetworkName
-ResourceGroupName $ResourceGroupName
$vnet.Subnets[0].Id
Which will give
/subscriptions/{Subscription ID}/resourceGroups/MyResourceGroup/providers/Microsoft.Network/virtualNetworks/MyNet/subnets/MySubnet

Related

argument is null or empty - start-job

I am getting an argument is null error while trying to execute below command. Is there a way to can call the variable inside start-job
$RG = "xx"
$Location1 = "xx"
$VNET1="xx"
$PublicIP1="xx"
$VNGW1 = "xx"
Start-Job {
New-AzVirtualNetworkGateway -Name $VNGW1 -ResourceGroupName $RG -Location $Location1 -GatewayType Vpn -VpnType RouteBased -GatewaySku VpnGw1 -Asn 65511 -IpConfigurations $gwipconf1 -EnableBgp $True
}
Cannot validate argument on parameter 'Name'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
Cannot validate argument on parameter 'Name'. The argument is null or
empty. Provide an argument that is not null or empty, and then try the
command again.
Without the Argument list in Start-Job cmdlet, we cannot use the variables.
We have created a PowerShell script & tested the below scripts in our environment which is working fine. We can create Virtual Network Gateway using start-job in below two ways:
Using Variables as Argument List
Using Script in Start-Job
Using Variables as Argument List:
Firstly, We have created all the dependency resources for Virtual Network Gateway using below cmdlets:
$subnet = New-AzVirtualNetworkSubnetConfig -Name 'gatewaysubnet' -AddressPrefix '10.254.0.0/27'
$ngwpip = New-AzPublicIpAddress -Name ngwpip -ResourceGroupName "vnet-gateway" -Location "UK West" -AllocationMethod Dynamic
$vnet = New-AzVirtualNetwork -AddressPrefix "10.254.0.0/27" -Location "UK West" -Name vnet-gateway -ResourceGroupName "vnet-gateway" -Subnet $subnet
$subnet = Get-AzVirtualNetworkSubnetConfig -name 'gatewaysubnet' -VirtualNetwork $vnet
$ngwipconfig = New-AzVirtualNetworkGatewayIpConfig -Name ngwipconfig -SubnetId $subnet.Id -PublicIpAddressId $ngwpip.Id
Once the dependencies are created, we used the below Start-Job cmdlet for the creation of Virtual Network Gateway:
$RG = "xx"
$Location = "xx"
$gwipconf = "xx"
$VNGW = "xx"
Start-Job {
New-AzVirtualNetworkGateway -Name $args[3] -ResourceGroupName $args[0] -Location $ args[1] -GatewayType Vpn -VpnType RouteBased -GatewaySku VpnGw1 -Asn 65511 -IpConfigurations $ args[2] -EnableBgp $True
} -ArgumentList #($RG, $Location, $gwipconf, $VNGW)
Using Script in Start-Job:
Here, we have created a PowerShell file (gateway.ps1) & stored in local machine which contains the dependent resources creation followed by virtual network gateway creation(using the start-job).
New-AzResourceGroup -Location "UK West" -Name "vnet-gateway"
$subnet = New-AzVirtualNetworkSubnetConfig -Name 'gatewaysubnet' -AddressPrefix '10.254.0.0/27'
$ngwpip = New-AzPublicIpAddress -Name ngwpip -ResourceGroupName "vnet-gateway" -Location "UK West" -AllocationMethod Dynamic
$vnet = New-AzVirtualNetwork -AddressPrefix "10.254.0.0/27" -Location "UK West" -Name vnet-gateway -ResourceGroupName "vnet-gateway" -Subnet $subnet
$subnet = Get-AzVirtualNetworkSubnetConfig -name 'gatewaysubnet' -VirtualNetwork $vnet
$ngwipconfig = New-AzVirtualNetworkGatewayIpConfig -Name ngwipconfig -SubnetId $subnet.Id -PublicIpAddressId $ngwpip.Id
New-AzVirtualNetworkGateway -Name myNGW -ResourceGroupName vnet-gateway -Location "UK West" -IpConfigurations $ngwIpConfig -GatewayType "Vpn" -VpnType "RouteBased" -GatewaySku "Basic" -CustomRoute 192.168.0.0/24
Now, We have passed the above powershell script file path as an argument to the Start-job to create VPN gateway:
start-job -filepath \<pathofPowershellScript>
Here is the sample output for the reference :

I need to Specify OS Disk Name while Creating Azure VMSS using PowerShell

I want to Specify OS Disk Name while Creating VMSS using PowerShell to overcome the random OS Disk name like "VMSSNAME_1686_disk1_b5f021da0ba7409fbe7d028bdd50".
Command :
Set-AzVmssStorageProfile $vmssConfig `
-OsDiskCreateOption "FromImage" `
-ImageReferenceId $galleryImage.Id -OsDiskName $OSDiskName
# Set up information for authenticating with the virtual machine
Set-AzVmssOsProfile $vmssConfig `
-AdminUsername $username `
-AdminPassword $Password `
-ComputerNamePrefix "UKSSPAG"
# Attach the virtual network to the config object
Add-AzVmssNetworkInterfaceConfiguration -VirtualMachineScaleSet $vmssConfig -Name "NICConfig" -Primary $true -IPConfiguration $ipConfig
# Create the scale set with the config object (this step might take a few minutes)
New-AzVmss -ResourceGroupName $resourceGroup -VMScaleSetName $scaleSetName -VirtualMachineScaleSet $vmssConfig
Error :
New-AzVmss : Parameter 'osDisk.name' is not allowed.
ErrorCode: InvalidParameter
ErrorMessage: Parameter 'osDisk.name' is not allowed.
ErrorTarget: osDisk.name
StatusCode: 400
ReasonPhrase: Bad Request
OperationID : e97450a3-ed89-46f8-84fa-6779a002a3e9
At line:1 char:1
+ New-AzVmss -ResourceGroupName $resourceGroup -VMScaleSetName $scaleSe ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [New-AzVmss], ComputeCloudException
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.Compute.Automation.NewAzureRmVmss

Cannot parse the request - MissingJsonReferenceId : Error while creating NIC using PowerShell Az command

I am using below Az PowerShell command to create the NIC for virtual machine.
# Create a NIC for the web server VM.
$nicVMweb = New-AzNetworkInterface -ResourceGroupName $rgName -Location $location `
-Name $VmFrontendNICCardName -PublicIpAddress $publicipvm1 `
-NetworkSecurityGroup $nsgfe -Subnet $virtualNetwork.Subnets[0]
Cannot parse the request. StatusCode: 400 ReasonPhrase: Bad Request ErrorCode: InvalidRequestFormat ErrorMessage: Cannot parse the request. Additional details: Code: MissingJsonReferenceId Message: Value for
| reference id is missing. Path properties.ipConfigurations[0].properties.subnet. OperationID : 78525e42-a036-460f-10f9-5b993b7ca5e6
Issue Resolved by Below PowerShell
$Subnet = Get-AzVirtualNetwork -Name $virtualNetworkName -ResourceGroupName $rgName
$IPconfig = New-AzNetworkInterfaceIpConfig -Name $VmFrontendIpConfigName -PrivateIpAddressVersion IPv4 -PrivateIpAddress "10.0.0.10" -SubnetId $Subnet.Subnets[0].Id
$nicVMweb = New-AzNetworkInterface -Name $VmFrontendNICCardName -ResourceGroupName $rgName -Location $location -IpConfiguration $IPconfig
What is the issue in first command?
Could not reproduce your issue, your first command works fine on my side.
$virtualNetwork = Get-AzVirtualNetwork -Name "<vnet-name>" -ResourceGroupName "<group-name>"
$publicipvm1 = Get-AzPublicIpAddress -ResourceGroupName <group-name> -Name joyvm-ip2
$nsgfe = Get-AzNetworkSecurityGroup -Name joyvm-nsg -ResourceGroupName <group-name>
$nicVMweb = New-AzNetworkInterface -ResourceGroupName <group-name> -Location "West US 2" -Name "joyinter" -PublicIpAddress $publicipvm1 -NetworkSecurityGroup $nsgfe -Subnet $virtualNetwork.Subnets[0]
For this issue, it may be related to the version of Az.Network module, I use the 3.3.0 version, try to update it to the latest version:
Update-Module -Name Az.Network

Azure Powershell command cannot find my VM under my resource group?

I'm trying to change the TCP Port timeout on my Ubuntu VM in Azure, and am following this guide to do so, however I seem to get stuck at Step 8 where I have to type the following command:
Get-AzureRmVM -Name "digitron" -ResourceGroup "DIGITRON-RG" |
Get-AzureRmPublicIpAddress
Where it spits back the following error:
Get-AzureRmPublicIpAddress : The Resource 'Microsoft.Network/publicIPAddresses/digitron' under resource group
'DIGITRON-RG' was not found.
StatusCode: 404
ReasonPhrase: Not Found
OperationID : '5031da35-262c-4e1a-a85b-885a6a0fd36c'
At line:1 char:63
+ ... "digitron" -ResourceGroup "DIGITRON-RG" | Get-AzureRmPublicIpAddress
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [Get-AzureRmPublicIpAddress], NetworkCloudException
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.Network.GetAzurePublicIpAddressCommand
What's strange here is if I run the command Get-AzureRmVm, the powershell will spit back:
ResourceGroupName Name Location VmSize OsType NIC ProvisioningState
DIGITRON-RG digitron eastus Standard_DS2_v2 Linux digitron727 Succeeded
Now reading the error makes me think that the VM itself has no public IP address, but I've set it in the Azure Portal as seen in this image (where it says 40.71.98.172 etc):
Why is the Powershell giving me this error?
The Resource 'Microsoft.Network/publicIPAddresses/digitron' under
resource group 'DIGITRON-RG' was not found.
Because Get-AzureRmPublicIpAddress can't get the right parameter, this error means the resource can't find in DIGITRON-RG. For test, we can use Get-AzureRmResource to test the resource exist or not:
PS > Get-AzureRmResource | ?{$_.name = "digitron"}
By the way, the command Get-AzureRmPublicIpAddress need parameters: -Name (the name of the public IP address), -ResourceGroupName (the name of the resource group)
PS > Get-AzureRmPublicIpAddress -Name "jason-ip" -ResourceGroupName "jason"
Name : jason-ip
ResourceGroupName : jason
Location : eastus
Id : /subscriptions/5xxxxabb-222b-49c3-9488-0361e29a7b15/resourceGroups/jason/providers/Microsoft.Network/publicIPAddresses/jason-ip
Etag : W/"5a7200b2-7c2b-4c7a-be27-0bbb7c8f4665"
ResourceGuid : 32xxxf-750a-46a4-abda-c25xxx2b64
ProvisioningState : Succeeded
Tags :
PublicIpAllocationMethod : Dynamic
IpAddress : 13.92.255.103
PublicIpAddressVersion : IPv4
IdleTimeoutInMinutes : 30
IpConfiguration : {
"Id": "/subscriptions/5xxxxb-222b-49c3-9xx8-0361e29a7b15/resourceGroups/jason/providers/Microsoft.Network/networkInterfaces/jason647/ipConfigurations/ipconfig1"
}
DnsSettings : null
We can use this command to get the public IP direct, and increase the timrout to 30 minutes.
Below is the script I used to get the Private and Public IP for an Azure ARM VM:
$rg = Get-AzureRmResourceGroup -Name "MyResourceGroup01"
$vm = Get-AzureRmVM -ResourceGroupName $rg.ResourceGroupName -Name "MyVM01"
$nic = Get-AzureRmNetworkInterface -ResourceGroupName $rg.ResourceGroupName -Name $(Split-Path -Leaf $VM.NetworkProfile.NetworkInterfaces[0].Id)
$nic | Get-AzureRmNetworkInterfaceIpConfig | Select-Object Name,PrivateIpAddress,#{'label'='PublicIpAddress';Expression={Set-Variable -name pip -scope Global -value $(Split-Path -leaf $_.PublicIpAddress.Id);$pip}}
(Get-AzureRmPublicIpAddress -ResourceGroupName $rg.ResourceGroupName -Name $pip).IpAddress
#Output:
Name PrivateIpAddress PublicIpAddress
---- ---------------- ---------------
ipconfig1 10.0.0.10 MyVM01-pip
40.80.217.1

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