Author Posts

May 6, 2014 at 12:48 am

Hello, I have made script for deleting old folder(log) from one drive.After deleting files, I need to check the current drive size and senddetails in HTMl format though mail. But somehow i am not able to figure thing out. Output in HTml is not correect. I am pasting my code .Please help me rectify the error.

I am new to powershell scripting, teaching myself from here and there.

May 6, 2014 at 3:22 am

You haven't posted your code yet. However, if you click on Resources -> Free eBooks on this site, you'll find one called "Creating HTML Reports in PowerShell" which has some good info on this topic, as well as an EnhancedHTML2 module that you can use to do quite a bit more than the built-in ConvertTo-Html cmdlet.

May 6, 2014 at 6:50 am

Hello Dave,

Thanks for the reply.I have attached my code in attachemnt . I am not sure if that works here. This is first post here. I am attaching here again.
If that dont work , pasting code here directly.

##############Code#######################

## script to delete old logs##

#—– define parameters —–#

#...................................
# Email Settings
#...................................

$smtpsettings = @{
To = "123@xyz.com"
From = "234@xyz.com"
Subject = "Blackberry Server DriveSpace Alert "
SmtpServer = "smtpserver.inter.rsag.site"
}

[int]$foldersize = 0

#—– define function —–#

function Get-Size
{
param([string]$path)
"{0:n2}" -f ((gci -path $path -recurse | measure-object -property length -sum).sum /1mb)
}

#—– get current date —-#
$Now = Get-Date

#—– define amount of days —-#
$Days = "31"

#—– define folder where files are located —-#
$TargetFolder = 'D:\Program Files (x86)\Research In Motion\BlackBerry Enterprise Server\Logs'

#—– define extension —-#
$Extension = "*.log"

#—– define LastWriteTime parameter based on $Days —#
$LastWrite = ($Now.AddDays(-$Days)).tostring()

##Get all folders from the $TargetFolder and apply the $LastWrite filter##

$Folders = Get-Childitem $TargetFolder | ?{ $_.PSIsContainer } | Where {($_.LastWriteTime -le "$LastWrite")}

##calculating Size of folders which will be deleted##

Foreach ($Folder in $Folders)
{
if ($folder -ne $NULL)
{
write-host "calculating Folder size of $Folder" -ForegroundColor "yellow"
$foldersize += get-size D:\"Program Files (x86)"\"Research In Motion"\"BlackBerry Enterprise Server"\Logs\$folder
}
else
{
Write-Host "Empty Folder " -foregroundcolor "Orange"
}
}

write-host "Total foldersize that will be deleted :$foldersize MB"

##Deleting folder one by one##

foreach ($Folder in $Folders)
{
if ($folder -ne $NULL)
{
write-host "Deleting Folder $Folder" -ForegroundColor "DarkRed"
Remove-Item $Folder.FullName | out-null
}
else
{
Write-Host "No more folder to delete!" -foregroundcolor "Green"
}
}

## Getting total free space on all drive of server##

$PresentSize = gwmi win32_volume -Filter 'drivetype = 3' | select driveletter,freespace,capacity

###############################################################

##Converting disk space output to HTMl table##
$tableHtml = $null

#Begin Summary table HTML header
$header= "

Disk Name Free Space Total Disk Capacity

"

$tablehtml+= $header

#Begin rows of table##

$tablehtml+=

foreach($u in $presentsize)
{
$tablehtml += "

"

$tablehtml+=

"$_.driveletter" "$_.freespace" "$_.capacity"

}

##Sending Notification mail to Team##

$body = "Hello Team,

Logs older than $LastWrite in path $TargetFolder are deleted.

Total foldersize deleted is: $foldersize MB.

Now free space in disks of server blackberry server are as follows:

$tablehtml

Thanks,

Hasin Choudhary

."

Send-MailMessage @smtpsettings -Body $body -bodyasHtml -Encoding ([System.Text.Encoding]::UTF8)

#########################Code complete#####################################################################

May 6, 2014 at 9:22 am

You have to rename .PS1 attachments to .TXT to upload them here, but I can work with what you've posted into the thread. I had to replace a bunch of smart quotes with normal single / double quotation marks to get the code to display properly in the ISE (and most of those probably came from the forum software), but once I did that, this part of the code jumps out as having a few problems:

##Converting disk space output to HTMl table##
$tableHtml = $null
 
#Begin Summary table HTML header
$header= " 
Disk Name Free Space Total Disk Capacity
" $tablehtml+= $header #Begin rows of table## foreach($u in $presentsize) { $tablehtml += "
"$_.driveletter" "$_.freespace" "$_.capacity"