November 22, 2014

Powershell storage analysis

So on a daily basis we have a report emailed to us with the current total storage and a percentage of that storage that is free.
It’s definitely easier than logging into servers and calculating the amount of storage on every mount point.

The powershell script works in 2 ways.
1) The powershell script itself (storagereport.ps1) and the server list (serverlist.txt) that it references. If you are adding or subtracting servers from the environment, just add or subtract them from the serverlist.txt and save the file in the same directory as the powershell script.

I suggest creating a folder on the server that this report will run from and putting in a text file with a list of servers, then referencing this list in your script.

Script (storagereport.ps1)
$TotalGB = @{Name="Capacity(GB)";expression={[math]::round(($_.Capacity/ 1073741824),2)}}
$FreeGB = @{Name="FreeSpace(GB)";expression={[math]::round(($_.FreeSpace / 1073741824),2)}}
$FreePerc = @{Name="Free(%)";expression={[math]::round(((($_.FreeSpace / 1073741824)/($_.Capacity / 1073741824)) *

100),0)}}

function get-mountpoints {
$volumes =Get-WmiObject -computer $server win32_volume | Where-object {$_.DriveLetter -eq $null -and $_.name -like

"*Indexvol*" }
$volumes | Select SystemName, Label, $TotalGB, $FreeGB, $FreePerc |ConvertTo-HTML -head $a | add-content

C:\Test.htm

}

remove-item C:\<user>\<ServerStorage>.htm
$servers = (Get-Content c:\<user>\serverlist.txt)

foreach ($server in $servers){
get-mountpoints
}
Invoke-Expression C:\<user>\<ServerStorage>.htm


# Send email as a HTML body.
$smtpServer = "smtp.<domain>.com"
$mailto = "<username>@<domain>.com,"

$msg = new-object Net.Mail.MailMessage  
$smtp = new-object Net.Mail.SmtpClient($smtpServer) 
$msg.From = $MailFrom
$msg.IsBodyHTML = $true
$msg.To.Add($Mailto) 
$msg.Subject = "My exchange Storage Report."
$MailTextT =  Get-Content  -Path C:\<user>\<ServerStorage>.htm
$msg.Body = $MailTextT
$smtp.Send($msg)



Server list (serverlist.txt)
server01
server02
server03
server04
server05
server06
server07
server08
server09
server10
server11
server12
server13
server14



As you add and remove servers from your environment, update the serverlist.txt to ensure it will appear on your report.

To add/remove recipients to the email report, amend the $mailto = section in the script to reflect your new addition (eg) user1@domain.com, user2@domain.com, user3@domain.com


# Send email as a HTML body.
$smtpServer = "smtp.<domain>.com"
$mailto = "<username>@<domain>.com,"


Once all have been configured. Create a scheduled task to run the report daily. This will automatically send the report out via email daily as a result.









No comments:

Post a Comment