Created
February 26, 2025 15:33
-
-
Save kobutton/c2214288c09424e70bc93a2602f2db8a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Import-Module VMware.VimAutomation.Core | |
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false | Out-Null | |
Connect-VIServer vcsa.home.kobutton.com | |
foreach($vm in Get-VM){ | |
$DatastoreIds = $vm.DatastoreIdList | |
$VMDatastoreClusters = @() | |
foreach ($DatastoreId in $DatastoreIds){ | |
$Datastore = Get-Datastore -id $DatastoreId | |
if ($Datastore.ExtensionData.Parent.Type -eq "StoragePod" ){ | |
$VMDatastoreClusters += ($Datastore.ParentFolderId) | |
} | |
} | |
foreach ($StoragePod in ($VMDatastoreClusters | Get-Unique)){ | |
$DatastoreCluster = Get-DatastoreCluster -Id $StoragePod | Get-Unique | |
$StoragePodView = Get-View -ViewType "StoragePod" -Filter @{"Name" = $DatastoreCluster.Name} | |
$StoragePodView.PodStorageDrsEntry.StorageDrsConfig | |
$thisVMInList = $StoragePodView.PodStorageDrsEntry.StorageDrsConfig.VmConfig | Where-Object {$_.Vm -eq $vm.Id} | Get-Unique | |
$StorageResourceManager = Get-View StorageResourceManager | |
$spec = New-Object VMware.Vim.StorageDrsConfigSpec | |
Write-Host $thisVMInList | |
if (-not $thisVMInList){ | |
Write-Host "Creating SDRS Override" | |
$vmConfigSpec = New-Object VMware.Vim.StorageDrsVmConfigSpec | |
$vmConfigSpec.Operation = "add" | |
$vmConfigSpec.Info = New-Object VMware.Vim.StorageDrsVmConfigInfo | |
$vmConfigSpec.Info.Enabled = $false | |
$vmConfigSpec.Info.Vm = $vm.ExtensionData.MoRef | |
$spec.VmConfigSpec += $vmConfigSpec | |
$StorageResourceManager.ConfigureStorageDrsForPod($DatastoreCluster.ExtensionData.MoRef,$spec,$true) | |
} | |
else{ | |
Write-Host "Editing SDRS Override" | |
$vmConfigSpec = New-Object VMware.Vim.StorageDrsVmConfigSpec | |
$vmConfigSpec.Operation = "edit" | |
#$vmConfigSpec.Info = New-Object VMware.Vim.StorageDrsVmConfigInfo | |
$vmConfigSpec.Info = $thisVMInList | |
$vmConfigSpec.Info.Enabled = $false | |
#$vmConfigSpec.Info.Vm = $vm.ExtensionData.MoRef | |
$spec.VmConfigSpec += $vmConfigSpec | |
$StorageResourceManager.ConfigureStorageDrsForPod($DatastoreCluster.ExtensionData.MoRef,$spec,$true) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment