Most of the time, we will be excited to create or run PowerCLI Script to accomplish the tasks within short period of time by eliminating lot of manual process. In most of the enterprise environment, I feel 2 tasks (VM Build & VM Decommission) which is very difficult to track due to its high volume. Among these 2 tasks, tracking VM for decommission is the tough tasks. Respective application or developer team have given go ahead for decommission the virtual machines. Support team would have powered off that virtual machines but forgot to delete the VM because there are lot of different process may be evolved with the VM deletion and decommission. Due to that, lot of virtual machines would have left out and lying in clusters and datastores in powered off state by occupying lot of costlier storage space. So we need to keep track of that powered off virtual machines. I believe Virtual machines in powered off state for atleast 30 days would be good candidate for decommission. This PowerCLI Script will help you to get the report with the list of Powered off virtual machines which is older than 30 days in the vCenter server. This report will help you to find the good candidate for decommission to reclaim the occupied storage to save the cost to your organization.
PowerCLI Script – Report Powered off VM’s older than 30 days
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
#************************************************************************************************************* # Script Name : VMPoweredOff30DaysAgo.ps1 # Purpose : Get the report of VMS Powered Off 30 Days ago # Date : 28-11-2016 # - Initial version # : # Author : www.vmwarearena.com # #************************************************************************************************************* # If(!(Get-PSSnapin | Where {$_.Name -Eq "VMware.VimAutomation.Core"})) { Add-PSSnapin VMware.VimAutomation.Core } $VCServer = Read-Host 'Enter VC Server name' $vcUSERNAME = Read-Host 'Enter user name' $vcPassword = Read-Host 'Enter password' -AsSecureString $vccredential = New-Object System.Management.Automation.PSCredential ($vcusername, $vcPassword)
$LogFile = "VMPoweredOff_" + (Get-Date -UFormat "%d-%b-%Y-%H-%M") + ".csv"
Write-Host "Connecting to $VCServer..." -Foregroundcolor "Yellow" -NoNewLine $connection = Connect-VIServer -Server $VCServer -Cred $vccredential -ErrorAction SilentlyContinue -WarningAction 0 | Out-Null $Global:Report = @()
If($? -Eq $True)
{ Write-Host "Connected" -Foregroundcolor "Green"
$PoweredOffAge = (Get-Date).AddDays(-30) $Output = @{} $PoweredOffvms = Get-VM | where {$_.PowerState -eq "PoweredOff"} $EventsLog = Get-VIEvent -Entity $PoweredOffvms -Finish $PoweredOffAge -MaxSamples ([int]::MaxValue) | where{$_.FullFormattedMessage -like "*is powered off"} If($EventsLog) { $EventsLog | %{ if($Output[$_.Vm.Name] -lt $_.CreatedTime) { $Output[$_.Vm.Name] = $_.CreatedTime } } } $Result = $Output.getEnumerator() | select @{N="VM";E={$_.Key}},@{N="Powered Off Date";E={$_.Value}}
If($Result) { $Result | Export-Csv -NoTypeInformation $LogFile } Else { "NO VM's Powered off last 30 Days" } } Else { Write-Host "Error in Connecting to $VIServer; Try Again with correct user name & password!" -Foregroundcolor "Red" }
Disconnect-VIServer * -Confirm:$false # #------------------------------------------------------------------------------------------------------------- |
How to Execute the Script?
Copy and Paste the above Script and save it in the Notepad. Name the script as “VMPoweredoff30DaysAgo.ps1″
Execute the Script in PowerCLI “.\VMPoweredoff30DaysAgo.ps1″
Input vCenter Server Name to execute the script to get the report of powered off VM’s older than 30 days
Enter the Username with administrative credentials on vCenter Server
Enter the password for the above entered Username
Hit Enter to execute the script and pull the report.
Script Output
Virtual machines powered off older than 30 days report will be exported and saved in the Microsoft Excel output file with the filename “VMPoweredOff_Today_Date-time.CSV” under the same directory where the PowerCLI Script “VMPoweredoff30DaysAgo.ps1.ps1” is located.
I hope script will be useful for you to save lot of time and avid manual works. Thanks for Reading !!! Be social and share it in social media, if you feel worth sharing it.