Tag Archives: PowerShell

Copying Azure Managed disks between regions

I recently had the need to copy some Azure managed disks to another region. Since managed disks are not backed by storage accounts like unmanaged disks, you cannot simply do a blob copy and be done. You have to copy your managed disk to a temporary storage account in your target region and create a managed disk from it.

I could not find something that did what I wanted, so I had to roll my own in the form of a PowerShell script. The script does the following:

  • Takes a source and target resource group, a target location, a set of managed disks and a target SKU as parameters. (Source and target resource groups can be the same.)
  • Creates a temporary storage account with a random name (mdcopynnnnn) in the target resource group in the target location. A new storage account is created each time the script runs.
  • Copies each disk into the target storage account using a blob copy with a SAS.
  • Creates new managed disk resources in the target resource group and target location, with the same names as the source disks.

Important notes:

  • Only supports OS disks for now, since I could not be bothered to write code to check if the disks are OS or data. Maybe later…
  • By default the target storage account is PremiumLRS, you can change this in the variables.
  • I use a 3600 seconds (1 hour) duration for the Shared Access Signature. If you think your disk takes longer to copy than that; increase the value in the variables.
  • There is a timeout, set to the same value as the SAS duration, so the script will continue after the copy has been going on for longer.
  • Assumes you have already logged into Azure and selected the correct subscription.
  • Only copies disks within the same subscription.
  • Assumes your target resource group already exists.
  • Neither deletes the temporary storage accounts or the source managed disks. I will not delete your data!
  • Hardly any error checking.
  • Absolutely no warranty whatsoever; use at your own risk!

https://gist.github.com/morgansimonsen/203983b7c52d1fce497c3a9fd4c24b21

M

Getting volume data with PowerShell

It has always irritated me that I cannot export data from the disk management snap-in in Windows. Take this example from an Exchange server:

image

It would be very nice to be able to export this data to a CSV to create a quick storage report. Unfortunately you can’t. But with PowerShell you can!

This command will export the same data:

Get-WmiObject win32_volume | select Name,Label,@{Name=”Capacity (GB)”; Expression={“{0:N2}” –f ($_.capacity/1GB)}},@{Name=”Free Space (GB)”; Expression={“{0:N2}” -f ($_.freespace/1GB)}},@{Name=”Used Space (GB)”; Expression={“{0:N2}” -f ( ($_.capacity/1GB) – ($_.freespace/1GB) ) }} | ft –AutoSize

The result:

Name        Label        Capacity (GB) Free Space (GB) Used Space (GB)
—-        —–        ————- ————— —————
C:                      72,50         18,17           54,33
E:LogLUN1 ExchangeLogs 1 249,87      1 245,32        4,55
E:         Exchange     0,97          0,93            0,03
E:DBLUN1  DBLUN1       2 046,87      1 358,92        687,95
E:DBLUN2  DBLUN2       2 046,87      1 467,69        579,19
E:DBLUN3  DBLUN3       2 046,87      1 527,84        519,03
E:DBLUN4  DBLUN4       499,87        375,19          124,68

Of course, you can export this to CSV etc.

‘Remove Exchange Attributes’ á la PowerShell

The extensions to Active Directory Users and Computers for Exchange 2000 and Exchange 2003 have a task called Remove Exchange Attributes that is accessible from the Exchange Tasks menu. It removes all Exhange related attributes from the objects on which it is run. This is useful when an object is in an inconsistent state, as regards to Exchange. For example if an Exchange attribute has invalid data or not all required attributes are present.
If you remove the last Exchange 2000/2003 server from your organization you will not be able to use the Remove Exchange Attributes task, even if the extensions are still installed on a computer. I recently found myself in just that situation and had to come up with a workaround. My favorite tool lately is PowerShell so I decided to use that. This is the command I came up with, using the cmdlets from Quest Software for Active Directory:

https://gist.github.com/morgansimonsen/8040285

As you can see, this command targets groups, but it can be easily changed to apply to other object types.
I found a list of all the Exchange attributes that the Remove Exchange Attributes task removes here: