VMware Cloud Community
peter79
Enthusiast
Enthusiast
Jump to solution

Script for finding VM's powered down for more than 7 days

Guys,

I need a bit help I'm looking for a script that will find all VM's that have been powered down for more than 7 days.  I would also like to output the names of theese VM's and how many days they have been powered down for to an excel spreadsheet.

Thanks in advance.

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Sorry, my mistake.

The correct script is now attached to the previous answer


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

View solution in original post

Reply
0 Kudos
10 Replies
LucD
Leadership
Leadership
Jump to solution

Try this.

It gets all poweredoff VMs and then removes the ones that were powered off during the last 7 days

$vms = Get-VM | where {$_.PowerState -eq "PoweredOff"}
$vmNames = New-Object System.Collections.ArrayList
$vms | %{$vmNames.Add($_.Name)} | Out-Null
Get-VIEvent -Start (Get-Date).AddDays(-7) -MaxSamples 99999 -Entity $vms | `
where {$_.GetType().Name -eq "VMPoweredOffEvent"} | %{
   
if($vmNames -contains $_.VM.Name){
       
$vmNames.Remove($_.VM.Name)
    }
}
$vmNames | Export-Csv "C:\VM-poweredoff-longer-than-7-days.csv" -NoTypeInformation


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

Reply
0 Kudos
peter79
Enthusiast
Enthusiast
Jump to solution

Tried running that but got an error saying

"Where-Object : A positional parameter cannot be found that accepts '$null'"

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

It looks as if the Get-VIEvent cmdlet didn't return anything. That could be because $vms is empty

The script looks for guests that are still powered off.

Does this return anything ?

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


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

Reply
0 Kudos
avlieshout
VMware Employee
VMware Employee
Jump to solution

Try this little gem

Function Get-PowerOffEvents {
  $taskNumber = 100
  $taskMgr = Get-View TaskManager
  
  $filter = New-Object VMware.Vim.TaskFilterSpec
  $filter.state = New-Object VMware.Vim.TaskInfoState
  $filter.state = "success"

  $taskCollector = Get-View ($taskMgr.CreateCollectorForTasks($filter))
  
  $taskCollector.RewindCollector | Out-Null 
  $taskCollection = $taskCollector.ReadNextTasks($taskNumber)
  $matches=@()
  while ($taskCollection) {
    $matches += $taskCollection | Where {$_.DescriptionId -eq "VirtualMachine.powerOff" -or $_.DescriptionId -eq "VirtualMachine.shutdownGuest"}
    $taskCollection = $taskCollector.ReadNextTasks($taskNumber)
  }
  $taskCollector.DestroyCollector() 
  $matches | Select EntityName,CompleteTime,@{N="DaysAgo";E={((Get-Date)-$_.CompleteTime).Days}},@{N="User";E={$_.reason.username}}
}

$vms = Get-VM | Where {$_.PowerState -eq "PoweredOff"} | Select -ExpandProperty Name
$events = @(Get-PowerOffEvents | Where {$vms -contains $_.entityName} | Group-Object -Property entityName)
$events | %{$_.Group | Sort -Property DaysAgo | Select -First 1} | `
    Sort -Property EntityName | Export-Csv "C:\Scripts\PoweredOffVMs.csv" -NoTypeInformation

this won't filter for older than 7 days, but this can be easily changed by replacing the last line with:

$events | %{$_.Group | Sort -Property DaysAgo | Select -First 1} | Where {$_.DaysAgo -gt 7} | `
    Sort -Property EntityName | Export-Csv "C:\Scripts\PoweredOffVMs.csv" -NoTypeInformation

Cheers,

Arnim

Arnim van Lieshout Blogging: http://www.van-lieshout.com Twitter: http://www.twitter.com/avlieshout If you find this information useful, please award points for "correct" or "helpful".
Reply
0 Kudos
peter79
Enthusiast
Enthusiast
Jump to solution

Yes that returns a list of powered down VM's.

Reply
0 Kudos
peter79
Enthusiast
Enthusiast
Jump to solution

I typed out the script again and ran now I'm geeting the following error

" ' missing a terminator"

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Do you run this from the PowerCLI prompt ? As a .ps1 file ?

Or from the PowerShell ISE ?

It works for me.


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

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You can copy/paste the script from your browser.

I attach the script as well as a .ps1 file


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

Reply
0 Kudos
peter79
Enthusiast
Enthusiast
Jump to solution

Sorry I must have copied th e code incorrectly (I run the scripts as ps1 files).  It runs but the outputed CSV file but it only contains a column labelled lengh and some entries below but no vmnames.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Sorry, my mistake.

The correct script is now attached to the previous answer


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

Reply
0 Kudos