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 = "[email protected]"
$To = "[email protected]"
$Subject = "Sweep Checker"
$SmtpServer = "webmail.you.com"
Send-MailMessage -From $From -to $To -Subject $Subject -Body $Body -SmtpServer $SmtpServer
#}
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 = '[email protected]'
To = '[email protected]'
Subject = 'Sweep Checker'
Body = $Body -join [System.Environment]::NewLine
SmtpServer = $SmtpServer
}
$SMM_Params
#Send-MailMessage @SMM_Params
}
output ...
Name Value
---- -----
Subject Sweep Checker
From [email protected]
To [email protected]
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