我想根据当前日期扫描文件夹路径。如果文件不存在则发送电子邮件

时间:2018-01-14 14:28:40

标签: powershell

我是新来的,需要一些帮助来增强这个脚本:

$path = "\\some\path"
if (!(test-path $path)) {
    Send-MailMessage  -To "car.one@outlook.com","car.two@gmail.com","car.three@yahoo.com" -From "Outbound@yahoo.com" -SmtpServer Smtp.office365.com -Subject "No file found" -BodyAsHtml -Body "No file was found at $path"  -UseSSL -Credential $cred
}

路径外观如下"\\Good\Morning\America有一个嵌套的子文件夹,日期为20180114.该文件夹中有文件。我只想扫描.abc和.def文件扩展名。

如果找不到这些附加信息,或者今天的日期文件夹不存在,请发送电子邮件通知。

如果文件夹和文件确实存在,则不采取任何措施。

提前感谢所有人的投入。

1 个答案:

答案 0 :(得分:0)

使用Get-ChildItem cmdlet和-Include参数来确定文件是否存在:

# Construct folder name and path from today's date
$TodaysFolderName = Get-Date -Format yyyyMMdd
$TodaysFolderPath = Join-Path '\\Good\Morning\America' $TodaysFolderName

# Fetch the required files
$Files = @(Get-ChildItem -Path $TodaysFolderPath -Include *.abc,*.def)

# See if any of the relevant files were found
if($Files.Count -lt 1){
    Send-MailMessage ...
}