我真的很讨厌脚本编写,需要您的帮助。我从互联网上的多个地方拼凑了此脚本,并且该脚本一直有效,直到启用IF语句为止。
我只是想从UNC路径中获取文件夹的文件计数,如果超过指定数量,那么我希望它发送一封电子邮件,让我知道当前计数。
但是,如果我取消注释if ($count -gt 50)
部分,那么如果计数超过50,我将不会收到电子邮件。
我不知道如何将“ .Count”设置为变量,以便我在脚本的其他地方使用。有人可以帮忙吗?
然后,我需要弄清楚如何运行它。只是想在Windows中执行预定的任务,每隔几分钟或每分钟运行一次,但是如果您有更好的主意,我想听听他们的意见!
$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
#}
答案 0 :(得分:0)
您的主要问题似乎是没有将文件计数保存到任何变量。相反,您将值保存为字符串的一部分-而不是数字。 [咧嘴]
以下代码将当前目录的文件计数显式放入var中,将其添加到 total 计数中,然后使用当前计数为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
}
输出...
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...
$Body
变量的全部内容...
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