Windows Server – Deduplication Clean Status Alert

Last updated on June 4th, 2019 at 11:04 am

Step 1 – Create batch file and save DedupClean_D.bat

@echo off
del C:\IT-Scripts\DedupClean\Get-DedupJob.txt
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""C:\IT-Scripts\DedupClean\DedupClean_D.ps1""' -Verb RunAs}"
wscript	"C:\IT-Scripts\DedupClean\DedupClean_D-Email-Alert.vbs"

Step 2 – Create Powershell file and save DedupClean_D.ps1

Start-DedupJob -Type GarbageCollection -Priority High -Volume D:
Get-DedupJob > C:\IT-Scripts\Get-DedupJob.txt

Step 3 – Create VB Script and save DedupClean_D-Email-Alert.vbs

Set objMail = CreateObject("CDO.Message")
Set objConf = CreateObject("CDO.Configuration")
Set objFlds = objConf.Fields
objFlds.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 'cdoSendUsingPort
objFlds.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "your_smtp_server" 'your smtp server domain or IP address goes here
objFlds.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 'default port for email
'uncomment next three lines if you need to use SMTP Authorization
'objFlds.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "your-username"
'objFlds.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "your-password"
'objFlds.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1 'cdoBasic
objFlds.Update
objMail.Configuration = objConf
strComputer = "." 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 
' C drive is specified at Device ID
Set colItems = objWMIService.ExecQuery( _
    "SELECT * FROM Win32_LogicalDisk where DeviceID='d:'",,48) 
' Preset value for sending email using AUTH LOGIN
cdoBasic = 1
For Each objItem in colItems
   if len(objItem.VolumeName) > 0 then
      freeSpace = FormatNumber((CDbl(objItem.FreeSpace)/1024/1024/1024))
      diskSize = FormatNumber((CDbl(objItem.Size)/1024/1024/1024))
      usedSpace = diskSize - freeSpace
      percentage = FormatNumber((CDbl(objItem.FreeSpace)/1024/1024/1024)) / _
         FormatNumber((CDbl(objItem.Size)/1024/1024/1024)) 
      diskDescription = "-----------------------------------" & vbCrLf _
           & "VolumeName: " & vbTab & objItem.VolumeName  & vbCrLf _
           & "-----------------------------------" & vbCrLf _
           & "Free Space: " & vbTab & freeSpace & " GB" & vbCrLf _
           & "Total Size: " & vbTab & diskSize & " GB" & vbCrLf _
           & "Occupied Space: " & usedSpace & " GB" & vbTab
      'Wscript.Echo diskDescription, FormatPercent(pctpercentage)
	      if percentage < 100 then
objMail.From = "[email protected]"
objMail.To = "[email protected]"
Set WshNetwork = WScript.CreateObject("WScript.Network")
objMail.Subject = "Dedup Clean completed on " & WshNetwork.ComputerName & "  |  Free Space Disk D: " & FormatPercent(percentage) & ""
'*******************************************************
'** Setting a text file to be shown in the email Body **
'*******************************************************
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
'** File to be inserted in Body
Const FileToBeUsed = "C:\IT-Scripts\DedupClean\Get-DedupJob.txt"
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
'** Open the file for reading
Set f = fso.OpenTextFile(FileToBeUsed, ForReading)
'** The ReadAll method reads the entire file into the variable BodyText
objMail.Textbody = diskDescription & vbCRLF & f.ReadAll 
'** Close the file
f.Close
'** Clear variables
Set f = Nothing
Set fso = Nothing
'* cdoSendUsingPickup (1)
'* cdoSendUsingPort (2)
'* cdoSendUsingExchange (3)
objMail.Send
end if
end if
Next
Set objFlds = Nothing
Set objConf = Nothing
Set objMail = Nothing

Step 4 – Open Task Scheduler and add new task in quiet hours.

Step 5 – On the task in action -> set to run ‘DedupClean_D.bat’

Step 6 – check Run with highest privileges.

Enjoy 🙂