Monday, September 19, 2016

Snapshot Report from Multiple VCenter - POWERCLI

<#
.SYNOPSIS
    Report snapshot status in multiple VCenter
.DESCRIPTION
    This script will send mail report with proper format,
    with details of snapshot from multiple VCenter
.NOTES
    File Name  : Get-SnapshotReport.ps1
Author     : Shankar Sahu
Requires   : PowerShell V3 + PowerCLI 5 or above
.HISTORY
    First published : 15.9.2016
Updated         : 19.9.2016 - validated for Verstion 2.
                               - added link references
.LINK
    This script posted to:
       http://scriptchef.blogspot.in/
#>


$htmlfile="D:\script\Status_Report.html"

$svr="10.1.0.4","10.1.0.3"

New-Item -ItemType file $htmlfile -Force

# CSS styling for report format and table - HTML

$htmlcss="<head>
    <title>Snapshot Status Report!</title>
<style>

body {
    width: 600px;
    margin: 40px auto;
    font-family: 'trebuchet MS', 'Lucida sans', Arial;
    font-size: 14px;
    color: #444;
}

table {
    *border-collapse: collapse; /* IE7 and lower */
    border-spacing: 0;
    width: 100%;  
}

.bordered {
    border: solid #ccc 1px;
    -moz-border-radius: 6px;
    -webkit-border-radius: 6px;
    border-radius: 6px;
    -webkit-box-shadow: 0 1px 1px #ccc;
    -moz-box-shadow: 0 1px 1px #ccc;
    box-shadow: 0 1px 1px #ccc;      
}

.bordered tr:hover {
    background: #fbf8e9;
    -o-transition: all 0.1s ease-in-out;
    -webkit-transition: all 0.1s ease-in-out;
    -moz-transition: all 0.1s ease-in-out;
    -ms-transition: all 0.1s ease-in-out;
    transition: all 0.1s ease-in-out;  
}  
 
.bordered td, .bordered th {
    border-left: 1px solid #ccc;
    border-top: 1px solid #ccc;
    padding: 10px;
    text-align: left;  
}

.bordered th {
    background-color: #dce9f9;
    background-image: -webkit-gradient(linear, left top, left bottom, from(#ebf3fc), to(#dce9f9));
    background-image: -webkit-linear-gradient(top, #ebf3fc, #dce9f9);
    background-image:    -moz-linear-gradient(top, #ebf3fc, #dce9f9);
    background-image:     -ms-linear-gradient(top, #ebf3fc, #dce9f9);
    background-image:      -o-linear-gradient(top, #ebf3fc, #dce9f9);
    background-image:         linear-gradient(top, #ebf3fc, #dce9f9);
    -webkit-box-shadow: 0 1px 0 rgba(255,255,255,.8) inset;
    -moz-box-shadow:0 1px 0 rgba(255,255,255,.8) inset;
    box-shadow: 0 1px 0 rgba(255,255,255,.8) inset;      
    border-top: none;
    text-shadow: 0 1px 0 rgba(255,255,255,.5);
}

.bordered td:first-child, .bordered th:first-child {
    border-left: none;
}

.bordered th:first-child {
    -moz-border-radius: 6px 0 0 0;
    -webkit-border-radius: 6px 0 0 0;
    border-radius: 6px 0 0 0;
}

.bordered th:last-child {
    -moz-border-radius: 0 6px 0 0;
    -webkit-border-radius: 0 6px 0 0;
    border-radius: 0 6px 0 0;
}

.bordered th:only-child{
    -moz-border-radius: 6px 6px 0 0;
    -webkit-border-radius: 6px 6px 0 0;
    border-radius: 6px 6px 0 0;
}

.bordered tr:last-child td:first-child {
    -moz-border-radius: 0 0 0 6px;
    -webkit-border-radius: 0 0 0 6px;
    border-radius: 0 0 0 6px;
}

.bordered tr:last-child td:last-child {
    -moz-border-radius: 0 0 6px 0;
    -webkit-border-radius: 0 0 6px 0;
    border-radius: 0 0 6px 0;
}
</style>
</head>
"
$counter=0
$date = ( get-date ).ToString('yyyy/MM/dd')
Function Header($File)
{

Add-content $File "<html>"
    Add-content $File $htmlcss
    Add-content $File "<body><h2>Snapshot Status Report</h2>"
    Add-content $File "<table class='bordered'>"
    Add-content $File " <thead>"
    Add-content $File "<tr>"
    Add-content $File "<th>VM</th>      
        <th>Name</th>
        <th>vCENTER</th>
        <th>Created_Date</th>
<th>SizeGB</th>
        <th>Description</th>
        </tr>
        </thead>"
}
Function Footer($File)
{
Add-Content $File "</table>"
Add-Content $File "</body>"
Add-Content $File "</html>"
}
Header $htmlfile
Function Connect($server)
{
      # $user=Read-Host -Prompt "Username"
      # $pass=Read-Host -Prompt "Password" -AsSecureString
      # $usercred=new-object system.management.automation.pscredential ($user,$pass)
      # Connect-VIserver -Server $server -credential $usercred
      # uncomment if you want to run it with different credential
      Connect-VIserver -Server $server   # Use vCenter admin credential to run script
}

Function Disconnect($svr)
{
Disconnect-VIServer $svr -Force -Confirm:$false
Write-host "Host is Disconnected"
}


foreach($s in $svr){
Connect($s)
Write-Host "=======VCenter:$s========"
   
        $Machine=Get-VM -Name * -Server $s | Get-snapshot | Select-object VM,Name,Created,Description,SizeGB -ErrorAction ignore
        foreach($info in $Machine){
                Add-Content $htmlfile "<tr>"
Add-Content $htmlfile "<td>$($info.VM)</td>"    
Add-Content $htmlfile "<td>$($info.Name)</td>"
Add-Content $htmlfile "<td>$($s)</td>"
Add-Content $htmlfile "<td>$($info.Created_Date)</td>"
                $size=[math]::round($($info.SizeGB),2)
Add-Content $htmlfile "<td>$($Size)</td>"
Add-Content $htmlfile "<td>$($info.Description)</td>"
Add-Content $htmlfile "</tr>"
            }
            Disconnect($s)
        }
Footer $htmlfile

Function Mailme
{
param($from,$to,$subject,$smtp,$flname)
$body = Get-Content $flname
$smtp= New-Object System.Net.Mail.SmtpClient $smtp
$msg = New-Object System.Net.Mail.MailMessage $from, $to, $subject, $body
$msg.isBodyhtml = $true
$smtp.send($msg)
}
Mailme adbadmin@shanky.com adbgroup@shanky.com "Snapshot Service Status - $Date" 10.1.0.67 $htmlfile

Windows Server 2016 Key Features and License Prospects

If you are planning to move to Windows 2016 and looking for benefits, I have listed down below : Why Windows Server 2016? ·     ...