Highlighted
Enthusiast
Enthusiast

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

Jump to solution

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.

0 Kudos
1 Solution

Accepted Solutions
Highlighted
User Moderator
User Moderator

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

0 Kudos
10 Replies
Highlighted
User Moderator
User Moderator

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

0 Kudos
Highlighted
Enthusiast
Enthusiast

Tried running that but got an error saying

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

0 Kudos
Highlighted
User Moderator
User Moderator

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

0 Kudos
Highlighted
VMware Employee
VMware Employee

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".
0 Kudos
Highlighted
Enthusiast
Enthusiast

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

0 Kudos
Highlighted
Enthusiast
Enthusiast

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

" ' missing a terminator"

0 Kudos
Highlighted
User Moderator
User Moderator

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

0 Kudos
Highlighted
User Moderator
User Moderator

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

0 Kudos
Highlighted
Enthusiast
Enthusiast

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.

0 Kudos
Highlighted
User Moderator
User Moderator

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

0 Kudos