Wednesday, February 13, 2013

Virtual Platform Change history

Following script can be used to find the latest changes to the virtual platform and a mail will be sent to the admin ID . 


## Change history 
## HTML Configuration 
$a = ”<style>“
$a += ”body { background-color:#EEEEEE; }”
$a += ”body,table,td,th { font-family:Tahoma; color:Black; Font-Size:10pt }”
$a += ”th { font-weight:bold; background-color:#CCCCCC; }”
$a += ”td { background-color:white; }”
$a += "</style>"


Connect-VIServer VcServer -User admin -Password pwd

$date=Get-Date
$Folder= "c:\Temp"
$FileName= "$Folder\Changehistory{0}{1:d2}{2:d2}.html" -f $date.Year,$date.Month,$date.Day

$Report= @()
ConvertTo-Html –title "ChangeHistory " –body "<H4>Date and time</H4>",$date -head $a  | Out-File $FileName

$Report= Get-VIEvent -maxsamples 20000 | where {$_.Gettype().Name -eq "VmRemovedEvent"} | Sort CreatedTime -Descending | Select CreatedTime, UserName, FullformattedMessage -First 20
$report | ConvertTo-Html -head $a -Body "<H4> VM guests Removed  - last 7 days  </h4>" | Out-File -append $FileName

$Report = Get-VIEvent -MaxSamples 20000 | where {$_.gettype().Name -eq "VmCreatedEvent" } |sort CreatedTime -Descending |select createdTime , Username , FullformattedMessage  -First 20
$report | ConvertTo-Html -head $a -Body "<H4> VM guests created  - last 7 days  </h4>" | Out-File -append  $FileName

$Report= Get-VIEvent -MaxSamples 20000 | where {$_.gettype().Name -eq "VmbeingClonedEvent" } | Sort createdTime -Descending |select createdtime  , username , FullformattedMessage  -first 20
$report | ConvertTo-Html -head $a -Body "<H4> VM cloned Last 7 days</h4>" | Out-File -append  $FileName


$Report= Get-VM | Get-Snapshot | Select-Object vm , SizeMB
$report | ConvertTo-Html -head $a -Body "<H4> SnapShot Details</h4>" | Out-File -append  $FileName

$Report= Get-Datastore | Select-Object name , CapacityMB , FreeSpaceMB 
$report | ConvertTo-Html -head $a -Body "<H4> DataStore details </h4>" | Out-File -append  $FileName

## Email the report 

function SendMail{
$sender = "Recipient@domain.com"
$recipient = "Recipient@domain.com"
$CC = "CCRecipient@domain.com"
$BCC = "BCCRecipient@domain.com"
$mailserver = ""
$subject = "Change History "
 
$body = "Report Attached"
 
$msg = new-object System.Net.Mail.MailMessage $sender, $recipient, $subject, $body
$attachment = new-object System.Net.Mail.Attachment $FileName
$msg.CC.Add($CC)
Uncomment to send BCC
# $msg.BCC.Add($BCC)
$msg.Attachments.Add($attachment)
$client = new-object System.Net.Mail.SmtpClient $mailserver
$client.Send($msg)
$attachment.Dispose()}
SendMail

Disconnect-VIServer Vcserver -Confirm:$false



No comments:

Post a Comment