1 2 Previous Next 20 Replies Latest reply on Jan 17, 2019 9:25 AM by LucD

    VM Last Poweron

    AGFlora Hot Shot

      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 ($pattern in $patterns) {

       

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

      }

       

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

       

      $sortedEvents = Sort-Object -InputObject $qualifiedEvents -Property CreatedTime -Descending

       

      $event = $sortedEvents | select -First 1

       

      $obj = New-Object PSObject

       

      $obj | Add-Member -MemberType NoteProperty -Name VM -Value $_

       

      $obj | Add-Member -MemberType NoteProperty -Name PowerState -Value $_.PowerState

       

      $obj | Add-Member -MemberType NoteProperty -Name LastPoweron -Value $null

       

      if ($event) {

       

      $obj.LastPoweron = $event.CreatedTime

      }

       

      Write-Output $obj

      }

      }

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

        • 1. Re: VM Last Poweron
          LucD Guru
          User ModeratorsvExpertCommunity Warriors

          Have a look at Events – Part 5 : Powered off for more than 1 week ?

          Replace the 7 days with 30 days

          • 2. Re: VM Last Poweron
            AGFlora Hot Shot

            Hi Luc

             

            I did read your article and that's where I got the code from at the top of this post.

             

            Maybe my question should be how do I add the last time the vm was powered on?

             

            Thanks

            • 3. Re: VM Last Poweron
              LucD Guru
              Community WarriorsUser ModeratorsvExpert

              I meant something like this

               

              $vms = Get-VM | where {$_.PowerState -eq "PoweredOff"}
              $vmPoweredOff = $vms | %{$_.Name}
              $events = Get-VIEvent -Start (Get-Date).AddDays(-31) -Entity $vms -MaxSamples ([int]::MaxValue) | 
                  where{$_.FullFormattedMessage -like "*is powered off"}
              $lastweekVM = $events | %{$_.Vm.Name}
              $vmPoweredOff | where {!($lastweekVM -contains $_)}
              
              • 4. Re: VM Last Poweron
                AGFlora Hot Shot

                Luc

                 

                How can I take your script:

                 

                $vms = Get-VM | where {$_.PowerState -eq "PoweredOff"}
                $vmPoweredOff = $vms | %{$_.Name}
                $events = Get-VIEvent -Start (Get-Date).AddDays(-31) -Entity $vms -MaxSamples ([int]::MaxValue) | 
                    where{$_.FullFormattedMessage -like "*is powered off"}
                $lastweekVM = $events | %{$_.Vm.Name}
                $vmPoweredOff | where {!($lastweekVM -contains $_)}
                

                ...and find out when was the last time the vm that have been powered down for 30 days powered on?

                Maybe make your script into a function and the code that get's the powred on date into a function as well then pass the vm names to the function?
                • 5. Re: VM Last Poweron
                  LucD Guru
                  User ModeratorsCommunity WarriorsvExpert

                  Ok, so if I get the question correctly you want to find out for the currently powered off VMs when they were last powered on and that should be more than 30 days ago ?

                  The attached script uses my Get-VIEventPlus function from my Get the vMotion/svMotion history post to find these.

                  Have a look.

                   

                  Using a function that does this VM per VM would take forever I'm afraid.

                  • 6. Re: VM Last Poweron
                    AGFlora Hot Shot

                    Hi Luc

                     

                    Actually i need the following:

                     

                    1. all vm that have not been powered on for 30 days or more

                    2. the last time these vm that have not been powered on for 30 days or more-the last time they were powered on

                    3. vm Owner annotation

                     

                    vm  LastPowerOn Owner

                    ----  ------------------- ----------

                     

                     

                    How difficult is it to list the owner annotation for each vm to this script?

                    • 7. Re: VM Last Poweron
                      LucD Guru
                      vExpertUser ModeratorsCommunity Warriors

                      Try changing the last line from

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

                      to

                      Select @{N="VM";E={$_.Vm.Name}},CreatedTime,UserName
                      • 8. Re: VM Last Poweron
                        AGFlora Hot Shot

                        Thanks this worked...

                         

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

                         

                        So let me make sure I understand what's going on here...

                         

                        Your script finds all the vm that have not been powered on for 30 days or more and lists the vm name, last time the vm was powered on, and now the user name. Is that correct?

                        • 9. Re: VM Last Poweron
                          LucD Guru
                          User ModeratorsCommunity WarriorsvExpert

                          That is correct

                          • 10. Re: VM Last Poweron
                            AGFlora Hot Shot

                            Great!

                             

                            One last question if I may...

                             

                            I'm trying to get a custom attribute but the following does not work:

                             

                            Select

                             

                            @{N="VM";E={$_.Vm.Name}},CreatedTime,UserName,@{N="Department";E={$_.CustomFields.Item("Department")}}

                             

                            What am I doing wrong?

                            • 11. Re: VM Last Poweron
                              LucD Guru
                              vExpertUser ModeratorsCommunity Warriors

                              Try it like this

                               

                               

                              Select @{N="VM";E={$_.Vm.Name}},CreatedTime,UserName, 
                              @
                              {N="Department";E={(Get-VM $_.Vm.Name).CustomFields.Item("Department")}}
                              • 12. Re: VM Last Poweron
                                AGFlora Hot Shot

                                Thanks again...I'm set now.

                                • 13. Re: VM Last Poweron
                                  AGFlora Hot Shot

                                  The script works great with the exception that one vm turned up in the report that wasn't quite powered off for 30 days...

                                   

                                  2/17/2013 8:53

                                  • 14. Re: VM Last Poweron
                                    LucD Guru
                                    vExpertCommunity WarriorsUser Moderators

                                    Do you know when it was last powered off ?

                                    You could look with

                                    $vm = Get-VM -Name ThatVM

                                    $events = Get-VIEvent -Start (Get-Date).AddDays(-31) -Entity $vm -MaxSamples ([int]::MaxValue) |
                                       where{$_.FullFormattedMessage -like "*is powered off"}
                                    $events

                                    if there was a poweroff in the last 31 days.

                                    1 2 Previous Next