Search code examples
azureazure-powershell

Azure: Creating Linux VM returns Required parameter 'osProfile.computerName' is missing (null)


I'm trying to run a very trivial PowerShell script to create a Linux VM in Azure.

Script looks like this:

$LocationName = "switzerlandnorth"
$ResourceGroupName = "my_group"
$SecurePassword = ConvertTo-SecureString "secret" -AsPlainText -Force
$Credential = New-Object System.Management.Automation.PSCredential ("user1", $SecurePassword);

$VirtualMachine = New-AzVMConfig -VMName "testvm" -VMSize "Standard_B2s"
$ComputerName = "testvm-dec2024"
$VirtualMachine = Add-AzVMNetworkInterface -VM $VirtualMachine -Id "/subscriptions/xxxxxx/resourceGroups/Production/providers/Microsoft.Network/networkInterfaces/yyyyy"
$VirtualMachine = Add-AzVMSshPublicKey -VM $VirtualMachine -Path "/home/xxxx/.ssh/authorized_keys" -KeyData "xxxxxx"
$VirtualMachine = Set-AzVMOperatingSystem -VM $VirtualMachine -Linux -ComputerName "test" -Credential $Credential
$VirtualMachine = Set-AzVMSourceImage -VM $VirtualMachine -PublisherName "canonical" -Offer "0001-com-ubuntu-minimal-jammy" -sku "minimal-22_04-lts-gen2" -Version "latest"

New-AzVM -VM $VirtualMachine -ResourceGroupName $ResourceGroupName -Location $LocationName -Verbose

I'm getting this error, which I suspect doesn't reflect the actual cause of the failure, given that ComputerName is defined as a hardcoded string:

Line |
  20 |  New-AzVM -VM $VirtualMachine -ResourceGroupName $ResourceGroupName -L …
     |  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Required parameter 'osProfile.computerName' is missing (null). ErrorCode: InvalidParameter ErrorMessage: Required parameter 'osProfile.computerName' is missing (null). ErrorTarget: 
     | osProfile.computerName StatusCode: 400 ReasonPhrase:  OperationID : xxxxxx

I've reinstalled the Azure PS modules from scratch, just to make sure there wasn't an issue with the version I was using.

Has anybody bumped into a similar issue before?


Solution

  • The error you encountered occurred because both the username and password were provided along with the SSH key. You cannot use both methods at the same time; you must choose either the username and password or the SSH key

    Here is the updated PowerShell script to create an azure Linux VM

     $LocationName = "Australia East"                                                                                                                                          
     $ResourceGroupName = "venkat-RG"
     $SecurePassword = ConvertTo-SecureString -String "Welcome@123$" -AsPlainText -Force
     $Credential = New-Object System.Management.Automation.PSCredential ("Venkat", $SecurePassword); 
     $ComputerName = "ContosoVM122"
     $VirtualMachine = New-AzVMConfig -VMName "testvm" -VMSize "Standard_B2s"
     $VirtualMachine = Add-AzVMNetworkInterface -VM $VirtualMachine -Id "/subscriptions/sub_ID/resourceGroups/venkat-RG/providers/Microsoft.Network/networkInterfaces/linux-vm"
     #$VirtualMachine = Add-AzVMSshPublicKey -VM $VirtualMachine -KeyData "xxxx" -Path "/home/vallepu/.ssh/authorized_keys"
     $VirtualMachine = Set-AzVMOperatingSystem -VM $VirtualMachine -Linux -ComputerName $ComputerName -Credential $Credential
     $VirtualMachine = Set-AzVMSourceImage -VM $VirtualMachine -PublisherName "canonical" -Offer "0001-com-ubuntu-minimal-jammy" -sku "minimal-22_04-lts-gen2" -Version "latest"
     New-AzVM -VM $VirtualMachine -ResourceGroupName $ResourceGroupName -Location $LocationName -Verbose
    

    Output:

    enter image description here

    Reference: Set operating system properties for a new Linux virtual machine