I was asked how to automate formatting a data drive on an Azure VM. The drive had been provisioned already when the infrastructure was created via ARM script, however the disk was raw. I developed this solution with Azure PowerShell Desired State Configuration (DSC), Azure PowerShell 1.0 (newest release as of April 2016), and my Windows 10 client machine, meaning I have PowerShell 5 and can leverage the PowerShell Gallery. DSC was originally developed for Windows Server and has been extended into Azure.
Setup From Administrative PowerShell
The first step is to install the xStorage module from the PowerShell Gallery. PowerShell needs the NuGet provider to download module packages from the gallery. I issued the below commands from an administrative PowerShell session. Once you have completed this on your development client machine, you don’t need to do it again.
PS C:\WINDOWS\system32> Install-Module xStorage
NuGet provider is required to continue
PowerShellGet requires NuGet provider version ‘18.104.22.168’ or newer to interact with NuGet-based repositories. The NuGet provider must be available in ‘C:\Program Files\PackageManagement\ProviderAssemblies’ or
‘C:\Users\tomta\AppData\Local\PackageManagement\ProviderAssemblies’. You can also install the NuGet provider by running ‘Install-PackageProvider -Name NuGet -MinimumVersion 22.214.171.124 -Force’. Do you want PowerShellGet to install andimport the NuGet provider now?
[Y] Yes [N] No [S] Suspend [?] Help (default is “Y”): y
You are installing the modules from an untrusted repository. If you trust this repository, change its InstallationPolicy value by running the Set-PSRepository cmdlet. Are you sure you want to install the modules from
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is “N”): y
This part does not need to be done from an Administrative PowerShell. I just started ISE from a PowerShell prompt and used it to write my Desired State Configuration (DSC) script.
PS C:\temp> ise
In ISE, I wrote and saved the below to c:\temp\prepardisk.ps1
Import-DSCResource -ModuleName xStorage
DiskNumber = 2
RetryIntervalSec = 60
Count = 60
DiskNumber = 2
DriveLetter = ‘F’
FSLabel = ‘Data’
DataDisk -outputpath C:\DataDisk
Start-DscConfiguration -Path C:\DataDisk -Wait -Force -Verbose
The above will wait for disk volume 2 to show up connected to the Azure VM, which it should already be. It will then initialize, partition, format, and bring the volume up as F:.
Next, we deploy the DSC configuration we want the VM to enact up to Azure, then tell he VM to update. You need an Azure storage account. The account is used to store the configuration so the VM can get it.
I performed these actions on a classic VM. If you’re using ARM, the following steps are different and I need to write another post for it J. I’m also assuming that you have authenticated your PowerShell session by executing Add-AzureAccount, and using Select-Subscription to activate the correct subscription for your VM and storage account.
PS C:\temp> $key=Get-AzureStoragekey -StorageAccountName timsstorageaccount
PS C:\temp> $context=New-AzureStorageContext -StorageAccountName $key.StorageAccountName -StorageAccountKey $key.Primary
PS C:\temp> Publish-AzureVMDscConfiguration -ConfigurationPath C:\temp\preparedisk.ps1 -StorageContext $context
PS C:\temp> $vm = get-azurevm -ServiceName TimsCloudService -name dev1vm1
PS C:\temp> Set-AzureVMDscExtension -vm $vm -ConfigurationArchive “preparedisk.ps1.zip” -ConfigurationName “DataDisk” -StorageContext $context | Update-AzureVM
OperationDescription OperationId OperationStatus
——————– ———– —————
Update-AzureVM 3f88ae8d-5cf0-a7c8-8e0f-f4c30324e96d Succeeded
Visually Check the Deployment
After a few minutes, I RDP’d into the Azure VM to be satisfied that all worked well. I opened Windows File Explorer and the disk showed up partitioned, formatted, and with the correct drive letter.