VMware Cloud Community
breakstuf50
Enthusiast
Enthusiast
Jump to solution

Need assistance sorting my output snapshot list by name, in email and output file.

Hi Experts,

i'm not very good at this can someone help me sort my output file and email body snapshot list by name? 

 

Thanks in advance,

breakstuf50

$server = "xxxxx"
$logincred = Get-VICredentialStoreItem -Host $server -File E:\snapshot_files\creds\atl4\Credential.xml
$SMTPserver = "xxxx"
$from = "xxxx"
$to = "xxxx"
$d = get-date -uformat '%m-%d-%Y-%H%M%S'
$filename = "E:\xxxx\xxxx\xxx\xxxx_" + $d + ".html"

Connect-VIServer -Server $server -Protocol https -User $logincred.User -Password $logincred.Password -WarningAction SilentlyContinue | Out-Null

Function Set-AlternatingRows {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True,ValueFromPipeline=$True)]
[object[]]$HTMLDocument,

[Parameter(Mandatory=$True)]
[string]$CSSEvenClass,

[Parameter(Mandatory=$True)]
[string]$CSSOddClass
)
Begin {
$ClassName = $CSSEvenClass
}
Process {
[string]$Line = $HTMLDocument
$Line = $Line.Replace("<tr>","<tr class=""$ClassName"">")
If ($ClassName -eq $CSSEvenClass)
{ $ClassName = $CSSOddClass
}
Else
{ $ClassName = $CSSEvenClass
}
$Line = $Line.Replace("<table>","<table width=""50%"">")
Return $Line
}
}

Function Get-SnapshotCreator {
Param (
[string]$VM,
[datetime]$Created
)

(Get-VIEvent -Entity $VM -Types Info -Start $Created.AddSeconds(-10) -Finish $Created.AddSeconds(10) | Where FullFormattedMessage -eq "Task: Create virtual machine snapshot" | Select -ExpandProperty UserName).Split("\")[-1]
}

$Header = @"
<style>
TABLE {border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
TR:Hover TD {Background-Color: #C1D5F8;}
TH {border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color: #e41d8d;}
TD {border-width: 1px;padding: 3px;border-style: solid;border-color: black;}
.odd { background-color:#ffffff; }
.even { background-color:#dddddd; }
</style>
<title>
Snapshot Report - $server - $d
</title>
"@

$Report = Get-VM |
Get-Snapshot |
Select VM,
Name,
Description,
@{Name="SizeGB";Expression={ [math]::Round($_.SizeGB,2) }},
@{Name="Creator";Expression={ Get-SnapshotCreator -VM $_.VM -Created $_.Created }},
Created,
@{Name="Days Old";Expression={ (New-TimeSpan -End (Get-Date) -Start $_.Created).Days }}

If (-not $Report)
{ $Report = [PSCustomObject]@{
VM = "No snapshots found on any VM's controlled by $server"
Name = ""
Description = ""
Size = ""
Creator = ""
Created = ""
'Days Old' = ""
}
}

$Report = $Report |
ConvertTo-Html -Head $Header -PreContent "<p><h2>Snapshot Report - $server - $d</h2></p><br>" |
Set-AlternatingRows -CSSEvenClass even -CSSOddClass odd
$Report | Out-File $filename


# Send email
$subject = "vCenter $server weekly Snapshot Report "
$emailbody = $Report
#if( $snapshotCount -gt 0 )
#{
 #$emailbody = $Report
#}
$mailer = New-Object Net.Mail.SMTPclient( $SMTPserver)
$msg = New-Object Net.Mail.MailMessage( $from, $to , $subject , $emailbody )
$msg.IsBodyHTML = $true
$mailer.send($msg )
Disconnect-VIServer -Server $server -Force -Confirm:$false
Remove-Variable * -ErrorAction SilentlyContinue

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You could do something like this with the Sort-Object cmdlet.

$Report = Get-VM |
    Get-Snapshot |
    select VM,
    Name,
    Description,
    @{Name = "SizeGB"; Expression = { [math]::Round($_.SizeGB, 2) } },
    @{Name = "Creator"; Expression = { Get-SnapshotCreator -VM $_.VM -Created $_.Created } },
    Created,
    @{Name = "Days Old"; Expression = { (New-TimeSpan -End (Get-Date) -Start $_.Created).Days } } |
Sort-Object -Property Name


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

You could do something like this with the Sort-Object cmdlet.

$Report = Get-VM |
    Get-Snapshot |
    select VM,
    Name,
    Description,
    @{Name = "SizeGB"; Expression = { [math]::Round($_.SizeGB, 2) } },
    @{Name = "Creator"; Expression = { Get-SnapshotCreator -VM $_.VM -Created $_.Created } },
    Created,
    @{Name = "Days Old"; Expression = { (New-TimeSpan -End (Get-Date) -Start $_.Created).Days } } |
Sort-Object -Property Name


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
breakstuf50
Enthusiast
Enthusiast
Jump to solution

Thanks Luc it worked.

Reply
0 Kudos