Quantcast
Channel: VMware Communities : All Content - All Communities
Viewing all articles
Browse latest Browse all 180329

VM Last Poweron

$
0
0

Hi

 

I'm trying to get a list of the vm in my environment that were not poweredon in 30 days and the last time they were powered on but I seem to be only getting the vm that are powered off.

 

Thanks

 

$vms

=Get-VM | where {$_.PowerState -eq"PoweredOff"}

$vmPoweredOff

=$vms | %{$_.Name}

$events

=Get-VIEvent-Start (Get-Date).AddDays(-30) -Entity $vms | where{$_.FullFormattedMessage -like"*is powered off"}

$lastMonthVM

=$events | %{$_.Vm.Name}

$vmPoweredOff

| where {!($lastMonthVM-contains$_)}

  

# Get a VM's last power on date based on the VM's events.

# Requires PowerCLI 4.0 and PowerShell v2.

function

Get-LastPowerOn {

 

param(

[Parameter(

Mandatory

=$true,

ValueFromPipeline

=$true,

HelpMessage

="VM"

)]

[

VMware.VimAutomation.Types.VirtualMachine]

 

$VM

)

 

Process {

 

# Patterns that indicate an attempt to power a VM on. This differ

 

# across versions and likely across language. Please add your own

 

# if you find one missing.

 

$patterns= @(

 

"*Power On virtual machine*", # vCenter 4 English

 

"*is starting*"# ESX 4/3.5 English

)

 

$events=$VM | Get-VIEvent

 

$qualifiedEvents= @()

 

foreach ($patternin$patterns) {

 

$qualifiedEvents+=$events | Where { $_.FullFormattedMessage -like$pattern }

}

 

$qualifiedEvents=$qualifiedEvents | Where { $_-ne$null }

 

$sortedEvents=Sort-Object-InputObject$qualifiedEvents-PropertyCreatedTime-Descending

 

$event=$sortedEvents | select-First 1

 

$obj=New-ObjectPSObject

 

$obj | Add-Member-MemberTypeNoteProperty-NameVM-Value$_

 

$obj | Add-Member-MemberTypeNoteProperty-NamePowerState-Value$_.PowerState

 

$obj | Add-Member-MemberTypeNoteProperty-NameLastPoweron-Value$null

 

if ($event) {

 

$obj.LastPoweron =$event.CreatedTime

}

 

Write-Output$obj

}

}

$Vms | Get-LastPowerOn | Export-CsvLastPowerOn.csv-NoTypeInformation-UseCulture


Viewing all articles
Browse latest Browse all 180329

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>