Partilhar via


Powershell-Generate Automated CSVFreespace and Memory Report

This script relies the Hyper-V clusters are been managed by VMM but if you do not use VMM you can also modify based on your environment. It should just give you an idea how to collect data and use sentmail function to get a daily report of your resource status.

Note: script version 1.3 is targeted to be run as a task so no console outputs are considered, just a logfile is written into same folder where is script is located

as no user interaction is possible because we want to use this as a task following variables has to be updated in the script

$VMMServer = "YOURVMMSERVER"
$smtpServer = "YOURSMTP"
$smtpFrom = "johndoe@mydomain.com"
$SMTPPort = "25"
$Username = "johndoe@mydomain.com"
$Password = 'YOURPASSWORD'
$smtpTo = reports@mydomain.com

 
#
#Creator: Ramazan Can
#V1.2   - dumping into log and generating mail with function "SentReportviaMail"
#       - mail sender, recipient, account for authentification can be modified in function "SentReportviaMail"
#               $smtpFrom - $Username - $smtpTo 
#V1.3   - task scheduler version

#Write-Host " "
import-module virtualmachinemanager
import-module failoverclusters
$VMMServer = "YOURVMMSERVER"
$VMMClusters=(Get-SCVMHostCluster -vmmserver $VMMServer).Name
$timestamp=(get-date -Format d).Replace("/","_")
$date=Get-date
Get-item ".\CSVandMemory_Report_$timestamp.txt" -ea 0 | Remove-Item -ea 0
$logfile=".\CSVandMemory_Report_$timestamp.txt"

"This Report was run at $date " | out-file -filepath $logfile -append
" " | out-file -filepath $logfile -append

function GetCSVFreeSpace {
#incorporated and modified from https://blogs.msdn.com/b/clustering/archive/2010/06/19/10027366.aspx
$objs = @()

$csvs = Get-ClusterSharedVolume -Cluster $Cluster
foreach ( $csv in $csvs )
{
   $csvinfos = $csv | select -Property Name -ExpandProperty SharedVolumeInfo
   foreach ( $csvinfo in $csvinfos )
   {
      $obj = New-Object PSObject -Property @{
         Name        = $csv.Name
         Path        = $csvinfo.FriendlyVolumeName
         Size        = $csvinfo.Partition.Size
         FreeSpace   = $csvinfo.Partition.FreeSpace
         UsedSpace   = $csvinfo.Partition.UsedSpace
         PercentFree = $csvinfo.Partition.PercentFree
      }
      $objs += $obj
   }
}

$objs | ft -auto Name,Path,@{ Label = "Size(GB)" ; Expression = { "{0:N2}" -f ($_.Size/1024/1024/1024) } },@{ Label = "FreeSpace(GB)" ; Expression = { "{0:N2}" -f ($_.FreeSpace/1024/1024/1024) } },@{ Label = "UsedSpace(GB)" ; Expression = { "{0:N2}" -f ($_.UsedSpace/1024/1024/1024) } },@{ Label = "PercentFree" ; Expression = { "{0:N2}" -f ($_.PercentFree) }}
}

function SentReportviaMail {
$logfile=(Get-item ".\CSVandMemory_Report*" -ea 0).Name
$Logs=Get-Content $logfile
$smtpServer = "YOURSMTP"
$smtpFrom = "johndoe@mydomain.com"
$SMTPPort = "25"
$Username = "johndoe@mydomain.com"
$Password = 'YOURPASSWORD'
$smtpTo = "reports@mydomain.com"
$messageSubject = "$VMMServer - Automated CSV FreeSpace and Total Memory Report"

[string]$messagebody = ""

foreach ($log in $logs )
{
    $messagebody = $messagebody + $log + "`r`n"
}
#Write-Host " "
#Write-Host "Starting to sent mail to $smtpTo via $smtpServer ...." -ForegroundColor green
$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);
$smtp.Send($smtpFrom,$smtpTo,$messagesubject,$messagebody)
#Write-Host "mail sent completed " -ForegroundColor green
#Write-Host " "
}


foreach ($Cluster in $VMMClusters)
{
    #Write-Host "Starting to collect memory and CSV free space data in $Cluster ....." -foregroundcolor green
    [int]$TotalFreeMemory = 0;
    [int]$TotalMemory = 0;
    $ClusterNodes = Get-Cluster $Cluster | Get-ClusterNode
    foreach ($ClusterNode in $ClusterNodes)
    {
        [int]$FreeMemory = [math]::round(((Get-WmiObject -ComputerName $ClusterNode -Class Win32_OperatingSystem).FreePhysicalMemory / 1MB), 0)
        [int]$TotalFreeMemory = [int]$TotalFreeMemory + [int]$FreeMemory
        [int]$NodeMemory = [math]::round(((Get-WmiObject -ComputerName $ClusterNode -Class Win32_OperatingSystem).TotalVisibleMemorySize / 1MB), 0)
        [int]$TotalMemory = [int]$TotalMemory + [int]$NodeMemory

    }

    $TotalAvailableMemory = $TotalFreeMemory - $NodeMemory
 
    "Cluster: $Cluster" | out-file -filepath $logfile -append
    "Total Memory: $TotalMemory" | out-file -filepath $logfile -append
    "Total Free Memory: $TotalFreeMemory" | out-file -filepath $logfile -append
    "Total Available Memory: $TotalAvailableMemory" | out-file -filepath $logfile -append
    " " | out-file -filepath $logfile -append
    "CSV Freespace : " | out-file -filepath $logfile -append
    GetCSVFreeSpace | out-file -filepath $logfile -append
    " " | out-file -filepath $logfile -append
}
SentReportviaMail

example output mail:

Disclaimer: Please read, understand and test script before you run put in production! This should just give you an idea around the power of powershell and automation