Al_
Enthusiast
Enthusiast

Process VMs in specific order

The contents of the $data array appear in the correct order, but the $ToolsStatus array seems to lose or somehow not contain the correctly sorted order. The goal is to process/rightsize the VMs in the order of the $data array:

($vms2rs = Get-VM -Tag yes | where{(Get-VM -Tag test).Name -Contains $_.Name}).Name | out-null

$data = ForEach ($item in $vms2rs) {
$Name = $item.Name
Get-VM -Name $Name |
Get-OMResource |
Get-OMStat -Key "Super Metric|sm_6dcf4b1d-fa56-4119-bef8-b197df54b70a" -From ([DateTime]::Now).AddMinutes(-120) |
Select-Object @{N='VM';E={$name}},Resource, Value -Last 1
}
$data | Sort-Object -Property Value -Descending | Select VM,Value

ForEach ($item in $data) {
$Name = $data.VM
$ToolsStatus = Get-VM -Name $Name | Select Name,@{N="Tools Status";E={$_.ExtensionData.Guest.ToolsStatus}}
ForEach ($object in $ToolsStatus){
$srv = $object.name
$tools = $object."Tools Status"
if ("toolsOk","toolsOld" -contains $tools) {
Write-Host "Rightsizing " $srv, $tools
#Get-VM -Name $Name | Get-OMResource | Apply-OMRightsizing -ViewOnly
}
else {Write-Host "Skipping " $srv, $tools}
}
exit
}

Returns:
VM : tstviewcs
Value : 6

VM : 2016test
Value : 5

VM : tstviewss
Value : 3

VM : rhel79-test
Value : 2

VM : VDSDev1
Value : 0

Rightsizing tstviewcs toolsOld
Rightsizing rhel79-test toolsOk
Rightsizing tstviewss toolsOld
Rightsizing 2016test toolsOk
Skipping VDSDev1 toolsNotRunning

0 Kudos
0 Replies