PowerCLI Script – Report Virtual Machines with 3 days older VM Snapshots

简介:

This could be one among the very few PowerCLI scripts in my blog. I am in learning phase to create a PowerCLI script to automate and get the report of virtual infrastructure within short span of time. This script also created with the help of one of my friend. In this post, I am going to discuss about the PowerCLI script which helps us to report the Virtual Machines with 3 days older VM snapshots and export the output in Excel (.CSV) file. It is always recommended not to keep VM snapshots more than 24- 72 hours. The VM snapshot file continues to grow in size when it is retained for a longer period. This can cause the snapshot storage location to run out of space and impact the system performance. This PowerCLI script could be really useful to identify the Virtual machines with 3 days older snapshot and delete them immediately or raise a concern to the respective team for the snapshot deletion before VM snapshots causes outage to your production infrastructure.

PowerCLI Script – Report Virtual Machines with 3 days older VM Snapshots

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

#*************************************************************************************************************

# Script Name : VMSnapShot3DaysOld.ps1

# Purpose : Get the report of VMS Snapshot 3 Days Old and save it in CSV file

# Date : 27-10-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 = "VMSnapShot3DaysOld_" + (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

$Result = @()

 

If($? -Eq $True)

 

{

Write-Host "Connected" -Foregroundcolor "Green"

$Results = @()

$datetime = Get-Date

 

$threeDaysAgo = (Get-Date).AddDays(-3)

Get-VM | Foreach-Object {

Get-Snapshot -VM $_ | Foreach-Object {

If($_.Created -lt $threeDaysAgo) {

$Result += $_ | Select VM,@{Name="Snapshot Name";E={$_.Name}},@{Name="Date Created";E={$_.Created}}

}

}

}

 

$Result | Export-Csv -NoTypeInformation $LogFile

}

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 VMSnapShot3DaysOld.ps1

  1. Execute the Script in PowerCLI “.\VMSnapShot3DaysOld.ps1″

  2. Input  vCenter Server Name to execute the script to get the report of VM Snapshots older then 3 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.

VM Snapshots older than 3 days

Script Output

Virtual Machines with 3 days older VM Snapshots will be exported and saved in the Microsoft Excel output file with the filename “VMSnapshot3DaysOld_Today_Day.CSV”  under the same directory where the PowerCLI Script “VMSnapshot3Daysold.PS1”  is located.
vm-snapshots-older-than-3-days-1

When you open the output Excel (.CSV) file “VMSnapshot3DaysOld_Today_Day.CSV”, You will be able to get the details of Virtual Machine which has 3 days older VM Snapshots.  This output file contains Virtual Machine Name, Snapshot Name and Date Created.

vm-snapshots-older-than-3-days-2

That’s it. We are done with the PowerCLI script and got the awesome output with the list of Virtual machines which contains 3 Days older VM Snapshot. I hope this script will be informative for you.  Thanks for Reading!!!. Be social and share it in social media, if you feel worth sharing it.

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

520feng2007
相关文章
|
Java Shell PHP
Guidelines for Function Compute Development - Use Fun Local for Local Running and Debugging
Preface The following key concepts are involved in this document: Function Compute: an event-driven service that allows you to focus on writing and .
1739 0
Running Homebrew as root is extremely dangerous and no longer supported.
版权声明:本文为 testcs_dn(微wx笑) 原创文章,非商用自由转载-保持署名-注明出处,谢谢。 https://blog.csdn.net/testcs_dn/article/details/81113749 ...
2666 0