Hello
Sorry for my previous post and for not writing earlier, it turned out to be some kind of copy-paste error
My script now looks like this:
$Hosts = @(
"xx.xx.xx.1",
"xx.xx.xx.2",
"xx.xx.xx.3")
$counter = 10
foreach ($Server in $Hosts){
Start-Job -ScriptBlock { param ($Server, $counter) Add-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue Connect-VIServer -Server $Server -user xxx -password "xxx" | Out-Null $vms = Get-Vm | where {$_.PowerState -eq "PoweredOn"} $Check = @() $Check = $Check + $Server + $vms for ($i=0; $i -lt $counter; $i++){ $startdate = Get-date $alldata = @() $stats = Get-Stat -Entity $Check -Realtime -Stat cpu.usage.average -MaxSamples 1 $stats | Group-Object -Property Entity | %{ $stat = "" | Select Date, HostName, VMName, CPUAvg $stat.Date = Get-date $stat.VmName = $_.name $stat.HostName = $Server if ($stat.VmName -eq $Server) {$stat.VmName = $null} $cpu = $_.Group | Measure-Object -Property value -Average $stat.CPUAvg = [int]$cpu.Average $alldata += $stat } $alldata | Export-Csv "c:\tmp\$Server.csv" -noTypeInformation $logQuery = new-object -ComObject "MSUtil.LogQuery" $inputFormat = new-object -comobject "MSUtil.LogQuery.CSVInputFormat" $outputFormat = new-object -comobject "MSUtil.LogQuery.SQLOutputFormat" $outputFormat.server = "(local)" $outputFormat.database = "CPUStats" $outputFormat.driver = "SQL Server" $outputFormat.createTable = $true $query = "SELECT Date, HostName, VMName, CPUAvg INTO CPUusage FROM c:\tmp\$Server.csv" $null = $logQuery.ExecuteBatch($query,$inputFormat,$outputFormat) $enddate = Get-date $delay = $startdate.AddSeconds(30) - $enddate Start-Sleep -s $delay.seconds Clear-Variable -Name alldata } Disconnect-VIServer * -Force -Confirm:$false } -Name $Server -ArgumentList ($Server, $counter)
}
Checks the cpu data for host and vms 10 times ($counter = 10), every 30 seconds ($delay = $startdate.AddSeconds(30) - $enddate). It creates (or appends with new data) the "CPUuage" table in the existing "CPUStats" database on the "(local)" SQL server.
I have two questions, please advise:
1) How can I skip the part where the script writes to the csv file and later loads it, can I somehow keep it in memory?
2) Is the "cpu.usage.average" the best stat for current cpu load or should I use something else?
Regards
Genoan