0

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?

1
  • The error you're getting above is due to adding Add-AzVMSshPublicKey while you have already specified a username and password. Adding SSH keys in this case is causing the error Commented Dec 24, 2024 at 6:42

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Venkat, this solved the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.