Andrius Naruševičius
Andrius Naruševičius

Reputation: 8578

How to attach OSDisk and change OSProfile for Azure VM

I am setting up a VM restoration pipeline. It looks like this:

I Copy the OS disk

$diskConfig = New-AzureRmDiskConfig -AccountType $storageType `
    -Location $snapshot.Location -SourceResourceId $snapshot.Id -CreateOption Copy
$disk = New-AzureRmDisk -Disk $diskConfig -ResourceGroupName $resourceGroupName `
    -DiskName $diskName

I then Attach it to my VM like so:

$vmConfig = New-AzureRmVMConfig -VMName $virtualMachineName `
    -VMSize $virtualMachineSize
$vmConfig = Set-AzureRmVMOSDisk -VM `
    $vmConfig -ManagedDiskId $disk.Id -CreateOption Attach -Windows

However I also want to indirectly modify the OSProfile property of the VM by binding a certificate to it as I am following this guide by Microsoft. I do:

$vmConfig = Add-AzureRmVMSecret -VM $vmConfig -SourceVaultId $vaultId `
    -CertificateStore $certificateStore -CertificateUrl $certificateUrl

And now when I try to finalize the creation by

New-AzureRmVM -VM $vmConfig -ResourceGroupName $resourceGroupName -Location $location

I get an error:

New-AzureRmVM : Parameter 'osProfile' is not allowed.

I am aware that it modifies OSProfile.Secrets (that is - adds a new record to the list), but there is a restriction that I cannot edit it whatsover. I also tried doing it by creating a VM first and then adding those Secrets, but it gives me the almost the same error

Update-AzureRmVM : Changing property 'osProfile' is not allowed.

By the way, if I use FromImage instead of Attach, I get error:

New-AzureRmVM : Cannot specify user image overrides for a disk already defined in the specified image reference.

How can I solve this?

Upvotes: 0

Views: 2622

Answers (3)

user1888467
user1888467

Reputation: 86

Provision a new VM from an image that has the closest matching osProfile that you want. Go ahead and let it create a new OS disk.

After the VM is fully provisioned, go to the VM in Azure Portal and stop the VM. Then go to the Disks settings in the VM's menu and click Swap OS Disk. Pick your desired OS disk. Note: The disk you pick might have to closely match the size and type of the existing OS disk. I didn't test that.

osProfile will remain on the VM. Worked for me at least. Hope it helps someone else.

Upvotes: 0

Andrius Naruševičius
Andrius Naruševičius

Reputation: 8578

I ended up creating a powershell script that executes a powershell command (using Invoke-AzureRmVMRunCommand) on the VM which:

  1. Retrieves certificates

    Get-AzureKeyVaultSecret -VaultName $keyVaultName -name (Get-AzureKeyVaultSecret -VaultName $keyVaultName).name

  2. Downloads and binds those certificates to IIS
  3. Creates an ssl binding and assigns it to https binding

    Get-ChildItem cert:\localmachine\My | New-Item -Path IIS:\SslBindings\!443

Upvotes: 1

4c74356b41
4c74356b41

Reputation: 72191

Pretty sure this error means that you are trying to use existing disk to create vm. you cannot modify the os settings in this case. only when creating from new disk.

Upvotes: 0

Related Questions