Managed Disk - PowerShell - azure

I am trying to create a new Managed disk VM with PowerShell. By default it creates premium disk and also generate unique string at the end of disk name. I want to create disk as standard disk and also want to set the name for disk.
I found the command Set-AzureRmosdisk but ita not working with New-AzureRmVMConfig command.
Can someone please help me how to do achieve this ?

Not sure what's the structure of your script is but the key link to get a standard disk is the Set-AzureRMVMOSDisk and the parameter you need to set is StorageAccountType
Set-AzureRmVMOSDisk -VM $vm -Name $osDiskName -CreateOption FromImage -Windows -StorageAccountType Standard_LRS
For Standard you need to specify StorageAccountType to be either StandardSSD_LRS or Standard_LRS

Found the proper way from below article:
https://savilltech.com/2018/03/17/deploying-an-azure-iaas-vm-using-powershell/

Related

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?
I ended up creating a powershell script that executes a powershell command (using Invoke-AzureRmVMRunCommand) on the VM which:
Retrieves certificates
Get-AzureKeyVaultSecret -VaultName $keyVaultName -name (Get-AzureKeyVaultSecret -VaultName $keyVaultName).name
Downloads and binds those certificates to IIS
Creates an ssl binding and assigns it to https binding
Get-ChildItem cert:\localmachine\My | New-Item -Path IIS:\SslBindings\!443
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.
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.

Attaching restored OS disk to existing VM

I have one VM with a daily backup scheduled. Today I deleted a file in that VM and changed some configuration. I restored yesterday's data disk from my recovery service vault and changed the names of the recovered data disk.
Now I want to attach yesterday's restored backup to my existing VM. Is it possible?
If not then suppose I delete my VM but I keep its network interface card. I can create a new VM from restored VHDs using ARM templates but how can I assign an existing NIC to my new VM?
Also, I have added this VM to my domain controller. If I recreate the VM, do I need to add the new VM to the domain controller or will it work normally?
Now I want to attach yesterday's restored backup to my existing VM. is
it possible?
Yes, we can attach this restore disk to your existing VM, then we can find the disk in your existing VM.
I delete VM but I keep network interface card for the VM, now I can
create VM from restored VHD's using ARM templates but how to assign
exiting NIC in the new VM?
Yes, we can use PowerShell to create a VM with existing NIC and VHD, here is an example:
$rgname = "jason-newgroup"
$loc = "japaneast"
$vmsize = "Standard_DS1_v2"
$vmname = "jason-newtest2"
$vm = New-AzureRmVMConfig -VMName $vmname -VMSize $vmsize
$nic = Get-AzureRmNetworkInterface -Name "NICname" -ResourceGroupName $rgname
$nicId = $nic.Id
$vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $nicId
$osDiskName = "jason-newtest"
$osDiskVhdUri = "https://jasonnewgroupdisks912.blob.core.windows.net/vhds/jason-newtest201681285042.vhd"
$vm = Set-AzureRmVMOSDisk -VM $vm -VhdUri $osDiskVhdUri -name $osDiskName -CreateOption attach -Windows
New-AzureRmVM -ResourceGroupName $rgname -Location $loc -VM $vm
if I recreate the VM do I need to add new VM to domain controller or
will it work normally?
Yes, the new create VM (restore) will add to the domain controller, we don't need to add the VM to domain controller again.

How to increase size of OS disk in Windows Azure using Powershell

I want to increase size of OS disk in Windows Azure using Powershell or any other tool. Please help
Regards
Umair
Resize the OS drive
Open your Powershell ISE or Powershell window in administrative mode and follow the steps below:
Sign-in to your Microsoft Azure account in resource management mode and select your subscription as follows:
Login-AzureRmAccount
Select-AzureRmSubscription –SubscriptionName 'my-subscription-name'
Set your resource group name and VM name as follows:
$rgName = 'my-resource-group-name'
$vmName = 'my-vm-name'
Obtain a reference to your VM as follows:
$vm = Get-AzureRmVM -ResourceGroupName $rgName -Name $vmName
Stop the VM before resizing the disk as follows:
Stop-AzureRmVM -ResourceGroupName $rgName -Name $vmName
And here comes the moment we’ve been waiting for! Set the size of the OS disk to the desired value and update the VM as follows:
$vm.StorageProfile.OSDisk.DiskSizeGB = 1023
Update-AzureRmVM -ResourceGroupName $rgName -VM $vm
The new size should be greater than the existing disk size. The maximum allowed is 1023 GB.
Updating the VM may take a few seconds. Once the command finishes executing, restart the VM as follows:
Start-AzureRmVM -ResourceGroupName $rgName -Name $vmName
And that’s it! Now RDP into the VM, open Computer Management (or Disk Management) and expand the drive using the newly allocated space.
pasted from: https://learn.microsoft.com/en-us/azure/virtual-machines/virtual-machines-windows-expand-os-disk
i have done this by using below command on windows azure powershell latest version. Please note old version of powershell doesnot support this command .
Update-AzureDisk -DiskName [Disk-Name] -Label [DiskLabel]-ResizedSizeInGB 1020

Azure Managed Disks. Access to underlying blob?

Would it be possible to access the blob in Azure managed disks? If say ,I needed to copy it to another storage account(regular storage account). Since managed storage only support LRS at the moment.
If say ,I needed to copy it to another storage account(regular storage
account).
You should understand the difference between managed disks and
unmanaged disks. With unmanaged disks, you had to create storage
accounts to hold the disks (VHD files) for your Azure VMs. When
scaling up, you had to make sure you created additional storage
accounts so you didn’t exceed the IOPS limit for storage with any of
your disks. With Managed Disks handling storage, you are no longer
limited by the storage account limits (such as 20,000 IOPS / account).
You also no longer have to copy your custom images (VHD files) to
multiple storage accounts. You can manage them in a central location –
one storage account per Azure region – and use them to create hundreds
of VMs in a subscription. More information please refer to this
link.
Update:
You could copy managed disk to your private storage account by using the following cmdlets.
$sas = Grant-AzureRmDiskAccess -ResourceGroupName shui -DiskName shuitest -DurationInSecond 3600 -Access Read
$destContext = New-AzureStorageContext –StorageAccountName contosostorageav1 -StorageAccountKey 'YourStorageAccountKey'
Start-AzureStorageBlobCopy -AbsoluteUri $sas.AccessSAS -DestContainer 'vhds' -DestContext $destContext -DestBlob 'MyDestinationBlobName.vhd'
You can't copy to a regular storage account, but you can create copies of it to any location you want. Let's suppose the disk is in "eastus" and you want a copy in "brazilsouth"
Get disk:
$disk = Get-AzureRmDisk -ResourceGroupName $rgName -DiskName $diskName
Make a copy config to another location:
$location = "brazilsouth"
$snapshot = New-AzureRmSnapshotConfig -SourceUri $disk.Id -CreateOption Copy -Location $location
Create a snapshot:
New-AzureRmSnapshot -Snapshot $snapshot -SnapshotName $newDiskName -ResourceGroupName $rgName
All done! This way you can keep a secondary copy in another datacenter location.

How to start a azure deallocated vm?

I needed to do a vm backup and I followed this article:
https://azure.microsoft.com/en-us/documentation/articles/virtual-machines-linux-capture-image/
So, I executed:
azure vm shutdown vm-1
But now I really need to start the deallocated vm but I don't know how to do it.
When I try to execute this command:
Start-AzureVM -ServiceName "vm-1" -Name "vm-1"
I'm getting this message:
No deployment found in service: 'vm-1'.
And when I try to list all my vm, I don't see vm-1
Any idea of how to start a deallocated vm?
Thanks
Understood your problem. Your VM has been deleted by you and you want it back. Now in order to back your vm you need to make sure you have the vhds of the vm in place.
a. Find out the vhd and convert it to disk(OS Disk and data disk).
b. Use the OS disk's diskname to create a new vm using this powershell-
Set-AzureSubscription -SubscriptionId "xyz" -CurrentStorageAccountName "lmn"
$vm=New-AzureVMConfig -DiskName "OSDiskDiskName" -InstanceSize "InstanceSizeofvm" -Name "VMName"
New-AzureVM -Location "LocationName" -ServiceName "abc" -VNetName "vNetName" -VM $vm -verbose
c. Find out the data disk and attach to the newly created VM.
In case you have deleted the vhds as well, raise a ticket with MS, only they can help you in that case.

Resources