Hi Guys,
I am trying to create a script with task and one of the tasks is to create new azure vm. Everything is good until it comes to the part when it needs to create VMConfig. This is the error messsage I receive and under that I posted a code.
Cannot index into a null array.
-
New-AzureRmNetworkInterface -Name $VMNIC -ResourceGroupName $ ...
-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- CategoryInfo : InvalidOperation: (
, RuntimeException
- FullyQualifiedErrorId : NullArray
- CategoryInfo : InvalidOperation: (
Add-AzureRmVMNetworkInterface : Cannot validate argument on parameter ‘Id’. The argument is null or empty. Provide an argument that
is not null or empty, and then try the command again.
-
Add-AzureRmVMNetworkInterface -Id $VMNIC.Id
-
~~~~~~~~~
- CategoryInfo : InvalidData: (
[Add-AzureRmVMNetworkInterface], ParameterBindingValidationException
- FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Azure.Commands.Compute.AddAzureVMNetworkInterfaceCommand
- CategoryInfo : InvalidData: (
New-AzureRmVM : Cannot validate argument on parameter ‘VM’. The argument is null or empty. Provide an argument that is not null or
empty, and then try the command again.
- … M -ResourceGroupName $ResourceGroup -Location $Location -VM $VMConfig
-
~~~~~~~~~
- CategoryInfo : InvalidData: (
[New-AzureRmVM], ParameterBindingValidationException
- FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Azure.Commands.Compute.NewAzureVMCommand
- CategoryInfo : InvalidData: (
–> CAN SOMEONE PLEASE HELP ME WITH THIS ARRAY. VARIABLES ARE NOT EMPTY. HERE IS THE CODE
CODE
25 { $subscription = Read-Host “Specify subscription where you want to create new Resource Group”
Select-AzureRmSubscription -Subscription $subscription
$ResourceGroup = Read-Host 'Enter the existing Resource Group or create a new one'
$Location = Read-Host 'Enter your location'
$vmcred = Get-Credential -Message "Type the name and password of the VM local administrator account."
$Publisher = Read-Host "Enter the publisher name (Example : MicrosoftWindowsServer)"
$Offer = Read-Host " Specify Offer (Example: WindowsServer) "
$SKU = Read-Host " Specify SKU (Example: 2016-Datacenter) "
$VMName = Read-Host " Specify VM Name"
$VMSize = Read-Host " Specify VM Size (Example: Standard_DS_v2)"
$subnet = Read-Host "Enter the existing subnet or create a new one"
$VNET = Read-Host "Enter the existing VNET or create a new one"
if(!$resourceGroup){
Write-Host "Resource group '$resourceGroupName' does not exist. To create a new resource group, please enter a location.";
if(!$Location) {
$Location = Read-Host "resourceGroupLocation";
}
Write-Host "Creating resource group '$ResourceGroup' in location '$Location'";
New-AzureRmResourceGroup -Name $ResourceGroup -Location $Location
}
else{
Write-Host "Using existing resource group '$resourceGroupName'";
}
if(!$subnet){
$subnet = Read-Host 'Enter the existing subnet or create a new one'
if(!$subnet) {
Write-Host "Subnet '$subnet' does not exist. To create a new one, please press enter.";
$subnet = Read-Host "Subnet";
}
Write-Host "Creating new subnet '$subnet'";
New-AzureRmVirtualNetworkSubnetConfig -Name $subnet
}
else{
Write-Host "Using existing subnet config '$subnet'";
}
if(!$VNET){
$VNET = Read-Host 'Enter the existing VNET or create a new one'
if(!$VNET) {
Write-Host "VNET '$VNET' does not exist. To create a new one, please press enter.";
$VNET = Read-Host "VNET NAME";
}
Write-Host "Creating new VNET '$VNET'";
New-AzureRmVirtualNetwork -Name $VNET -ResourceGroupName $ResourceGroup `
-Location $Location -AddressPrefix -Subnet $subnet
}
else{
Write-Host "Using existing VNET '$VNET'";
}
$PublicIp = Read-Host "Enter the Public IP Name"
New-AzureRmPublicIpAddress -Name $PublicIp -ResourceGroupName $ResourceGroup `
-Location $Location -AllocationMethod Dynamic
------------(HERE IT STARTS TO FAIL.EVERYTHING ABOVE IS WORKING AS IT SHOULD-----------------------)
$VMNIC = Read-Host 'Enter the NIC Name'
New-AzureRmNetworkInterface -Name $VMNIC -ResourceGroupName $ResourceGroup `
-Location $Location -SubnetId $VNET.Subnets[0].Id -PublicIpAddressId $PublicIp.Id
$VMConfig = New-AzureRmVMConfig -VMName $VMName -VMSize $VMSize | `
Set-AzureRmVMOperatingSystem -Windows -ComputerName $VMname -Credential $VMCred | `
Set-AzureRmVMSourceImage -PublisherName $Publisher -Offer $Offer -Skus $SKU -Version 'latest' | `
Add-AzureRmVMNetworkInterface -Id $VMNIC.Id
# Create the virtual machine
New-AzureRmVM -ResourceGroupName $ResourceGroup -Location $Location -VM $VMConfig
}
}
}