2016-11-08 59 views
0

我已經寫了一個PowerCLI腳本來檢索相關信息以及上個月我們虛擬機的使用統計信息,並將其輸出爲csv,但速度很慢。任何人都可以提出任何可以幫助加快報告速度的改進措施。PowerCLI性能

我是相當新的PowerShell的/ PowerCLI的所以任何幫助將受到歡迎

$todayMidnight = (Get-Date -Hour 0 -Minute 0 -Second 0).AddMinutes(-1) 
$EndOfLastMonth = (Get-Date -Year (get-date).Year -Month (get-date).Month -Day 1 -Hour 0 -Minute 0 -Second 0).AddMinutes(-1) 
$FirstofLastMonth = $EndOfLastMonth.AddMonths(-1) 
$workingDays = "Monday","Tuesday","Wednesday","Thursday","Friday" 
$dayStart = New-Object DateTime(1,1,1,8,0,0) 
$dayEnd = New-Object DateTime(1,1,1,18,0,0) 

$CSVFile = Read-Host "Enter Filename for csv" 

$Report = @() 

Connect-VIServer "VCServer" | Out-Null 

$ServerList = Get-VM | Where-Object {$_.VMHost.Name -ne "192.168.106.161" -and $_.PowerState -eq "PoweredOn" } | Sort-Object Name 

$Counter = 1 

foreach ($Server in $ServerList) { 

    $VMInfo = {} | select Name, OS, VMHost, IPAddress, NumCPU, TotalMemMB, AvgMemPcnt, MaxMemPcnt, AvgCPUMhz, AvgCPUPcnt, MaxCPUPcnt 
    $VMInfo.name = $Server 

    $MaxCPUPcnt = get-vm $Server | Get-Stat -Stat cpu.usage.average -IntervalSecs 1 | 
     Where-Object { $workingDays -contains $_.Timestamp.DayOfWeek -and $_.Timestamp.TimeOfDay -gt $dayStart.TimeOfDay -and $_.Timestamp.TimeOfDay -lt $dayEnd.TimeOfDay} | 
     Measure-Object value -Max | select Maximum 

    $MaxMemPcnt = get-vm $Server | Get-Stat -Stat mem.usage.average -IntervalSecs 1 | 
    Where-Object { $workingDays -contains $_.Timestamp.DayOfWeek -and $_.Timestamp.TimeOfDay -gt $dayStart.TimeOfDay -and $_.Timestamp.TimeOfDay -lt $dayEnd.TimeOfDay} | 
    Measure-Object value -Max | select Maximum 

    $AvgCPUMhz = get-vm $Server | Get-Stat -Stat cpu.usagemhz.average -IntervalMins 5 | 
     Where-Object { $workingDays -contains $_.Timestamp.DayOfWeek -and $_.Timestamp.TimeOfDay -gt $dayStart.TimeOfDay -and $_.Timestamp.TimeOfDay -lt $dayEnd.TimeOfDay} | 
     Measure-Object value -Average | select Average 

    $AvgCPUPcnt = get-vm $Server | Get-Stat -Stat cpu.usage.average -IntervalMins 5 | 
     Where-Object { $workingDays -contains $_.Timestamp.DayOfWeek -and $_.Timestamp.TimeOfDay -gt $dayStart.TimeOfDay -and $_.Timestamp.TimeOfDay -lt $dayEnd.TimeOfDay} | 
     Measure-Object value -Average | select Average 

    $AvgMemPcnt = get-vm $Server | Get-Stat -Stat mem.usage.average -IntervalMins 5 | 
     Where-Object { $workingDays -contains $_.Timestamp.DayOfWeek -and $_.Timestamp.TimeOfDay -gt $dayStart.TimeOfDay -and $_.Timestamp.TimeOfDay -lt $dayEnd.TimeOfDay} | 
     Measure-Object value -Average | select Average 

    $VMinfo.MaxCPUPcnt = [math]::round($MaxCPUPcnt.Maximum,2) 
    $VMinfo.MaxMemPcnt = [math]::round($MaxMemPcnt.Maximum,2) 
    $VMinfo.AvgCPUMhz = [math]::round($AvgCPUMhz.Average,2) 
    $VMInfo.AvgCPUPcnt = [math]::round($AvgCPUPcnt.Average,2) 
    $VMInfo.AvgMemPcnt = [math]::round($AvgMemPcnt.Average,2) 

    $TotalMemMB = get-vm $Server | select MemoryMB 
    $VMInfo.TotalMemMB = $TotalMemMB.MemoryMb 

    $VMInfo.VMHost = (get-vm gbvc0007 | Get-VMHost).name 

    $VMInfo.OS = (Get-VM $Server | Get-View).summary.config.GuestFullName 

    $VMInfo.IPAddress = (Get-VM $Server | Get-VIew).summary.guest.ipaddress 

    $VMInfo.NumCPU = (Get-VM $Server | Get-VIew).summary.config.NumCPU 

    $Report += $VMInfo 

    $Counter++ 
} 

clear 
$Report | ft -AutoSize 

$Report | Export-Csv -Path $CSVFile 

Disconnect-VIServer -Server * -Confirm:$false 

回答

0

嗯,首先,你會想做的事:

$ VM = GET- vm $服務器

並且隨後使用它,因此您不需要爲每個操作多花一分鐘時間查詢服務器。另外,我並不知道get-stat是如何工作的,但是如果您只需一次調用服務器就可以將虛擬機所需的所有統計信息提供給變量,那麼您可以稍後輕鬆過濾出所需的所有內容,從而節省大量的時間有可能,這樣的事情:

$vm = get-vm $Server 
$vm | Get-Stat -Stat mem.usage.average,cpu.usage.average,cpu.usagemhz.average 

而且你可以很容易得到的東西變的你已經有了這樣的:
$ vm.MemoryMB或大概就像$ vm.GuestID

你應該看看這裏 - http://www.virtualizationadmin.com/articles-tutorials/general-virtualization-articles/use-powercli-quick-stats-part2.html

至於一般方法,我建議你使用像PoshRSJob這樣的東西來平行執行你的foreach循環,這將改善你的腳本劇烈運行所花費的時間,唯一需要注意的是你需要在每個RSJob中進行身份驗證,這可能是一個問題。

+0

如果有幫助的話,您應該標記回答爲有幫助或有幫助;) – 4c74356b41

0

非常感謝您的回覆和回覆如此迅速。它指出我的方向正確。

我已經填充了一個變量,然後從中選擇。

$VM = Get-VM $Server 

$AvgStats = $VM | Get-Stat -Stat mem.usage.average,cpu.usage.average,cpu.usagemhz.average -IntervalMins 5 | 
    Where-Object { $workingDays -contains $_.Timestamp.DayOfWeek -and $_.Timestamp.TimeOfDay -gt $dayStart.TimeOfDay -and $_.Timestamp.TimeOfDay -lt $dayEnd.TimeOfDay} 

$AvgMemPcnt = $AvgStats | Where-Object {$_.metricid -eq "mem.usage.average"} | Measure-Object value -Average | select Average 

然後重複用每個度量的實際答案填充變量的行。