1

First of all the background of my question:

Sometimes a used application of us is running into a loop in logging, which causes really big sized log-files (rising by 30 MBs per second). As we don't have the access to the full source of the application we cannot fix the issue ourself. We manually have to turn of the application service for removing the log file and start the application afterwards again.

What would really help us:

As we don't want to check the log-file size every few hours ourself, we thought about an automated mechanism doing this job for us. That's why I come up to my question:

Is there a possibility to create an executed file (executed by task scheduler every hour or scanning the file/folder 24/7) the size of a folder or a file and sending any kind of notification (e. g. E-Mail / anything else) when the folder/file exceeds a certain size (e. g. 200 MBs).

I am totally a beginner to PowerShell and Batch-Files, that's why I am really grateful for any kind of consulting and if possible a fully provided and described solution.

| improve this question | | | | |
  • 2
    Have you looked at anything or tried anything? The answer is yes but just like StackOverflow SU is not a free scripting service. Have a look at Get-Item, Measure-Object and Send-MailMessage. – Seth May 13 '19 at 8:35
  • Hey Seth. Thanks, that already helps me a lot :) So I will take a look at those methods and try if I can script it myself. – SteffPoint May 13 '19 at 8:53
  • @Seth: I have created the script using ur advice - see the answer :) Thank you very much! – SteffPoint May 14 '19 at 10:30
1

Using the advice of Seth in the comments section, I have created the script using the listed methods - it works fine for me:

$startFolder = "C:\softwarename\data\logs";

$totalSize = 0;

$colItems = Get-ChildItem $startFolder
foreach ($i in $colItems)
{
    $subFolderItems = Get-ChildItem $i.FullName -recurse -force | Where-Object {$_.PSIsContainer -eq $false} | Measure-Object -property Length -sum | Select-Object Sum
    $totalSize = $totalSize + $subFolderItems.sum / 1MB

}

$startFolder + " | " + "{0:N2}" -f ($totalSize) + " MB"

if($totalSize -gt 1000){

Send-MailMessage -to "testuser@test.com" -from "PowerShell <test@test.com>" -Subject "Test" -body "Message Message Message" -Credential "MailUser"

}

As soon as the folder is larger than 1000 MB (-gt 1000), it will send an email.

| improve this answer | | | | |
  • 1
    The | Select-Object Sum shouldn't be necessary as you use .sum to access that property. You could use -File for the recursive Get-Item (not necessary here, using -Directory on the first Get-ChildItem could make sense). Nice job. – Seth May 14 '19 at 12:56
  • Thank you very much Seth. I have removed | Select-Object Sum in my local file and it still has same functionality. So there really isn't a need for this :) – SteffPoint May 14 '19 at 13:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.