Was cleaning out some old PS scripts recently and found this one in my toolkit.

This one will appease some of those out there who came to Hyper-V from another virtualization technology and are familiar with the term datastore…

We use SOFS storage so our VM paths are all SMB shares but I’ve included the CSV path split for those still running CSV’s to the nodes.

What this gives you is a quick view into which storage device each VM lives on:

datastore

Adjust to your requirements…


# Setup the session
import-module virtualmachinemanager
Set-SCVMMServer -VMMServer <VMMServerName>

# The engine room
$VMS = Get-SCVirtualMachine  # | ogv -passthru
ForEach ($VM in $VMS) 
{
    $DS = $null
    $dsprop = Get-SCCustomProperty -Name Datastore
    $DataStore = Get-SCCustomPropertyValue -CustomProperty $dsprop -InputObject $VM
    IF ($DataStore.Value -eq $null) 
    {
        $Location = $VM.location
        # For iSCSI and FC
        IF ($Location -like "C:\ClusterStorage*")
            {
            $DS = $location.Split("{\}")[2]
            }
        # For SMB3 Shares
        IF ($Location -like "\\*")
            {
            $DS = $location.Split("{\}")[3]
            }
        IF ($DS -eq $null){$DS = "Local Storage"}
        IF ($DS -like "Volume*"){$DS = "iSCSI_" + $DS}
        IF ($DS -like "Disk0*"){$DS = "SMB3_" + $DS}
        IF ($DS -like "*Hyper-V"){$DS = "SMB3_" + $DS}
        Write-Host -ForegroundColor Green "Setting DataConfig for $VM to '$DS'"
        Write-EventLog -EventId 2 -LogName Application `
            -Message "Setting DataConfig for $VM to '$DS'" `
            -Source SCVMMService -EntryType Information
        Set-SCCustomPropertyValue -CustomProperty $dsprop -InputObject $VM -Value $DS
    }
    
    ELSE 
        {
        $DSV = $DataStore.Value
        Write-Host -ForegroundColor Yellow "DataConfig exists for $VM with value '$DSV'"
        }
}


Enjoy!
Dan

Leave A Comment