查看ESXI主机的资源使用情况

虽然Vcenter控制台默认可以看到ESXI主机当前的内存和CPU使用情况,但是需要每一台主机都点击查看,假如主机数量较多的时候,手工效率很低,此脚本可以收集vcenter下所有主机的CPU内存使用百分比.按照实际情况填写IP地址、用户名、密码即可,收集结果会以日期为命名保存在当前目录下

$ipaddress=''
$username=''
$passwd=''
Write-host "Script Running now , please waiting it.." -ForegroundColor Green
$erroractionpreference = 'SilentlyContinue'
$Infolist = @()
$Today=(get-date).tostring("yyyyMMdd")+"VMwareInfo"

Write-host "Loading VMware Module...." -ForegroundColor Green
ASNP VMware.Vimautomation.Core

Write-host "Login Vcenter...." -ForegroundColor Green
Connect-VIServer $ipaddress -User $username -Password $passwd

Write-host "Collecting Infomation...." -ForegroundColor Green
$esxihost=(Get-VMHost | % { $_.Name })

foreach($n in $esxihost)
{
    $cpuused=$(Get-VMHost $n.TrimEnd()).CpuUsageMhz
    $cputotal=$(Get-VMHost $n.TrimEnd()).CpuTotalMhz
    $cpupercent = ($cpuused * 100.0)/$cputotal
    $cpupercent = "{0:N1}" -f $cpupercent

    $memused=$(Get-VMHost $n.TrimEnd()).MemoryUsageGB
    $memtotal=$(Get-VMHost $n.TrimEnd()).MemoryTotalGB
    $mempercent = ($memused * 100.0)/$memtotal
    $mempercent = "{0:N1}" -f $mempercent

    $Info = New-Object PSObject
    $Info | Add-Member NoteProperty -Name 'Name' -Value $n.TrimEnd()
    $Info | Add-Member NoteProperty -Name 'CPU %' -Value $cpupercent
    $Info | Add-Member NoteProperty -Name 'Mem %' -Value $mempercent

    $Infolist+=$Info
}

$Infolist | Export-csv -NoTypeInformation -Encoding UTF8 "$Today.csv"

Write-host "Finish !" -ForegroundColor Green

cmd /c "pause"
exit

你可能感兴趣的:(vmware,powershell)