raj8
Contributor
Contributor

poweredoff VM's

Hi,

I was unable to generate the poweredoff vm's list with below script. some of the vm's are left unreported. could you please help me

$ExportFilePath = "D:\atl.csv"

$Report = @()

$VMs = get-vm |Where-object {$_.powerstate -eq "poweredoff"}

$Datastores = Get-Datastore | select Name, Id

$VMHosts = Get-VMHost | select Name, Parent

### Get powered off event time:

Get-VIEvent -Entity $VMs -MaxSamples ([int]::MaxValue) |

where {$_ -is [VMware.Vim.VmPoweredOffEvent]} |

Group-Object -Property {$_.Vm.Name} | %{

  $lastPO = $_.Group | Sort-Object -Property CreatedTime -Descending | Select -First 1

  $vm = Get-VIObjectByVIView -MORef $lastPO.VM.VM

  $report += New-Object PSObject -Property @{

    VMName = $vm.Name

    Powerstate = $vm.Powerstate

    OS = $vm.Guest.OSFullName

    IPAddress = $vm.Guest.IPAddress[0]

    ToolsStatus = $VMView.Guest.ToolsStatus

    Host = $vm.host.name

    Cluster = $vm.host.Parent.Name

    Datastore = ($Datastores | where {$_.ID -match (($vmview.Datastore | Select -First 1) | Select Value).Value} | Select Name).Name

    NumCPU = $vm.NumCPU

    MemMb = [Math]::Round(($vm.MemoryMB),2)

    DiskGb = [Math]::Round((($vm.HardDisks | Measure-Object -Property CapacityKB -Sum).Sum * 1KB / 1GB),2)

    PowerOFF = $lastPO.CreatedTime

    Note = $vm.Notes  }

}

$Report = $Report | Sort-Object VMName

if ($Report) {

  $report | Export-Csv $ExportFilePath -NoTypeInformation}

else{

  "No PoweredOff events found"

}

0 Kudos
7 Replies
LucD
Leadership
Leadership

Can you be a bit more specific, and/or give some more details ?

For which VMs do you not get an entry in the report ?

Are these VMs that were powered off quite some time ago ?

What is the retention period for Tasks and Events in the environment ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
raj8
Contributor
Contributor

Hi Lucd,

please find below

Vm's are which are in poweredoff state are not reported in csv. only few are are exported in excel.

not sure of poweredoff time, might be long back.

DB retention period was not set

0 Kudos
LucD
Leadership
Leadership

Ok, I would `suggest to investigate one of the VMs that is powered off and that does not appear in the report.

Fetch the power off events for that VM, and verify if there any events returned.

$vmName = 'MyVM'

$vm = Get-VM -Name $vmName

Get-VIEvent -Entity $vm -MaxSamples ([int]::MaxValue) |

where{$_ -is [VMware.Vim.VmPoweredOffEvent]} |

Select @{N='VM';E={$_.Vm.Name}},CreatedTime


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
raj8
Contributor
Contributor

Hi Lucd,

Tried it on  vm which was not reported, it returned nothing

PS C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> $vmName = 'atlvgar-eod03'

PS C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI>

PS C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> $vm = Get-VM -Name $vmName

PS C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI>

PS C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI>

PS C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI>

PS C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> Get-VIEvent -Entity $vm -MaxSamples ([int]::MaxValue) |

>>

>> where{$_ -is [VMware.Vim.VmPoweredOffEvent]} |

>>

>> Select @{N='VM';E={$_.Vm.Name}},CreatedTime

>>

PS C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI>

0 Kudos
LucD
Leadership
Leadership

That would mean there is no event of that type for that specific VM.

Why is difficult to determine from where I'm sitting.

You could run the following to check what event types are available for that VM.

$vmName = 'MyVM'

$vm = Get-VM -Name $vmName

Get-VIEvent -Entity $vm -MaxSamples ([int]::MaxValue) |

Group-Object -Property {$_.GetType().Name} |

Sort-Object -Property Name

Perhaps this VM has another eventtype for it's power off.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
raj8
Contributor
Contributor

it returned nothing as well. does it mean there are no events for this vm?

PS C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> $vmName = 'atlvgar-eod03'

PS C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI>

PS C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> $vm = Get-VM -Name $vmName

PS C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI>

PS C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI>

PS C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI>

PS C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> Get-VIEvent -Entity $vm -MaxSamples ([int]::MaxValue) |

>>

>> Group-Object -Property {$_.GetType().Name} |

>>

>> Sort-Object -Property Name

>>

PS C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI>

0 Kudos
LucD
Leadership
Leadership

Not necessarily.

Let's check if there any events related to this VM.

$vmName = 'MyVM'

Get-VIEvent -MaxSamples ([int]::MaxValue) |

where{$_.VM.Name -eq $vmName} |

Group-Object -Property {$_.GetType().Name} |

Sort-Object -Property Name


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos