PowerShell: Virtual Machine Snapshots Report from VMM Servers

# vmSnapshotReport.ps1
# Requirements:
# - Credentials to access VMM Servers with Administrator role
# - Firewall open WinRM ports from jump host to VMM Servers

# Variables
$vmmServers=@(
    @{servername='LAX-VMM01.kimconnect.com';adminname='intranet\clusterReport';password=$env:kimconnectcom}
    @{servername='PHX-VMM01.kimconnect.net';adminname='intranet\clusterReport';password=$env:kimconnectnet}
)

# Email relay parameters
$emailFrom='admins@kimconnect.com'
$emailTo='admins@kimconnect.com'
$subject='Hyper-V Snapshots Report'
$smtpRelayServer='email-relay.kimconnect.com'
$emailDay='Monday'
$css="
    <style>
    .h1 {
        font-size: 18px;
        height: 40px;
        padding-top: 80px;
        margin: auto;
        text-align: center;
    }
    .h5 {
        font-size: 22px;
        text-align: center;
    }
    .th {text-align: center;}
    .table {
        padding:7px;
        border:#4e95f4 1px solid;
        background-color: white;
        margin-left: auto;
        margin-right: auto;
        width: 100%
        }
    .colgroup {}
    .th { background: #0046c3; color: #fff; padding: 5px 10px; }
    .td { font-size: 11px; padding: 5px 20px; color: #000;
            width: 1px;
            white-space: pre;
        }
    .tr { background: #b8d1f3;}
    .tr:nth-child(even) {
        background: #dae5f4;
        width: 1%;
        white-space: nowrap
    }
    .tr:nth-child(odd) {
        background: #b8d1f3;
        width: 1%;
        white-space: nowrap
    }
    </style>
"

$results=$null
foreach($vmmServer in $vmmServers){
    $servername=$vmmServer.servername
    $username=$vmmServer.adminName
    $password=convertto-securestring $vmmServer.password -AsPlainText -Force
    $credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username,$password
    $pssession=new-pssession -computername $servername -credential $credential
    if($pssession.State -eq 'Opened'){
        $results+=invoke-command -session $pssession {Get-SCVMCheckpoint -vmmserver localhost}
        remove-pssession $pssession
    }else{
        write-warning "Unable to connect to $servername"
        break
    }
}
$report=$results|select @{name='VMMServer';e={$_.PSComputername}},VM,Name
if($null -ne $report){
    $currentReport=$report | ConvertTo-Html -Fragment | Out-String
    $currentReportHtml=$currentReport -replace '\<(?<item>\w+)\>', '<${item} class=''${item}''>'
    $emailContent='<html><head>'+$css+"</head><body><h5 class='h5'>$subject</h5>"+$currentReportHtml+'</body></html>'
    write-host "####################################################################################################`r`nVM Snapshots Report:`r`n####################################################################################################`r`n$(($report|out-string).trim())"
    $today=(get-date).DayOfWeek
    if ($today -eq $emailDay){
        Send-MailMessage -From $emailFrom `
        -To $emailTo `
        -Subject $subject `
        -Body $emailContent `
        -BodyAsHtml `
        -SmtpServer $smtpRelayServer
        write-host "$subject email has been sent to $emailTo with time stamp $((get-date|out-string).trim())"
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *