0

I really stink at scripting and I need your help. I pieced together this script from several places on the internet and it works, until I enable my IF statement...

I'm just trying to get a count of files of a folder from a UNC path, and if it's over a specified amount, then I want it to send an email letting me know with the current count.

However, if I uncomment the if ($count -gt 50) part, then I won't get an email if the count is over 50.

I don't know how to make the ".Count" a variable for me to use elsewhere in the script. Can someone please help?

Then I'll need to figure out how to run it. Was thinking just a scheduled task in windows and have it run every few minutes or something, but if you have any better ideas, I'd like to hear them!

$FolderList = @(
    "\\server\path\test"
        )
$Body = ($FolderList | ForEach-Object {
    "Check to see if Sweep Service is running, file count for '$($_)':  " + (Get-ChildItem -Path $_ -File -ErrorAction SilentlyContinue | Measure-Object).Count
}) -join "`r`n"

#if ($count -gt 50)
#{
    $From = "me@you.com"
    $To = "me@you.com"
    $Subject = "Sweep Checker"
    $SmtpServer = "webmail.you.com"
    Send-MailMessage -From $From -to $To -Subject $Subject -Body $Body -SmtpServer $SmtpServer
#}
TylerH
  • 20,799
  • 66
  • 75
  • 101
  • Do you add an end brace after uncommenting the if statement? If so please [add](https://stackoverflow.com/posts/58242863/edit) an end brace to your code (indenting the if block would help too). – Ross Jacobs Oct 04 '19 at 20:37
  • Yes, sorry. I had the end brace commented out but it gave me an error, so when I comment out the if statement I started deleting the end brace to test. – Ryan Jacoby Oct 04 '19 at 20:38
  • I adjusted the indentation to make it easier to see the bits that *would* be inside the if loop, were it not commented out – TylerH Oct 04 '19 at 20:42
  • Do you understand how variable assignment works? You create a variable called `$count` the same way you created a variable named `$body`: You assign a value to it with an `=` sign. – Daniel Mann Oct 04 '19 at 20:49
  • This [answer](https://stackoverflow.com/questions/11526285/how-to-count-objects-in-powershell) may help with counting in powershell. – interduo Oct 04 '19 at 20:51
  • @interduo That won't help because the question isn't about *counting*. It's about *variable assignment*. – Daniel Mann Oct 04 '19 at 20:55
  • I do know how variables work, but I'm not sure if the statement is actually creating the "$count" variable or not: + (Get-ChildItem -Path $_ -File -ErrorAction SilentlyContinue | Measure-Object).Count What is the point of the "." before the word "count"? Is it outputting the count in the statement and forgets it right afterwards, or can I reference the count in another call? Well, I'm assuming I can't, since it doesn't work when I try. – Ryan Jacoby Oct 04 '19 at 20:56
  • The `.` character means "member of object". The `Get-ChildItem` command you posted outputs a number of objects, and you are counting them by piping to `Measure-Object`. `Measure-Object` itself outputs an object, of which `Count` is one of its properties. Thus `.Count` means "I want the `Count` property of the `Measure-Object` output object". – Bill_Stewart Oct 04 '19 at 21:03
  • @Daniel Mann He's trying to _count_ the number of files in a given folder and assign it to a variable, and the answer I referenced shows an example of both. – interduo Oct 04 '19 at 21:15
  • @Bill_Stewart ah, that makes more sense, thanks very much for the explanation! – Ryan Jacoby Oct 04 '19 at 22:26

1 Answers1

0

your major problem seems to be NOT saving the file count to any variable. instead, you are saving the value as part of a string - not a number. [grin]

the following code explicitly puts the file count for the current dir into a var, adds that to the total count, and then uses the current count to build your output string for the body of your msg.

$FolderList = @(
    $env:TEMP
    $env:USERPROFILE
    $env:ALLUSERSPROFILE
        )
$TriggerFileCount = 20
$TotalFileCount = 0

$Body = foreach ($FL_Item in $FolderList)
    {
    $CurrentFileCount = @(Get-ChildItem -LiteralPath $FL_Item -File -ErrorAction SilentlyContinue).Count
    $TotalFileCount += $CurrentFileCount

    # send out to the "$Body" collection
    'Check to see if Sweep Service is running, file count for [ {0} ] = {1}' -f $FL_Item, $CurrentFileCount
    }

if ($TotalFileCount -gt $TriggerFileCount)
    {
    $SMM_Params = @{
        From = 'NotMe@example.com'
        To = 'NotYou@example.com'
        Subject = 'Sweep Checker'
        Body = $Body -join [System.Environment]::NewLine
        SmtpServer = $SmtpServer
        }

    $SMM_Params

    #Send-MailMessage @SMM_Params
    }

output ...

Name                           Value
----                           -----
Subject                        Sweep Checker
From                           NotMe@example.com
To                             NotYou@example.com
SmtpServer                     webmail.you.com
Body                           Check to see if Sweep Service is running, file count for [ C:\Temp ] = 22...

the full content of the $Body variable ...

Check to see if Sweep Service is running, file count for [ C:\Temp ] = 22
Check to see if Sweep Service is running, file count for [ C:\Users\MyUserName ] = 5
Check to see if Sweep Service is running, file count for [ C:\ProgramData ] = 0
Lee_Dailey
  • 7,292
  • 2
  • 22
  • 26