In my last blog post, I wrote a script which automates the virtual desktop provisioning process based on pre-existing computer accounts in Active Directory. This can be located in the following location. However, within that script, I prompted the user to enter the name of a Storage Repository for the location of the virtual desktop VHD files.
Well, what if there are multiple SRs? What happens if the selected SR fills up? How do I accommodate for the usage of multiple SRs without requiring end-user input so that the script can scale?
I’ve given this some thought and I’ve come up with a few answers. However, I will use this blog to feature a method which I feel is the best of all the solutions I’ve come up with in my personal opinion.

Script Snippet

$O = Connect-XenServer -Server 192.168.0.0 -UserName 'root' -Password '111111'
$StorageFilter = Get-XenServer:SR | Where-Object {$_.sm_config["use_vhd"] -eq 'true'} | Sort-Object -Property 'physical_utilisation'
:StorageFilter ForEach ($Q in $StorageFilter)
{
$Size = $Q.physical_size
$Util = $Q.physical_utilisation
$GB = 1000000000
$DiffSize = $Size - $Util
$FreeSpace = $DiffSize/$GB
if($Q.name_label -eq 'Local storage')
{
##Do Nothing with the SR labeled 'Local Storage', which contains the OS.
}
elseif ($FreeSpace -lt 150) ##150 is an arbitrary number I came up with.
{
##Do Nothing, since we don't want SRs with low available disk space.
}
else
{
##The following line is not necessary, I just wanted to see that the logic flowed.
$Q.name_label+" Is greater than 150GB"+" $FreeSpace"
$SR = $Q
$SR ##This variable will then be utilized elsewhere in the script for VM copies.
break StorageFilter ##This breaks the loop once the first eligible SR is located.
}
}

Script Notes
$StorageFilter = this variable acquires all of the available SRs that one would see in Xencenter. Each SR object contains a property called ‘SM_Config’. SM_Config contains a collection of key=value pairs. The SRs which contain VHD files that represent virtual desktops contain a key=value pair of ‘Use_VHD = true’. Therefore, the script only selects those SRs with this value in its respective property. Finally, the script sorts the results based on ‘Physical Utilisation’ in ascending order. This means, the SRs with more free space are on top with the less free space SRs towards the bottom.

The script then initiates a loop (foreach) which then processes all of the SRs which were previously filtered with a powershell label called :StorageFilter. This label is used to break the loop once the ‘break’ key word is executed against the label. Within the ‘foreach’ loop, the script calculates the free space of each SR. The free space is in terms of Gigabytes. The script then filters out any SR with the name of ‘Local Storage’, which normally would be the SR for the OS and any SR with free space lower than a specified number (I arbitrarily chose 150GB for my lab). Finally, the $SR variable is initialized with the resulting SR and the loop is then broken. The $SR variable which contains the resulting SR is then used later on in the automation script for the copying of VHD files. Theoretically, the script will always copy a VM to an SR with the most available disk space, during each iteration; dynamically.

Free Space Script
Just for fun, here is another script snippet which will acquire a list of all the SRs containing virtual desktops and their available disk space:

$Storage = Get-XenServer:SR | Where-Object {$_.sm_config["use_vhd"] -eq 'true'} | Sort-Object -Property 'physical_utilisation'
ForEach ($x in $Storage)
{
$Size = $x.physical_size
$Util = $x.physical_utilisation
$GB = 1000000000
$DiffSize = $Size - $Util
$FreeSpace = $DiffSize/$GB
$x.name_label+": $FreeSpace GB"
}

Hope this helps and I welcome your feedback.