Today, bought along an interesting challenge. When working on one of those famous ‘5 minute tasks’ it proved to be not quite as simple as first thought.

Some upcoming work requires a change of the assigned Storage Policy against a range of VMs, seemed simple enough at the offset. Not all the VMs where assigned to the same Storage Policy and updating manually via the vSphere Client was just not for me, time to break out some PowerCLI!

Started pulling together a simple PowerCLI command, simple enough that it actually started as a single line, before it grow in the ‘script’ that is below, but the first test was a complete failure. Well, failure is just an opportunity to learn and grow, right, so back to the drawing board.

The second test then updated the storage policy of the VM Home object, but not the VMDK’s – the VMDK’s is the most important part of this Storage Policy update.

After a little more testing we got there, the below script will pull the first 10 VMs that are assigned a Storage Policy named “Policy-1”. It will then look through and updated the Storage Policy assignment on the VM Home object and all the virtual hard disks that are assigned to the new Storage Policy named “Updated-Policy-2”.

$VMsToUpdate = Get-VM | Get-SpbmEntityConfiguration | Where-Object StoragePolicy -Like "Policy-1" | Select-Object -First 10
$NewStoragePolicy = Get-SpbmStoragePolicy -Name "Updated-Policy-2"
foreach ($VM in $VMsToUpdate) 
{
    $VMHDDs = Get-VM -Name $VM | Get-HardDisk
    Set-SpbmEntityConfiguration -Configuration $VM -StoragePolicy $NewStoragePolicy
    
    foreach ($HDD in $VMHDDs) 
    {    
        Set-SpbmEntityConfiguration -Configuration $HDD -StoragePolicy $NewStoragePolicy
    }
}

Leave a Comment

Your email address will not be published. Required fields are marked *