Creating OS disk from VHD stored in another storage account - azure-sdk-.net

Have an existing PowerShell script that is creating VM's using pre-created VHD's stored in a storage account (copied across regional storage accounts for speed).
In PS we can use the following:
New-AzureRmDisk -DiskName $osDiskName -Disk `
(New-AzureRmDiskConfig -AccountType Premium_LRS `
-Location $location -CreateOption Import `
-StorageAccountId $storageAccountId `
-SourceUri $osVHDUri) `
-ResourceGroupName $resourceGroupName
$osDisk = Get-AzureRMDisk -DiskName $osDiskName -ResourceGroupName $resourceGroupName
$VirtualMachine = Set-AzureRmVMOSDisk -VM $VirtualMachine -ManagedDiskId $osDisk.Id -CreateOption Attach -Windows -StorageAccountType Premium_LRS
Where $storageAccountId is similar to:
/subscriptions/{0}/resourceGroups/my-snapshot/providers/Microsoft.Storage/storageAccounts/mysnapshots -f $sourceSnapshotSubscriptionId
In the .net Azure SDK, I don't see a way to replicate this? When I try to create, it's saying unable to find, yet my PS works ok.

If you want to use an existing disk instead of a marketplace image, use this code:
var managedDisk = azure.Disks.Define("myosdisk")
.WithRegion(location)
.WithExistingResourceGroup(groupName)
.WithWindowsFromVhd("https://mystorage.blob.core.windows.net/vhds/myosdisk.vhd")
.WithSizeInGB(128)
.WithSku(DiskSkuTypes.PremiumLRS)
.Create();
azure.VirtualMachines.Define("myVM")
.WithRegion(location)
.WithExistingResourceGroup(groupName)
.WithExistingPrimaryNetworkInterface(networkInterface)
.WithSpecializedOSDisk(managedDisk, OperatingSystemTypes.Windows)
.WithExistingAvailabilitySet(availabilitySet)
.WithSize(VirtualMachineSizeTypes.StandardDS1)
.Create();
Check this link for further reference. Hope it helps.

Related

how to copy capture images in azure from one subscription to another subscription

i am trying to copy azure captured image from one subscription-another subscription currently in portal move is coming soon so i am trying to copy it and searching for some power shell script i don't want to create a vm from that with out creating vm i am trying to copy
it is managed disk through power shell i can copy managed disk from one subscription-another by creating vm from it but i am trying without creating vm i am trying copy or move capture image is this possible with power shell can any have idea about this.?
i am trying copy or move capture image is this possible with power
shell can any have idea about this.?
No, it is not possible. Image does not copy from one subscription to another subscription. You need copy image's managed disk to other subscription.
You have two option.
1.Using image's managed to create a snapshot and copy this snapshot to other subscription, then using this snapshot to create a managed disk, then create a image.
#Create a snapshot from managed disk
$disk = "/subscriptions/************/resourceGroups/SHUICLI/providers/Microsoft.Compute/disks/shui_OsDisk_1_21af43450987448184b5e9793da08e54"
$snapshot = New-AzureRmSnapshotConfig -SourceUri $disk.Id -CreateOption Copy -Location $region
$snapshotName = $imageName + "-" + $region + "-snap"
New-AzureRmSnapshot -ResourceGroupName $resourceGroupName -Snapshot $snapshot -SnapshotName $snapshotName
#copy the snapshot to another subscription, same region
$snap = Get-AzureRmSnapshot -ResourceGroupName $resourceGroupName -SnapshotName $snapshotName
#change to the target subscription
Select-AzureRmSubscription -SubscriptionId $targetSubscriptionId
$snapshotConfig = New-AzureRmSnapshotConfig -OsType Windows `
-Location $region `
-CreateOption Copy `
-SourceResourceId $snap.Id
$snap = New-AzureRmSnapshot -ResourceGroupName $resourceGroupName `
-SnapshotName $snapshotName `
-Snapshot $snapshotConfig
More information about this please refer to this blog.
2.Copy image's managed disk to a storage account, then using this VHD to create a new image.
##create $SAS
$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'
See this answer.

Create a Disk in Azure New Portal is not working , old portal I could do it easily

I'm not even sure why azure even has a GUI Website. It is starting to feel a bit ridiculous when the old manage.windowsazure.com I could powershell up a VHD, and then very easily use a Storage and container and Add the image and then choose from gallery of my own images.
NOW I read that in May 2017 a lot of things with the old portal are going away. I created a Storage Account myvmblobs and then a container mywincontainer and then I uploaded a VHD , tmppro2.vhd is sitting there as a VHD Blob
URL https://myvmblobs.blob.core.windows.net/mywincontainer/TMPPRO2.VHD
So I read that I could create a Disk image from powershell ( I have to no way to do it with website portal.azure.com )
Add-AzureDisk 'tmppro2' -MediaLocation https://myvmblobs.blob.core.windows.net/mywincontainer/TMPPRO2.VHD -Label 'OS' -OS "Windows"
However, I don't know if the Label or OS is important...
Add-AzureDisk : BadRequest: The storage account with the name myvmblobs as specified in the VHD URI https://myvmblobs.blob.core.windows.net/mywincontainer/TMPPRO2.VHD does not exists in the current subscription
According to your description, we can use PowerShell or template to create new VM in Azure ARM module.
About PowerShell, here is a example script(use existing VHD):
$rgname = "jason-newgroup"
$loc = "japaneast"
$vmsize = "Standard_DS1_v2"
$vmname = "jason-newtest2"
$vm = New-AzureRmVMConfig -VMName $vmname -VMSize $vmsize
$nic = Get-AzureRmNetworkInterface -Name ("jason-newtest45") -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 -Linux
New-AzureRmVM -ResourceGroupName $rgname -Location $loc -VM $vm
Use template to create Azure VM:
Here is the template.
Update:
We can use azure storage explorer to upload VHD to Azure:

Azure Microsoft.Azure.Diagnostics Extension

Trying to configure the BootDiagnostics on a VM on a storage account in a resource group different from the VMs. To the best of my knowledge this can only be done by installing the VmDiagnosticsExtension. I'm executing the following:
Set-AzureRmVMDiagnosticsExtension -ResourceGroupName $RgName -VMName $VmName `
-DiagnosticsConfigurationPath 'D:\DiagnosticsConfig.xml' `
-StorageAccountEndpoint '/subscriptions/ff26f7/resourceGroups/rgroup/providers/Microsoft.Storage/storageAccounts/amsdiag01'`
-StorageAccountName 'amsdiag01' `
-StorageAccountKey 'key''>
I get this error message:
ErrorMessage: Handler 'Microsoft.Azure.Diagnostics.IaaSDiagnostics'
has reported failure for VM Extension
'Microsoft.Insights.VMDiagnosticsSettings' with terminal error code
'1009' and error message: 'Enable failed for plugin (name:
Microsoft.Azure.Diagnostics.IaaSDiagnostics, version 1.6.3.0) with
exception Command
C:\Packages\Plugins\Microsoft.Azure.Diagnostics.IaaSDiagnostics\1.6.3.0\DiagnosticsPluginLauncher.exe
of Microsoft.Azure.Diagnostics.IaaSDiagnostics has exited with Exit
code: -106'
No further information available in C:\WindowsAzure\Logs\WaAppAgent.log on server. In the portal under 'Diagnostics' I can see the correct configuration. But the extension is in a '(unavailable)' state. Container in storage account is not created. Also, can't reboot the server due to the extension. I can remove and re-add the extension using PowerShell, but always same result. When performing a Get-AzureRmVMExtension I see the following 'Public Settings' entry:
PublicSettings : {
"storageAccount": "amsdiag01",
"xmlCfg": "PFdhZENmZz4NCiAgICAgIDxEaWFnbm9zdGljTW9uaXRvckNvbmZpZ3VyYXRpb24gb3ZlcmFsbFF1b3RhSW5NQj0iNDA5NiI+DQogICAgICAgIDxQZXJmb3JtYW5jZUNvdW50ZXJzIHNjaGVkdWxlZFRyYW5zZmVyUGVyaW9kPSJQVDFNIj4NCsKgwqDCoMKgwqAgICAgIDxQZXJmb3JtYW5jZUNvdW50ZXJDb25maWd1cmF0aW9uIGNvdW> eU5hbWU9IkNQVSBwcml2aWxlZ2VkIHRpbWUiIGxvY2FsZT0i
I've chosen not to use extensions but instead using the following:
$RgName = 'RG1'
$VmName = 'VM01'
$Vm = Get-AzureRmVM -ResourceGroupName $RgName -Name $VmName
$Vm = Set-AzureRmVMBootDiagnostics -Enable -VM $Vm -ResourceGroupName $RgName -StorageAccountName diagss01
Update-AzureRmVM -ResourceGroupName $RgName -VM $Vm

Update Azure Website instance size (Small, Medium, Large) with Powershell

Is there a way to scale an Azure WebSite instance size (not instance count) using PowerShell? I haven't found the way.
I know it can be done using Azure CLI or from the Portal, but I want to be able to do it with PowerShell. Is this posible?
I want to update an existing WebSite instance size, not create a new website.
Yes, you first need to know the name of the Resource Group and Web Hosting Plan to which your website belongs, which you can get from the new Azure Portal. Then you can change the worker size to 0 (Small), 1 (Medium) or 2 (Large).
Switch-AzureMode AzureResourceManager
$resourceGroup = "MyResourceGroup"
$webHostingPlan = "MyWebHostingPlan"
$whp = Get-AzureResource `
-Name $webHostingPlan `
-ResourceGroupName $resourceGroup `
-ResourceType "Microsoft.Web/serverFarms" `
-ApiVersion 2014-04-01
$newWorkerSize = 1
$whp.Properties.workerSize = $newWorkerSize
$whp.Properties.workerSizeId = $newWorkerSize
Set-AzureResource `
-Name $webHostingPlan `
-ResourceGroupName $resourceGroup `
-ResourceType "Microsoft.Web/serverFarms" `
-ApiVersion 2014-04-01 `
-PropertyObject $whp.Properties

How to script scaling a Website from Small to Large? [duplicate]

Is there a way to scale an Azure WebSite instance size (not instance count) using PowerShell? I haven't found the way.
I know it can be done using Azure CLI or from the Portal, but I want to be able to do it with PowerShell. Is this posible?
I want to update an existing WebSite instance size, not create a new website.
Yes, you first need to know the name of the Resource Group and Web Hosting Plan to which your website belongs, which you can get from the new Azure Portal. Then you can change the worker size to 0 (Small), 1 (Medium) or 2 (Large).
Switch-AzureMode AzureResourceManager
$resourceGroup = "MyResourceGroup"
$webHostingPlan = "MyWebHostingPlan"
$whp = Get-AzureResource `
-Name $webHostingPlan `
-ResourceGroupName $resourceGroup `
-ResourceType "Microsoft.Web/serverFarms" `
-ApiVersion 2014-04-01
$newWorkerSize = 1
$whp.Properties.workerSize = $newWorkerSize
$whp.Properties.workerSizeId = $newWorkerSize
Set-AzureResource `
-Name $webHostingPlan `
-ResourceGroupName $resourceGroup `
-ResourceType "Microsoft.Web/serverFarms" `
-ApiVersion 2014-04-01 `
-PropertyObject $whp.Properties