PowerCLI Script – Report Powered off VM’s older than 30 days

简介:

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″

  1. Execute the Script in PowerCLI “.\VMPoweredoff30DaysAgo.ps1

  2. Input  vCenter Server Name to execute the script to get the report of powered off VM’s older than 30 days

  3. Enter the Username with administrative credentials on vCenter Server

  4. Enter the password for the above entered Username

  5. Hit Enter to execute the script and pull the report.

PowerCLI Script

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.

PowerCLI Script

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.

本文转自学海无涯博客51CTO博客,原文链接http://blog.51cto.com/549687/1881866如需转载请自行联系原作者

520feng2007
相关文章
|
4月前
|
JavaScript 前端开发
We‘re sorry but xxxxxx doesn‘t work properly without JavaScript enabled.
We‘re sorry but xxxxxx doesn‘t work properly without JavaScript enabled.
|
Web App开发 JavaScript 前端开发
Selenium使用中报错:We\'re sorry but hr-frontend-v2 doesn\'t work properly without JavaScript enabled
Selenium使用中报错:We\'re sorry but hr-frontend-v2 doesn\'t work properly without JavaScript enabled. Please enable it to continue 这个错误提示表明目标网页要求启用JavaScript才能正常工作,而默认情况下,Selenium WebDriver是启用JavaScript的。如果遇到此错误,请按照以下步骤尝试解决问题
742 0
Selenium使用中报错:We\'re sorry but hr-frontend-v2 doesn\'t work properly without JavaScript enabled
|
测试技术
loadrunner 运行脚本-Run-time Settings->General->Additional attributes设置
loadrunner 运行脚本-Run-time Settings->General->Additional attributes设置
100 0
【hacker的错误集】DeprecationWarning: find_element_by_* commands are deprecated.
DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead。依旧是使用单词意思分析报错原因
143 0
【hacker的错误集】DeprecationWarning: find_element_by_* commands are deprecated.
|
Oracle 关系型数据库 Linux
Oracle Linux: Shell Script to Calculate Values Recommended Linux HugePages / HugeTLB Configuration (Doc ID 401749.1)
Oracle Linux: Shell Script to Calculate Values Recommended Linux HugePages / HugeTLB Configuration (Doc ID 401749.1)
215 0
SAP MM FK08 (Confirm change vendor) Usage Research
SAP MM FK08 (Confirm change vendor) Usage Research
SAP MM FK08 (Confirm change vendor) Usage Research
|
前端开发 Shell
React Native:Debugger and device times have drifted by more than 60s. Please correct this by running adb shell "date `date +%m%d%H%S`"
问题 Debugger and device times have drifted by more than 60s. Please correct this by running adb shell "date `date +%m%d%H%M%T.
3936 0