PowerCLI

Script: Track-Datastores.ps1

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#Global Functions
#This function generates a nice HTML output that uses CSS for style formatting.
function Generate-Report {
    Write-Output "<html><head><title></title><style type=""text/css"">.Error {color:#FF0000;font-weight: bold;}.Title {background: #0077D4;color: #FFFFFF;text-align:center;font-weight: bold;}.Normal-left {text-align:left;}.Normal {text-align:center;}</style></head><body><table><tr class=""Title""><td colspan=""3"">  VMware Datastore Change Report  </td></tr><tr class="Title"><td>Datastore</td><td>  Difference(MB)  </td><td>  % Free  </td></tr>"
 
                Foreach ($line in $myColDiff){
                    Write-Output "<td class=""Normal-left"">$($line.name)</td><td class=""Normal"">$($line.Diff)</td><td class=""Normal"">$($line.PercentFree)</td></tr> "
                }
        Write-Output "</table></body></html>"
    }
   
# Variables #
#############

$VIServer = "VISERVER"
$username = "changeme"
$password = "supersecret"
$digits = 2
$Folder = 'C:\Reports'
$currentFile = 'Datastores_Current.xml'
$previousFile = 'Datastores_Previous.xml'

# Script #
##########

# First, if a Current file exists, rename this Current file to Previous

If (Test-Path "$Folder\$currentFile")
{
    Remove-Item -Path "$Folder\$previousFile" -Force
    Rename-Item -Path "$Folder\$currentFile" -NewName $previousFile
}

# Next, let's measure and save current datastore sizes

# Connect to Virtual Center
$VC = Connect-VIServer $VIServer -user $username -password $password

# Get all datastores and put them in alphabetical order
$datastores = Get-Datastore | Sort-Object Name

# Create an array to hold the output
$myColCurrent = @()

# Loop through datastores
ForEach ($store in $datastores)
{
    # Create a custom object and define its properties
    $myObj = "" | Select-Object Name, CapacityGB, UsedGB, PercFree
    # Set the values of each property
    $myObj.Name = $store.name
    $myObj.CapacityGB = [math]::Round($store.capacityMB/1024,$digits)
    $myObj.UsedGB = [math]::Round(($store.CapacityMB - $store.FreeSpaceMB)/1024,$digits)
    $myObj.PercFree = [math]::Round(100*$store.FreeSpaceMB/$store.CapacityMB,$digits)
    # Add the object to the output array
    $myColCurrent += $myObj
}

# Export the output to an xml file; the new Current file
$myColCurrent | Export-Clixml -Path "$Folder\$currentFile"

# Disconnect from Virtual Center
Disconnect-VIServer -Confirm:$False

# Finally, let's compare the Current information to that in the Previous file

# Check if a Previous file exists
If (Test-Path "$Folder\$previousFile")
{
    # Import the Previous file
    $myColPrevious = Import-Clixml "$Folder\$previousFile"
    # Create an array to hold the differences
    $myColDiff = @()
    # Loop through the current datastores
    ForEach ($myObjCurrent in $myColCurrent)
    {
        # The actual compare command
        $diff = Compare-Object ($myColPrevious | Where { $_.Name -eq $myObjCurrent.Name }) $myObjCurrent -Property PercFree
        # In case of any differences, try to get specifics
        If ($diff)
        {
            # Again, a custom object and properties for outputting results
            $myObjDiff = "" | Select-Object Name, PercentFree, Diff
            # Again, setting the values of each property
            $myObjDiff.Name = $myObjCurrent.Name
            $myObjDiff.PercentFree = $myObjCurrent.PercFree
            # The most important property is the calculated difference between the current and previous values of PercFree. You can substitute it for UsedGB if you like.
            $myObjDiff.Diff = ($diff | Where { $_.SideIndicator -eq '=>' }).PercFree - ($diff | Where { $_.SideIndicator -eq '<=' }).PercFree
            # And agin, adding it to the output array
            $myColDiff += $myObjDiff
        }
        # Clearing the variable used inside the loop to prevent incorrect output in case of problems setting the variable!
        Clear-Variable diff -ErrorAction "SilentlyContinue"
    }
    # If nothing changed, we don't want an empty file.
    If ($myColDiff.Length -eq 0)
    {
        $myColDiff = "No changes since last check."
    }
    # And we conclude by outputting the results to a text file, which can be emailed or printed.
    Generate-Report > "$Folder\Track-Datastore.html"
}

IF ($myColDiff -ne "No changes since last check."){
    $SmtpClient = New-Object system.net.mail.smtpClient
    $SmtpClient.host = "my.smtp.host"   #Change to a SMTP server in your environment
    $MailMessage = New-Object system.net.mail.mailmessage
    $MailMessage.from = "System.Automation@example.com"   #Change to email address you want emails to be coming from
    $MailMessage.To.add("yomomma@example.com")  #Change to email address you would like to receive emails.
    $MailMessage.IsBodyHtml = 1
    $MailMessage.Subject = "VMware Datastore Change Report"
    $MailMessage.Body = Generate-Report
    $SmtpClient.Send($MailMessage)}

Script: Report-Snapshots.ps1

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
63
64
65
66
67
68
69
70
@"
===============================================================================
Title:             Report-Snaphots.ps1
Description:     List snapshots on all VMWARE ESX/ESXi servers as well as VM's
managed by Virtual Center.
Requirements:     Windows Powershell and the VI Toolkit
Usage:            .\Report-Snaphots.ps1
===============================================================================
"
@

#Global Functions
#This function generates a nice HTML output that uses CSS for style formatting.
function Generate-Report {
Write-Output "<html><head><title></title><style type=""text/css"">.Error {color:#FF0000;font-weight: bold;}.Title {background: #0077D4;color: #FFFFFF;text-align:center;font-weight: bold;}.Normal {}</style></head><body><table><tr class=""Title""><td colspan=""6"">VMware Snaphot Report</td></tr><tr><td>VM Name  </td><td>Snapshot Name  </td><td>Date Created  </td><td>Size(MB)  </td><td>Description  </td><td>Host  </td></tr>"

Foreach ($snapshot in $report){
Write-Output "<td>$($snapshot.vm)</td><td>$($snapshot.name)</td><td>$($snapshot.created)</td><td>$($snapshot.sizemb)</td><td>$($snapshot.description)</td><td>$($snapshot.host)</td></tr> "
}
Write-Output "</table></body></html>"
}

#Login details for standalone ESXi servers
$username = 'changeme'
$password = 'supersecret' #Change to the root password you set for you ESXi server

#Folder to store report
$Folder = "C:\Reports"

#List of servers including Virtual Center Server.  The account this script will run as will need at least Read-Only access to Cirtual Center
$ServerList = "vc1.example.net"#, "vc2.example.net", "vc3.example.net"    #Chance to DNS Names/IP addresses of your ESXi servers or Virtual Center Server

#Initialise Array
$Report = @()

#Get snapshots from all servers
foreach ($server in $serverlist){

# Check is server is a Virtual Center Server and connect with current user
if ($server -eq "VCServer"){Connect-VIServer $server}

# Use specific login details for the rest of servers in $serverlist
else {Connect-VIServer $server -user $username -password $password}

get-vm | get-snapshot | %{
$Snap = {} | Select VM,Name,Created,SizeMB,Description,Host
$Snap.VM = $_.vm.name
$Snap.Name = $_.name
$Snap.Created = $_.created
$Snap.SizeMB = $_.sizemb
$Snap.Description = $_.description
$Snap.Host = $_.vm.host.name
$Report += $Snap
}
}

# Disconnect from Virtual Center
Disconnect-VIServer -Confirm:$False

# Generate the report and email it as a HTML body of an email
Generate-Report > "$Folder\Report-Snapshots.html"
IF ($Report -ne ""){
$SmtpClient = New-Object system.net.mail.smtpClient
$SmtpClient.host = "my.smtp.host"   #Change to a SMTP server in your environment
$MailMessage = New-Object system.net.mail.mailmessage
$MailMessage.from = "System.Automation@example.com"   #Change to email address you want emails to be coming from
$MailMessage.To.add("yomomma@example.com")    #Change to email address you would like to receive emails.
$MailMessage.IsBodyHtml = 1
$MailMessage.Subject = "VMware Snapshots Report"
$MailMessage.Body = Generate-Report
$SmtpClient.Send($MailMessage)}

Download here:  http://vsential.com/wp-content/uploads/2011/05/Report-Snapshots.ps1_.txt

Script: Report-Datastores.ps1

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
@"
===============================================================================
Title:             Report-Datastores.ps1
Description:     Report Datastore usage for all Datastores managed by vCenter
Requirements:     Windows Powershell and the VI Toolkit
Usage:            .\Report-Datastores.ps1
===============================================================================
"
@

#Global Functions
#This function generates a nice HTML output that uses CSS for style formatting.
function Generate-Report {
Write-Output "<html><head><title></title><style type=""text/css"">.Error {color:#FF0000;font-weight: bold;}.Title {background: #0077D4;color: #FFFFFF;text-align:center;font-weight: bold;}.Normal-left {text-align:left;}.Normal {text-align:right;}</style></head><body><table><tr class=""Title""><td colspan=""4"">VMware Datastore Report</td></tr><tr><td>Datastore  </td><td>Capacity(GB)  </td><td>Used(GB)  </td><td>% Free  </td></tr>"

Foreach ($store in $report){
Write-Output "<td class=""Normal-left"">$($store.name)</td><td class=""Normal"">$($store.CapacityGB)</td><td class=""Normal"">$($store.UsedGB)</td><td class=""Normal"">$($store.PercFree)</td></tr> "
}
Write-Output "</table></body></html>"
}

#Login details
$username = 'changeme'
$password = 'supersecret'

#Current, Previous, Difference File information
$digits = 2
$Folder = 'C:\Reports'

#List of servers including Virtual Center Server.  The account this script will run as will need at least Read-Only access to Cirtual Center
$VIServer = "vc1.example.net"    #Chance to DNS Names/IP addresses of your ESXi servers or Virtual Center Server

#Initialise Array
$Report = @()

# Connect to Virtual Center
$VC = Connect-VIServer $VIServer -user $username -password $password

# Get all datastores and put them in alphabetical order
#$datastores = Get-Datastore | Sort-Object Name

#Get all datastores and put them in alphabetical order
foreach ($server in $serverlist){

# Check is server is a Virtual Center Server and connect with current user
if ($server -eq "VCServer"){Connect-VIServer $VIServer}

# Use specific login details for the rest of servers in $serverlist
else {Connect-VIServer $VIServer -user $username -password $password}

Get-Datastore | Sort-Object Name | %{
$Store = {} | Select Name, CapacityGB, UsedGB, PercFree
$Store.Name = $_.name
$Store.CapacityGB = [math]::Round($_.capacityMB/1024,$digits)
$Store.UsedGB = [math]::Round(($_.CapacityMB - $_.FreeSpaceMB)/1024,$digits)
$Store.PercFree = [math]::Round(100*$_.FreeSpaceMB/$_.CapacityMB,$digits)
$Report += $Store
}
# Disconnect from Virtual Center
Disconnect-VIServer -Confirm:$False
}

# Disconnect from Virtual Center
Disconnect-VIServer -Confirm:$False

# Generate the report and email it as a HTML body of an email
Generate-Report > "$Folder\Report-Datastore.html"
IF ($Report -ne ""){
$SmtpClient = New-Object system.net.mail.smtpClient
$SmtpClient.host = "my.smtp.host"   #Change to a SMTP server in your environment
$MailMessage = New-Object system.net.mail.mailmessage
$MailMessage.from = "System.Automation@example.com"   #Change to email address you want emails to be coming from
$MailMessage.To.add("yomomma@example.com")    #Change to email address you would like to receive emails.
$MailMessage.IsBodyHtml = 1
$MailMessage.Subject = "VMware Datastore Report"
$MailMessage.Body = Generate-Report
$SmtpClient.Send($MailMessage)}

Download here:  http://vsential.com/wp-content/uploads/2011/05/Report-Datastores.ps1_.txt

1 2  Scroll to top