azureazure-virtual-machineazure-vm-templates

getting Cannot parse the request InvalidRequestFormat when creating azure vm


I am running below script to create azure vm

Connect-AzAccount

$cred = Get-Credential

$vm = New-AzVMConfig -VMName "MyVirtualVM" -VMSize "Standard_D1"

$vm = Set-AzVMOperatingSystem -VM $vm -Windows -ComputerName "MyVM" -Credential $cred -ProvisionVMAgent -EnableAutoUpdate

$vm = Set-AzVMSourceImage -VM $vm -PublisherName "MicrosoftWindowsServer" -Offer "WindowsServer" -Skus "2016-Datacenter" -Version "latest"

$vm = Set-AzVMOSDisk -VM $vm -Name "MyOSDisk" -DiskSizeInGB 128 -CreateOption FromImage -Caching ReadWrite

$vm = Add-AzVMNetworkInterface -VM $vm -Id "/subscriptions/{subscriptionid}/resourceGroups/myresource/providers/Microsoft.Network/virtualNetworks/myresource-vnet"

New-AzVM -ResourceGroupName "Prod-IT" -Location "EastUS" -VM $vm

After running the script getting below error

New-AzVM : Cannot parse the request.
ErrorCode: InvalidRequestFormat
ErrorMessage: Cannot parse the request.
ErrorTarget: 
StatusCode: 400
ReasonPhrase: 
OperationID : 22637114-2ddd-49c2-a4b1-3748ac166e12
At line:15 char:1
+ New-AzVM -ResourceGroupName "Prod-IT" -Location "EastUS" -VM $vm
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [New-AzVM], ComputeCloudException
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.Compute.NewAzureVMCommand

Please help where I am going wrong. Thanks


Solution

  • New-AzVM : Cannot parse the request.

    When I try to execute your script, I also get the same error as below.

    enter image description here

    The error you are encountering is due to passing an invalid NIC ID

    To resolve the issue, ensure that you pass the valid NIC ID under the command Add-AzVMNetworkInterface -VM $vm -Id. If there is no NIC, create a new one and attach it to the VM.

    Here is the updated PowerShell Script to create a VM

        Connect-AzAccount
        
        $cred = Get-Credential
        
        $vnet = Get-AzVirtualNetwork -Name "myresource-vnet" -ResourceGroupName "myresource"
        
        $subnet = Get-AzVirtualNetworkSubnetConfig -Name "default" -VirtualNetwork $vnet
        
        $nic = New-AzNetworkInterface -Name "MyNIC" -ResourceGroupName "Prod-IT" -Location "EastUS" -Subnet $subnet
        
        $vm = New-AzVMConfig -VMName "MyVirtualVM" -VMSize "Standard_D1"
        
        $vm = Set-AzVMOperatingSystem -VM $vm -Windows -ComputerName "MyVM" -Credential $cred -ProvisionVMAgent -EnableAutoUpdate
        
        $vm = Set-AzVMSourceImage -VM $vm -PublisherName "MicrosoftWindowsServer" -Offer "WindowsServer" -Skus "2016-Datacenter" -Version "latest"
        
        $vm = Set-AzVMOSDisk -VM $vm -Name "MyOSDisk" -DiskSizeInGB 128 -CreateOption FromImage -Caching ReadWrite
        
        $vm = Add-AzVMNetworkInterface -VM $vm -Id $nic.Id
        
        New-AzVM -ResourceGroupName "Prod-IT" -Location "EastUS" -VM $vm
    

    Output:

    enter image description here

    After running the PowerShell script, the VM has been created successfully.

    enter image description here