Quantcast
Channel: PowerShell.org » All Posts
Viewing all articles
Browse latest Browse all 13067

Query multiple servers for performance counters in parallel

$
0
0

I’ve written a script to query multiple Hyper-V hosts for some metrics, then using some logic, returns the name of host, that I can then use to create a vm on. Here is my script, note being I haven’t parameterized it yet.

Workflow Pick-VMHost {

    $Servers = @('host00', 'host01', 'host02', 'host03')
    $UseHost = @{}
    $VMRAM = 1.5GB
    $VMDiskSize = 40GB

    ForEach -Parallel ($Server in $Servers) {
        $CPU = InlineScript { (Get-Counter -Counter "\Processor(_Total)\% Processor Time" -ComputerName $using:Server -SampleInterval 1 -MaxSamples 5).CounterSamples.CookedValue | Measure-Object -Average | Select-Object -ExpandProperty Average }
        $RAM = InlineScript { (Get-Counter -Counter "\Memory\Available Bytes" -ComputerName $using:Server -SampleInterval 1 -MaxSamples 5).CounterSamples.CookedValue | Measure-Object -Average | Select-Object -ExpandProperty Average }
        $Disk = Get-WmiObject win32_logicaldisk -PSComputerName $Server -Filter "DeviceID='D:'" | Select-Object -ExpandProperty FreeSpace
        
        If ( ($CPU -le 80) -and ($RAM -ge ($VMRAM + 3GB)) -and ($Disk -ge ($VMDiskSize + 25GB)) ) {
            If ($UseHost.Count -eq 0) {
                $Workflow:UseHost = @{ "Server" = $Server; "Disk" = $Disk }
            }

            ElseIf ($Disk -gt $UseHost.Disk) {
                $Workflow:UseHost = @{ "Server" = $Server; "Disk" = $Disk }
            }
        }
    }

    $UseHost.Server
}

$Result = Pick-VMHost

I feel like this code isn’t well written and there is a better way to do this, I’ve tried use just Get-Counter on its own but didn’t find a way to get average results for the counters, tried using Jobs, but couldn’t figure out how to return values properly, I’ve ended up using a Workflow. I’ve also posted part of this script on StackOverflow, because I couldn’t at the time figure out how to return the values I wanted in a meaningful way.

Finally I know that Hyper-V has VM Metric functionality, but as far as I’m aware it only queries the VM’s and not the host.


Viewing all articles
Browse latest Browse all 13067

Trending Articles