Tag Archives: PowerCLI

Check numer of running and stopped Vm’s in each resource group

Here is another powershell PowerCLI script that will give you the number of running and stopped Vm’s grouped by Cluster and Resource group.

# Add in the VI Toolkit goodness
if ( (Get-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) -eq $null ){Add-PSsnapin VMware.VimAutomation.Core}

$VCs =@(“srv-vc-0001″,”srv-vc-0002”)
$date=Get-Date -format “yyMMd”
$file = “c:\temp\VmwareStats$date.csv”

function GetVmwareStats
{
$report = @()
foreach ($VC in $VCs)
{
Connect-VIServer $VC | Out-Null

foreach ($Cluster in Get-Cluster)
{
#write-host $Cluster
$allcount = @()
foreach ($resourcepool in (Get-ResourcePool -Location $Cluster | Where {$_.Name -ne “Resources“ }))
{
$count = “” | Select Cluster,Hostname,ResourcePool, RunningVms, StoppedVms
$count.HostName = $vmHost.name
$count.Cluster = $Cluster.Name
$RunningsVms = Get-VM -Location $Cluster,$ResourcePool -NoRecursion | Where {$_.PowerState -eq “PoweredOn”}
$StoppedVms =Get-VM -Location $Cluster,$ResourcePool -NoRecursion | Where {$_.PowerState -eq “PoweredOff”}
$count.RunningVMs = $RunningsVms.Count
$count.StoppedVms = $StoppedVms.Count
$count.ResourcePool = $ResourcePool.Name
$allcount += $count
}
$allcount | Select Cluster, ResourcePool, RunningVms, StoppedVms
}
$report += $allcount
}

}

GetVmwareStats |export-Csv $file -notypeinformation -Delimiter “;”

Advertisements

Check you Vmware load

Ever wanted to check the average load of your ESXi servers. Check out this PowerShell PowerCLI script to get average CPU and memory load for your different clusters.

Will give you an output file like this:

HostName ClusterName MemMax MemAvg MemMin CPUMax CPUAvg CPUMin
srv-vm-0001.sirius.local DRS1-Test 72.11 1.96 1.73 29.96 0.25 0
srv-vm-0002.sirius.local DRS1-Test 77.05 60.29 1.14 67.5 11.852 0

# Add in the VI Toolkit goodness
if ( (Get-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) -eq $null ){Add-PSsnapin VMware.VimAutomation.Core}

$VCs =@(“vc_server_1″,”vc_server_2”)
$date=Get-Date -format “yyMMd”
$file = “c:\temp\vmware_load_$date.csv”

Function GetVmwareLoad
{
$report = @()
foreach ($VC in $VCs)
{

Connect-VIServer $VC | Out-Null

$allhosts = @()
foreach ($Cluster in Get-Cluster)
{
foreach($vmHost in ($cluster | Get-VMHost))
{
$hoststat = “” | Select HostName, ClusterName, MemMax, MemAvg, MemMin, CPUMax, CPUAvg, CPUMin
$hoststat.HostName = $vmHost.name
$hoststat.ClusterName = $Cluster.Name

$statcpu = Get-Stat -Entity ($vmHost)-start (get-date).AddDays(-30) -Finish (Get-Date)-MaxSamples 10000 -stat cpu.usage.average
$statmem = Get-Stat -Entity ($vmHost)-start (get-date).AddDays(-30) -Finish (Get-Date)-MaxSamples 10000 -stat mem.usage.average

$cpu = $statcpu | Measure-Object -Property value -Average -Maximum -Minimum
$mem = $statmem | Measure-Object -Property value -Average -Maximum -Minimum

$hoststat.CPUMax = $cpu.Maximum
$hoststat.CPUAvg = $cpu.Average
$hoststat.CPUMin = $cpu.Minimum
$hoststat.MemMax = $mem.Maximum
$hoststat.MemAvg = $mem.Average
$hoststat.MemMin = $mem.Minimum
$allhosts += $hoststat
}

}
$report += $allhosts |Select HostName, ClusterName, MemMax, MemAvg, MemMin, CPUMax, CPUAvg, CPUMin
}
$report
}

########## Main Program ###########

$output=GetVmwareLoad
$output| export-Csv $file -notypeinformation -Delimiter “;”