Change the boot diagnostic storage account of azure virtual machine - azure

I have enabled a diagnostic storage account for my virtual machine. Now I want to change the diagnostic storage account. How do I do it in portal / PowerShell?
I can see that we have a cmdlet - "Set-AzureRmVMBootDiagnostics", however, this is not updating the diagnostic storage account to a new one.
In specific:
My current diagnostic storage is "ibmngeus2" and I would like to change to "sqlbackupacct".

In the Azure portal, you should be able to click on the boot diagnostics section of the virtual machine, and then click the settings, and then click the storage account section to change the storage account. Keep in mind that the new storage account you want to use must be in the same subscription and location or it will not show up as one you can choose.

I got the settings under the "Boot Diagnostic" option under the Virtual machine pane.

In PowerShell (with the now-deprecated AzureRM module), you can do this:
Get-AzureRmVM <Stuff to filter the VMs you want go here> | Set-AzureRmVMBootDiagnostics -Enable -ResourceGroupName <storage-account-rg> -StorageAccountName <storage-account-name> | Update-AzureRmVM
This will get the VM objects, change the setting and apply the change.
Example, to update all VMs' diag storage account to bucket in RG mygroup:
Get-AzureRmVM | Set-AzureRmVMBootDiagnostics -Enable -ResourceGroupName mygroup -StorageAccountName bucket | Update-AzureRmVM
(You probably want to filter more...)
For the new Az module, the commands would change to: (same warnings as above will apply)
Get-AzVM <Stuff to filter the VMs you want go here> | Set-AzVMBootDiagnostic -Enable -ResourceGroupName <storage-account-rg> -StorageAccountName <storage-account-name> | Update-AzVM
Get-AzVM | Set-AzVMBootDiagnostic -Enable -ResourceGroupName mygroup -StorageAccountName bucket | Update-AzVM
(There is a deprecation warning for the plural version of the command - this uses the new name, but the warning still shows)

Related

Finding the last date of the vm using azure cli command

I have an account with Azure with different Resource Group and different virtual machine. I would like to know how I can determine which ones are unused. For example check the last date where the virtual machine was started or used by the user using azure cli command.
Please help me out with this...
Easiest would probably to look at the powerstate of a vm.
First list all the vm's and then run a query where you filter out those that are deallocated and belong to a specific resource group:
az vm list -d --query '[?powerState == `VM deallocated` && resourceGroup==`resource_group`]'
For more information on the queries look up 'JMESPath-query' on the Microsoft docs page. Hopefully this helps.
I would like to know how I can determine which ones are unused
Currently, there is no way to do that using cli.
check the last date where the virtual machine was started or used
We can get this information using PowerShell. which follows.
Get data information of deallocated VM using Get-AzVM -VMName xxxx - RgName xxx -Status
# To retrieve the date of VM was Deallocated.
$vmDeallocatedDate = Get-AzVM -VMName <Your VM name> -ResourceGroupName <Your ResourceGroup Name> -Status
$vmDeallocatedDate.Statuses[0].Time
List all the VM's and the timestamp of the action that triggered the Deallocation
Get-AzLog -Status Accepted -DetailedOutput | ?{$_.Authorization.Action -eq "Microsoft.Compute/virtualMachines/deallocate/action"} | fl ResourceId,EventTimestamp
References
Get-AzLog to get VM Deallocate status
Get-AzVM -status to get VM Deallocate status

Azure Scaleset - is there a Get-AzureRmVmssDiagnosticsExtension

Is there a Get-AzureRmVmssDiagnosticsExtension. How do I get if a scale set has diagnostics enabled and what storage it is using.
As far as I know, there is no command to get the storage account directly.
But we can use PowerShell to the information of vmss, like this:
PS C:\Users> get-azurermvmss -ResourceGroupName vmss -VMScaleSetName vmss

migrating existing Azure VMs to Azure Premium Storage

i am having two VM'S which is under domain running erp application and database server. Both vm are in Standard D12 v2 (4 cores, 28 GB memory)template.
Now we need to move these VM's into premium disk. So How can i migrate existing Azure VMs to Azure Premium Storage.
I will recommend migrating to Managed Disk and you can migrate existing VMs using standard storage account to Managed Disk and Premium Storage account follow article below.
Migrate existing Azure VMs using standard unmanaged disks to Premium managed disks
We can migrate existing Azure VMs from standard storage account to Premium storage account, but we need to shutdown the VMs.
If we can shutdown the VMs, then we can follow those steps to migrate them:
1.Via Azure portal to create a premium storage account(new).
2.Shutdown Azure VMs, copy VHDs to premium storage account. we can use PowerShell, Azure storage explorer or Azcopy to copy VHDs to new storage account.
3.Just delete Azure VMs and keep the VHDs, Virtual Network, NIC, Public IP address and other settings.
4. Use new storage account to create new VMs, when we create new VMs, in the configure page, we select original Vnet and NIC to it.
5. After it complete, try to login those VMs and test
erp application, make sure that the VMs to run properly, then delete original VHDs and storage account.
Note:
When we use VHDs from new storage account to create VMs, we need to select VM disk type to SSD.
===========================================
Update:
1. stop Azure VM and delete the VM via Azure portal.
2. use Azcopy(Microsoft Azure storage command line) to copy VHD to new Azure premium storage account:
AzCopy /Source:https://vmdisks416.blob.core.windows.net/vhds /Dest:https://jasondisk999.blob.core.windows.net/vhds /SourceKey:key /DestKey:key /Pattern:jasonvm20170519131021.vhd
3. Use PowerShell to create new Azure VM:
PS C:\Users> $rgname = "vm"
PS C:\Users> $loc = "eastus"
PS C:\Users> $vmsize = "Standard_DS1_v2"
PS C:\Users> $vmname = "jason-newtest2"
PS C:\Users> $vm = New-AzureRmVMConfig -VMName $vmname -VMSize $vmsize
PS C:\Users> $nic = Get-AzureRmNetworkInterface -Name "jasonvm422" -ResourceGroupName $rgname
PS C:\Users> $nicId = $nic.Id
PS C:\Users> $vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $nicId
PS C:\Users> $osDiskName = "jason-newtest"
PS C:\Users> $osDiskVhdUri = "https://jasondisk999.blob.core.windows.net/vhds/jasonvm20170519131021.vhd"
PS C:\Users> $vm = Set-AzureRmVMOSDisk -VM $vm -VhdUri $osDiskVhdUri -name $osDiskName -CreateOption attach -Linux
PS C:\Users> New-AzureRmVM -ResourceGroupName $rgname -Location $loc -VM $vm
WARNING: Since the VM is created using premium storage, existing standard storage account, vmdiag614, is used for boot diagnostics.
RequestId IsSuccessStatusCode StatusCode ReasonPhrase
--------- ------------------- ---------- ------------
True OK OK

How to keep / set the default admin password in Azure ASM after a copy of the OS Disk

I need to migrate old Classic VMs to New (still) Classic VMs in order to use :
New Kind of VMs (Standard_A to Standard_DS_V2)
Premium Disks
I've made a script which is :
Stop the existing VM
Delete the existing VM
Copy the old disks (OS + Data) to Premium Storage Account
Register the New Disks
Then I have another script to create the new VM based on Json informations
This script has been developped a while ago to create VMs based on the Json informations and existing Azure Disk Template.
I've adapted it to create a new VM based on the copy of the disks.
Everything is working quite well except the point of the admin user / password.
Each time I create a new VM, I can't access it with my old admin user (the one used to create the VM initially).
In order to access the VM, I need to use the Azure portal to reset the password
Then I can access the VM.
The thing is that I want to either :
do this step into my script
do not have to do this at all (we're using a copy of the OS Disk / Data Disks so it shouldn't be needed)
Can someone explain me how to manage this ?
I'm on it since days / weeks and I can't find a way to do it properly.
The actions made by my script for the VM creation (ubuntu VM):
- Set-AzureSubscription -CurrentStorageAccount $VM.StorageAccount -SubscriptionId $VM.SubscriptionId
- $GlobalOSDiskName = (Get-AzureDisk | Where-Object { $_.OS -iin "Linux","Windows" } | Select DiskName).DiskName
- $VM = Get-Content $json_file -Raw | ConvertFrom-Json
- $VMOSDisk = $VM.OSDisk
- $vmObj = New-Object -TypeName Microsoft.WindowsAzure.Commands.ServiceManagement.Model.PersistentVM
- $vmObj = New-AzureVMConfig -Name $VM.Name -InstanceSize $VM.InstanceSize -DiskName $VMOSDisk.DiskName
- $vmObj = Set-AzureOSDisk -VM $vmObj
- $VMDataDisks = $VM.DataDisks
- foreach ($VMDataDisk in $VMDataDisks) {
$vmObj = Add-AzureDataDisk -Import -DiskName $VMDataDisk.DiskName -LUN $VMDataDisk.Lun -HostCaching $VMDataDisk.HostCaching -VM $vmObj
}
- $password = Read-Host -Prompt "Enter password for admin account '$username'" -AsSecureString
- $vmObj = Set-AzureVMAccessExtension -VM $vmObj -UserName $username -Password $password
# This is the last test I've made for this but it's not better...
# Adding informations for Subnet, StaticIP, AvailabilitySet, EndPoint, ACL etc...
- New-AzureVM -VNetName $VM.Vnet -ServiceName $VM.ServiceName -VM $vmObj -WaitForBoot
# Creation of the VM based on the object $vmObj
==> The VM is created correctly but cannot be accessed
Moreover, when I reset the password with the Azure Portal, I'm prompted to fill my password when I use a sudo command when I was not prompted on the old VM.
That's not necessary a big deal, but I'd like to know why and how to enable / disable this.
Edit : I've tested the suggestion written in this doc which seems to work.
This method consists in :
Export the config of the source VM with command Export-AzureVM into an xml file
Edit the exported xml file to match the desired settings
Create a new VM Object based on this with command Import-AzureVM
The result seems to work properly :
The VM is well created with all the parameters
The Admin password remains the same without having set it anywhere
The concerns about this is that :
There is no reference in the exported xml to admin credentials
So there is no reference in the new vm object to admin credentials
The object created is exactly the same the one I create in my way
Can someone explain me the difference between both solutions ?
Thanks for your help
Finally after several testing, I still don't have any confirmation of anything...
In some cases, the admin account password needs to be reset, in other cases not...
If someone has an answer It would be very helpful...

How do I delete an Azure storage account containing a leased blob?

I was playing with Windows Azure durable virtual machines. In the end, I deleted the virtual machine (successfully) and tried to delete the associated storage account.
The request to delete the storage account fails.
On the Preview Portal (manage.windowsazure.com) when I delete the storage account I get this error:
Failed to delete Storage account 'portalvhdscwtwycpsxxxxx'
Details:
Storage account portalvhdscwtwycpsxxxxx has 1 container(s) which have an active image and/or disk artifacts. Ensure those artifacts are removed from the image repository before deleting this storage account.
On the previous portal (windows.azure.com) I get this error:
Submit Failed
Storage account portalvhdscwtwycpsxxxxx has 1 container(s) which have an active image and/or disk artifacts. Ensure those artifacts are removed from the image repository before deleting this storage account.
Trying to delete the blob itself (a 30GB VHD) on Azure Storage Explorer I get this error:
There is currently a lease on the blob and no lease ID was specified in the request.
So my assessment is that this blob is leased (by the previous, now deleted virtual machine) and I can't delete it unless I can get this lease ID.
The question is: how can I delete this blob and, consequently, the storage account?
The key to the solution is the message that the container has an active disk artifact and the advice to remove it from the repository.
The procedure to remove the disk image from the blob repository is:
Go to the Windows Azure Management Portal.
Click on Virtual Machines.
Click on Disks.
Click on the disk.
Click on Delete Disk.
After that, the storage account can be deleted.
Notes:
This applies even if you've already deleted all of your Virtual Machines and it shows 0; there still will be artifacts under the disks tab.
Disks are detached from a deleted VM asynchronously, it may take a few minutes after the VM is deleted for this field to clear up.
See also: Unable to delete VHD, “There is currently a lease on the blob…”
Unfortunately, Fernando's answer didn't work for me, since the storage was "orphan", as I deleted its VM before deleting the storage. I couldn't find a way to do it from the portal so I've installed azure-cli, and after authentication ran the following commands:
azure storage account delete <my-account>
This fails, and the error message contains the name of culprit, e.g.:
error: Storage account <my-account> has some active image(s) and/or disk(s), e.g. <my-image>. Ensure these image(s) and/or disk(s) are removed before deleting this storage
Then I deleted the offending image
azure vm disk delete <my-image>
And tried again to delete the storage, this time successfully.
azure storage account delete <my-account>
Unfortunately there is the case where the VM was deleted but Disks shows the VM attached to the blob (a 30GB VHD) precluding the deletion. Also there is the case of using the Azure Storage Explorer you find an orfan but leased VHD blob that can't be deleted and there is no reference on the Preview Portal.
Go to virtual machines, then click on discs. Mark the disc and choose delete disc at the bottom. You can now choose if you want to keep or delete the corresponding vhd.
It is important first to delete the disc via virtual machines not to delete via storage.
You can use Iaas Management Studio : break the lease, delete the blob, then remove the orphaned image.
In my case, storage could not be deleted because of vmimages.
Use power shell command
get-azurevmimage | Where-Object -Property Category -in -Value "user"
to list all images
To delete ALL YOU IMAGES use the following script:
get-azurevmimage | Where-Object -Property Category -in -Value "user" |
foreach {
echo "remove $($_.ImageName)"
Remove-AzureVMImage –ImageName $($_.ImageName)
}
As F.M. has already stated; there is a scenario where when deleting a VM the disk still shows as attached to the VM even though the VM has been deleted.
For me this happened because I had a set a spending limit. When the spending limit is hit, your services are disabled. Any VPN gateways you have created and VMs will be deleted. Then to top it off the disks attached to the deleted VMs still think they are attached :(
I have found this blog that explains the issue and shows how to use powershell to resolve.
Hope this helps other users.
Sometimes we via the new portal to delete azure storage account, but we can’t delete it and get this error:” Failed to delete storage account 'jason1disks796'. Error: The storage account cannot be deleted due to its artifacts being in use. “
We can use PowerShell to list all the VHD blobs of the storage account(ARM module):
PS > Login-AzureRmAccount
PS > $RGName = "jason1"
PS > $SAName = "jason1disks796"
PS > $ConName = "vhds"
PS > $TempObj = New-Object -TypeName PSCustomObject
PS > $TempObj |Add-Member -Name BlobName -MemberType NoteProperty -Value $null
PS > $TempObj |Add-Member -Name LeaseState -MemberType NoteProperty -Value $null
PS > $Keylist = Get-AzureRmStorageAccountKey -ResourceGroupName $RGName -StorageAccountName $SAName
PS > $Key = $Keylist[0].Value
PS > $Ctx = New-AzureStorageContext -StorageAccountName $SAName -StorageAccountKey $Key
PS > Get-AzureStorageContainer -Context $ctx
CloudBlobContainer : Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer
Permission : Microsoft.WindowsAzure.Storage.Blob.BlobContainerPermissions
PublicAccess : Off
LastModified : 1/19/2017 1:27:21 AM +00:00
ContinuationToken :
Context : Microsoft.WindowsAzure.Commands.Common.Storage.AzureStorageContext
Name : vhds
PS > $List = Get-AzureStorageBlob -Blob *.vhd -Container $ConName -Context $Ctx
PS > $List | ForEach-Object { $TempObj.BlobName = $_.Name; $TempObj.LeaseState = $_.ICloudBlob.Properties.LeaseState; $TempObj }
BlobName LeaseState
-------- ----------
SQL20170119092405.vhd Leased
PS > Get-AzureStorageBlob -Blob * -Container $con -Context $ctx | Remove-AzureStorageBlob
PS > Remove-AzureRmStorageAccount -ResourceGroupName $RGname -Name $SAName
If your storage account is in the ASM module, you can use this script to remove storage account:
Add-AzureAccount
$SAName = "jason1161"
$ConName = "vhds"
$TempObj = New-Object -TypeName PSCustomObject
$TempObj |Add-Member -Name BlobName -MemberType NoteProperty -Value $null
$TempObj |Add-Member -Name LeaseState -MemberType NoteProperty -Value $null
$Keylist = Get-AzureStorageKey -StorageAccountName $SAName
$Key = $Keylist.primary
$Ctx = New-AzureStorageContext -StorageAccountName $SAName -StorageAccountKey $Key
$List = Get-AzureStorageBlob -Blob *.vhd -Container $ConName -Context $Ctx
$List | ForEach-Object { $TempObj.BlobName = $_.Name; $TempObj.LeaseState = $_.ICloudBlob.Properties.LeaseState; $TempObj }
PS > Get-AzureStorageBlob -Blob * -Container $con -Context $ctx | Remove-AzureStorageBlob
PS > Remove-AzureStorageAccount -Name $SAName
Besides, there is another scenario, there is no container or blob in this storage account (an empty storage account, we can’t find blob or container in this storage account via PowerShell or portal), when we use portal to delete the storage account, and the error message” Failed to delete storage account 'jason1disks796'. Error: The storage account cannot be deleted due to its artifacts being in use”. In this scenario we can create a new VM and specify the storage account to the problematic Storage Account, then delete it again.
Do check before deleting your storage account; there must be the associated virtual machine(s), Disks and Images for each storage account you created. Go to Azure portal
Select Virtual Machines tab on left pane
Click on Instances Images and Disks
Note that, Individual Virtual machines has its attached disks which show on Disks area.Before deleting a Virtual machine, delete the associated disks first and delete the virtual machine has the disk second.Then delete the storage account last. Also look out for Network in the same left-hand side pane if any associated with the account you want to delete.
In the new updated Azure portal, many of the above-stated config pages are changed. You can see Images and Disks options in "All resources" pane. In the newer version of an Azure portal, you can easily identify VMs its associated Disks and its Storage account clearly on its adjacent vertical panes all in one page with different icon images.
For those who rely on GUI to manage Azure and have no idea to use PowerShell or do the other answers, you can now delete the stuck storage account by checking "Delete unattached images" when trying to delete the storage.
It will automatically delete the storage without much hassle.

Resources