PowerCLi获取宿主和虚机性能信息

 

#该脚本用来根据csv定义的信息批量创建虚机
$vc = "vc.y.com"
$user = "ZabbixMonitor@vc.com" #只读账号即可
$password = "Mpassword"

Set-PowerCLIConfiguration -InvalidCertificateAction ignore -Confirm:$false
Import-Module vmware.vimautomation.core

Connect-VIServer -Server $vc -username $user -Password $password -Force


#虚机总数、运行中的数量、未运行数量
$VMs = Get-VM
$vms_all = $VMs.Count
$vms_on = ($VMs | ? {$_.PowerState -eq "PoweredOn"}).count
$vms_off = $vms_all - $vms_on

#已分配给虚机的CPU、内存、存储
$vms_all_cpuNum_allocated = ($VMs |Measure-Object NumCpu -Sum).Sum
[int]$vms_all_memory_allocated = ($VMs |Measure-Object MemoryGB -Sum).Sum

[int]$vms_all_datastore_allocated = ($VMs|get-harddisk |Measure-Object CapacityGB -Sum).Sum


#宿主机CPU总量、CPU已用量、内存总量、内存已用量
$Hosts = Get-VMHost
$vmHosts_num = $Hosts.count
$vmHosts_cpu_all_num = ($Hosts |Measure-Object NumCpu -Sum).Sum
$vmHosts_cpu_all_freq = ($Hosts |Measure-Object CpuTotalMhz -Sum).Sum
$vmHosts_cup_used_freq = ($Hosts |Measure-Object CpuUsageMhz -Sum).Sum

[int]$vmHosts_memory_all = ($Hosts |Measure-Object MemoryTotalGB -Sum).Sum
[int]$vmHosts_memory_used = ($Hosts |Measure-Object MemoryUsageGB -Sum).Sum

#存储总量、剩余、已使用量
$datastores = Get-Datastore
[int]$datastore_all = ($datastores |Measure-Object CapacityGB -Sum).Sum
[int]$datastore_free = ($datastores |Measure-Object FreeSpaceGB -Sum).Sum
[int]$datastore_used = $datastore_all - $datastore_free



####################################################################################################

$VMObject = New-Object PSObject
$VMObject | Add-Member -MemberType NoteProperty -Name 宿主机数量 -Value $vmHosts_num -PassThru |
Add-Member -MemberType NoteProperty -Name 虚机数量 -Value $vms_all -PassThru |
Add-Member -MemberType NoteProperty -Name 虚机数量_运行中 -Value $vms_on -PassThru |
Add-Member -MemberType NoteProperty -Name 虚机数量_未运行 -Value $vms_off -PassThru |
Add-Member -MemberType NoteProperty -Name 宿主机CPU个数 -Value $vmHosts_cpu_all_num -PassThru |
Add-Member -MemberType NoteProperty -Name 宿主机CPU总量_GHz -Value $vmHosts_cpu_all_freq -PassThru |
Add-Member -MemberType NoteProperty -Name 宿主机CPU使用量_GHz -Value $vmHosts_cup_used_freq -PassThru |
Add-Member -MemberType NoteProperty -Name 宿主机内存总量 -Value $vmHosts_memory_all -PassThru |
Add-Member -MemberType NoteProperty -Name 宿主机内存使用量 -Value $vmHosts_memory_used -PassThru |
Add-Member -MemberType NoteProperty -Name 虚机已分配CPU总个数 -Value $vms_all_cpuNum_allocated -PassThru |
Add-Member -MemberType NoteProperty -Name 虚机已分配内存总量 -Value $vms_all_memory_allocated -PassThru |
Add-Member -MemberType NoteProperty -Name 虚机已分配存储总量 -Value $vms_all_datastore_allocated -PassThru |
Add-Member -MemberType NoteProperty -Name 存储总量 -Value $datastore_all -PassThru |
Add-Member -MemberType NoteProperty -Name 存储已用量 -Value $datastore_used -PassThru 

Disconnect-VIServer -Server $vc -Confirm:$false

 

posted on 2022-01-19 17:32  momingliu11  阅读(319)  评论(0编辑  收藏  举报