1
votes

I created an Azure Virtual Machine successfully through the https://portal.azure.com/ user interface. I started working on an Azure Powershell script to automate the creation of similar VMs and ended up with the function below:

function Create-CentOSVirtualMachine
{
    Set-AzureSubscription -SubscriptionName Pay-As-You-Go -CurrentStorageAccount "thefoobar"

    Write-Output "Searching for the latest CentOS image..."

    $image = (Get-AzureVMImage | Where-Object { $_.Label -like "*CentOS*" } | select -last 1)

    $imageName = $image.ImageName
    $imageLabel = $image.Label
    $dataDiskSizeInGB = 30
    $diskLabel = "thefoobar-data"
    $mediaOsLocation = "https://thefoobar.blob.core.windows.net/vhds/thefoobar-os.vhd"
    $mediaDataLocation = "https://thefoobar.blob.core.windows.net/vhds/thefoobar-data.vhd"

    Write-Output "Creating CentOS virtual machine from:" $imageLabel

    $user = "thefoobardev"
    $password = "thefoobarpassword"

    $vmConfig = New-AzureVMConfig -Name "thefoobar" -MediaLocation $mediaOsLocation -InstanceSize Basic_A1 -ImageName $imageName

    $vm = $vmConfig | Add-AzureProvisioningConfig -Linux -LinuxUser $user -Password $password | Add-AzureDataDisk -CreateNew -DiskSizeInGB $dataDiskSizeInGB -DiskLabel $diskLabel -LUN 0 -MediaLocation $mediaDataLocation

    $vm | Add-AzureEndpoint -Name 'ElasticSearchHTTP' -LocalPort 9200 -PublicPort 9200 -Protocol tcp
    $vm | Add-AzureEndpoint -Name 'ElasticSearchTransport' -LocalPort 9300 -PublicPort 9300 -Protocol tcp
    $vm | Set-AzureEndpoint -Name 'SSH' -LocalPort 22 -PublicPort 22 -Protocol tcp

    New-AzureVM -ServiceName "thefoobar" -VNetName "MyFoobarNetwork" -VMs $vm -Location "East US"
}

To me, it appears to match what I do through the user interface (though I renamed some strings in the script to "foobar" for this public question). The script runs without any errors, and the dashboard appears to show my VM exactly as I expect it.

However, with creation of the script, I cannot connect via PuTTY:

enter image description here

This is normally all I would need to fill out to connect after creating the VM through the GUI. Using my script lets me connect, but shows an error after typing in the user:

enter image description here

This is how I enter the security details through the portal UI:

enter image description here

When creating from the portal, I would instead be asked for the password for the user when PuTTYing. Is there anything missing from my Powershell script that would help me connect as I am able to when creating the VM from the portal?


I tried using some of the extra Linux parameters shown in https://msdn.microsoft.com/en-us/library/azure/dn495299.aspx.

If I add -NoSSHEndpoint or -DisableSSH, that simply creates my VM similarly, but with one less endpoint for SSH. And if I add the endpoint back manually, I still get the same error message. If I add -NoSSHPassword, I get the error New-AzureVM : BadRequest: The UserPassword property must be specified.

1
I had the same problem after I've created a VM using chef / puppet. It seems that the certificate is not being copied to the VM, the reason why you can't connect. I could not create a VM specifying username / password too. Unfortunately, I could not find one way to solve that.Thiago Custodio

1 Answers

0
votes

The following line from my existing code snippet returns an image by RightImage called 0b11de9248dd4d87b18621318e037d37__RightImage-CentOS-7.0-x64-v14.2.

$image = (Get-AzureVMImage | Where-Object { $_.Label -like "*CentOS*" } | select -last 1)

I tried a different CentOS image by OpenLogic and my existing PowerShell script worked:

Get-AzureVMImage | Where-Object { $_.ImageName -like "*OpenLogic-CentOS*" } | select -last 1

It gives me the image 5112500ae3b842c8b9c604889f8753c3__OpenLogic-CentOS-70-20150128.

Not sure what in particular prevents the RightImage image from allowing a remote connection.