Hi,
Can someone please correct me, I want to report when the Vm is switched off.
The script works fine, but I can't print when the VM is turned off.
$report = [System.Collections.ArrayList]@()
Write-Host "Gathering VM statistics"
foreach($vm in Get-View -ViewType Virtualmachine) {
if (!$vm.Config) { continue }
$vms = [PSCustomObject]@{
VMName = $vm.Name
Hostname = $vm.guest.hostname
OS = $vm.Config.GuestFullName
Boottime = $vm.Runtime.BootTime
VMState = $vm.summary.runtime.powerState
#TotalCPU = $vm.summary.config.numcpu
#TotalMemory = $vm.summary.config.memorysizemb
UsedSpaceGB = [math]::Round($vm.Summary.Storage.Committed/1GB,2)
#MemoryShare = $vm.Config.MemoryAllocation.Shares.Leve
#OverallCpuUsage = $vm.summary.quickStats.OverallCpuUsage
MemoryUsage = $vm.summary.quickStats.guestMemoryUsage
MemoryReservation = $vm.resourceconfig.memoryallocation.reservation
#CPUreservation = $vm.resourceconfig.cpuallocation.reservation
#Capacity = $vm.ExtensionData.Summary.Capacity
Datastore = $vm.Config.DatastoreUrl[0].Name
Unit = ""
VIEvent = $vm.VmPoweredOffEvent --------> HERE i have problem.
}
foreach ($c in $vm.CustomValue) {
switch ($c.Key) {
"901" { $vms.Unit = $c.Value }
}
}
$Report.Add($vms) | Out-Null
}
$Report | Export-Csv -Path C:\Users\Administrator\Downloads\report_$(Get-Date -Format 'yyyy_MM_dd').csv -Delimiter ';' -NoTypeInformation