Mark Minasi's Tech Forum
Sign up Calendar Latest Topics
 
 
 


Reply
  Author   Comment  
martinmcd

Avatar / Picture

Still looking for DenverCoder9
Registered:
Posts: 17
Reply with quote  #1 
I regularly check available free space on drives and mount points for my Exchange servers, and am working on automating the process. Most of these would be for Exchange 2010 on Server 2008. 

Here is what I have so far: 

# ServerName
get-wmiobject -query "select name,driveletter,freespace,capacity,blocksize from win32_volume where ((drivetype=3) OR (drivetype=3 AND driveletter=NULL))" `
-computer ServerName.domain.com | % {$_.BlockSize=(($_.FreeSpace)/($_.Capacity))*100;$_.FreeSpace=($_.FreeSpace/1GB);$_.Capacity=($_.Capacity/1GB);$_} | Format-Table @{n='Name (ServerName)';e={$_.name}},@{n='Capacity GB';e={'{0:N3}' -f $_.Capacity}}, @{n='Free Space GB';e={'{0:N2}'-f $_.FreeSpace}}, @{n='% Free';e={'{0:N2}'-f $_.BlockSize}} -auto

It outputs this (in columns): 

Name (ServerName) Capacity GB Free Space GB % Free
----------------- ----------- ------------- ------
\\?\Volume{77b9cd21-e270-11e2-b113-806e6f6e6963}\ 0.000 0.00 72.00
D:\ 100.000 72.00 72.00
P:\ 40.000 8.00 19.00
D:\MountPoints\ExchDB27-lun\ 980.000 980.00 100.00
D:\LogMountPoints\Log_ExchDB27-lun\ 300.000 300.00 100.00
D:\MountPoints\ExchDB30-lun\ 980.000 980.00 100.00
D:\LogMountPoints\Log_ExchDB31-lun\ 300.000 300.00 100.00
D:\MountPoints\ExchDB33-lun\ 980.000 980.00 100.00
D:\LogMountPoints\Log_ExchDB34-lun\ 300.000 300.00 100.00
D:\MountPoints\ExchDB39-lun\ 980.000 980.00 100.00
D:\LogMountPoints\Log_ExchDB39-lun\ 300.000 300.00 100.00
D:\MountPoints\ExchDB42-lun\ 980.000 980.00 100.00
D:\LogMountPoints\Log_ExchDB42-lun\ 300.000 300.00 100.00
D:\MountPoints\ExchDB45-lun\ 980.000 980.00 100.00
D:\LogMountPoints\Log_ExchDB45-lun\ 300.000 300.00 100.00
C:\ 60.000 24.00 40.00

My goals are:
  1. To get it to run sequentially for a set of servers; hopefully from Get-ExchangeServer. 
  2. Figure out how to get rid of the "\\?\Volume..." line. 
  3. Output to csv. 
  4. Email the results. 
I'll post updates if I make progress, but any advice would be appreciated.

__________________
https://xkcd.com/979/
0
wobble_wobble

Avatar / Picture

Associate Troublemaker Apprentice
Registered:
Posts: 932
Reply with quote  #2 
This in not neat, bits of different scripts pulled together from other people.
The final output is then emailed.

Might help point you in a direction until the Powershell people stop drinking in Seattle.
This pulls the space from a CSV but you should be able to modify that.
The ClusterName is in my case a hyper-v cluster name
SpaceThreshold is the % we are alerting on.


param
(
[string]$ClusterName = $("HyperV"),
[int]$SpaceThreshold = $("7.6")
)
Import-Module FailoverClusters
$objs = @()
$alert = 0
$csvs = Get-ClusterSharedVolume -Cluster $ClusterName
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
      }
         if ($obj.PercentFree -le $SpaceThreshold) {$alert++}
      $objs += $obj
   }
}
Write-Host "Cluster: " $ClusterName
$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) } } #| sort-object PercentFree
$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) } } >c:\scripts\output\csvspace.txt
$objs | ft -auto Name,@{ Label = "PercentFree" ; Expression = { "{0:N2}" -f ($_.PercentFree) } } >>c:\scripts\output\csvspace.txt
#If ($alert -gt 0){Write-Host "WARNING: One or more drives have exceeded the threshold value of" $SpaceThreshold "% remaining!"}
#return $alert
#Connection Details
$username=”Jmcglynn@bongo.com”
$password=”xxxxlcom4”
$smtpServer = “172.16.1.13”
$msg = new-object Net.Mail.MailMessage
#Change port number for SSL to 587
$smtp = New-Object Net.Mail.SmtpClient($SmtpServer, 25) 
#Uncomment Next line for SSL  
#$smtp.EnableSsl = $true
# Credentials not needed
#$smtp.Credentials = New-Object System.Net.NetworkCredential( $username, $password )
#From Address
$msg.From = "Hyper-V2@bongo.com"
#To Address, Copy the below line for multiple recipients
$msg.To.Add(“jmcglynn@mjf.ie”)
$msg.To.Add(“jmcglynn@mjf.ie”)
#Message Body
$msg.Body=”CSV Free Space”
#Message Subject
$msg.Subject = “Email with Attachments - Check to make sure free space is more than 7%”
#your file location
$files=Get-ChildItem “C:\scripts\Output”
Foreach($file in $files)
{
Write-Host “Attaching File :- ” $file
$attachment = New-Object System.Net.Mail.Attachment –ArgumentList C:\scripts\Output\$file
$msg.Attachments.Add($attachment)
}
$smtp.Send($msg)
$attachment.Dispose();
$msg.Dispose();


__________________
Have you tried turning it off and walking away? The next person can fix it!

New to the forum? Read this
0
cj_berlin

Avatar / Picture

Senior Member
Registered:
Posts: 406
Reply with quote  #3 
Here you go. This is a quick and dirty one, pulled from an infinitely more complex state monitoring script I created for a client last year:


$machines = (Get-ExchangeServer | sort Name).Name
$outfile = "$($env:TEMP)\exsvrdrives.csv"
$smtp_relay = "mailserver.mydomain.com"
$smtp_from = "exchange.monitoring@mydomain.com"
$smtp_to = @("admin1@mydomain.com","admin2@mydomain.com","admin3@mydomain.com")
$smtp_subject = "Exchange disk report"

"ServerName,DriveName,Capacity,FreeSpace,BlockSize" | Out-File $outfile -Force

foreach ($machine in $machines) {
    $drives = Get-WmiObject -Query "select name,freespace,capacity,blocksize from win32_volume where drivetype=3" -ComputerName $machine | where {$_.Name -notlike "\\?*"} | sort Name 
    foreach ($drive in $drives) {
        "`"$machine`",`"$($drive.Name)`",$($drive.Capacity),$($drive.FreeSpace),$($drive.BlockSize)" | Out-File $outfile -Append
    }
}

Send-MailMessage -To $smtp_to -From $smtp_from -Subject $smtp_subject -SmtpServer $smtp_relay -Attachments $outfile -Body $smtp_body


I omitted the GB and percentage calculations for the sake of brevity, but basically you can evaluate any expression within a $() within a string.

If the machine you would be running the script from hasn't got Exchange management tools installed you'd need a remoting session to one of the Exchange servers to get the machine names.

You could pull this off with a two-liner, I think, but it would be hardly human-readable and much less debuggable.

FWIW,

__________________
Evgenij Smirnov

My personal blog (German): http://www.it-pro-berlin.de/
My stuff on PSGallery: https://www.powershellgallery.com/profiles/it-pro-berlin.de/
0
martinmcd

Avatar / Picture

Still looking for DenverCoder9
Registered:
Posts: 17
Reply with quote  #4 
Thank you both. Those give me lots to work with. 
__________________
https://xkcd.com/979/
0
Previous Topic | Next Topic
Print
Reply

Quick Navigation:

Easily create a Forum Website with Website Toolbox.