VirtualCenter

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

Datastore Size Reporting via PowerCLI Script

After pulling together some different resources from the likes of Alan Renouf, Luc Dekens, Eric Sloof, and others in the PowerCLI community (Forgive me if I didn’t mention all but I read through a lot of different people’s posts!), I have pieced together something that I use regularly to report datastore sizes and amounts of free space.  All sent to you in a nice HTML e-mail.


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)}

vSMP VMs and Per CPU Licensed Applications

I have the handy vCalendar by Jason Boche sitting on my desk here at work and as I was tearing through the days of this weekend I came across one tidbit that is extremely handy to know.

August 28th talks about vSMP VMs and Applications which license per CPU.  Jason has a great tip that allows the application to see logical CPUs instead of Physical CPUs:

  1. Power off the VM & Edit the VM Settings
  2. Options tab
  3. General (in the Advanced options section)
  4. Configuration Parameters
  5. Add Row
  6. Enter “cpuid.coresPerSocket” in the Name column
  7. Enter a value of 2 or 4 in the Value column

Figured that this would be nice to share with everyone!

Sourced from:  vCalendar by Jason Boche

Scripts: Export VM Name and VMDK Size

Here is a script that I use to gather some size data and spit out a spreadsheet listing name and VMDK size.

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
#
# PowerCLI Script to gather VM Information (Name, VMDK Size)
# Scripted by James Bowling <jbowling @vsential.com>
#

#Global Functions
function Get-VmSize($vm)
{
#Initialize variables
$VmDirs =@()
$VmSize = 0

$searchSpec = New-Object VMware.Vim.HostDatastoreBrowserSearchSpec
$searchSpec.details = New-Object VMware.Vim.FileQueryFlags
$searchSpec.details.fileSize = $TRUE

Get-View -VIObject $vm | % {
#Create an array with the vm's directories
$VmDirs += $_.Config.Files.VmPathName.split("/")[0]
$VmDirs += $_.Config.Files.SnapshotDirectory.split("/")[0]
$VmDirs += $_.Config.Files.SuspendDirectory.split("/")[0]
$VmDirs += $_.Config.Files.LogDirectory.split("/")[0]
#Add directories of the vm's virtual disk files
foreach ($disk in $_.Layout.Disk) {
foreach ($diskfile in $disk.diskfile){
$VmDirs += $diskfile.split("/")[0]
}
}
#Only take unique array items
$VmDirs = $VmDirs | Sort | Get-Unique

foreach ($dir in $VmDirs){
$ds = Get-Datastore ($dir.split("[")[1]).split("]")[0]
$dsb = Get-View (($ds | get-view).Browser)
$taskMoRef  = $dsb.SearchDatastoreSubFolders_Task($dir,$searchSpec)
$task = Get-View $taskMoRef

while($task.Info.State -eq "running" -or $task.Info.State -eq "queued"){$task = Get-View $taskMoRef }
foreach ($result in $task.Info.Result){
foreach ($file in $result.File){
$VmSize += $file.FileSize
}
}
}
}

return $VmSize
}

# Variables
$VIServer = "VISERVER"
$username = "changeme"
$password = "supersecure"
$Folder = "C:"

# Connect to VIServer
Connect-VIServer $VIServer -user $username -Password $password

#Initialise Array
$Report = @()

# Gather VM Information
$AllVMs = Get-View -ViewType VirtualMachine | Where {-not $_.Config.Template}
$SortedVMs = $AllVMs | Select *, @{N="NumDisks";E={@($_.Guest.Disk.Length)}} | Sort-Object -Descending NumDisks
ForEach ($VM in $SortedVMs){
$Details = New-object PSObject
$Details | Add-Member -Name Name -Value $VM.name -Membertype NoteProperty
$DiskNum = 0
Foreach ($disk in $VM.Guest.Disk){
$Details | Add-Member -Name "Disk$($DiskNum)Capacity(GB)" -MemberType NoteProperty -Value ([math]::Round($disk.Capacity/ 1024MB))
$DiskNum++
}
$Report += $Details
}

$Report | Export-Csv "C:VMDiskSize.csv"

# Disconnect from VIServer
Disconnect-VIServer $VIServer -Confirm:$False
 Scroll to top