代码之家  ›  专栏  ›  技术社区  ›  Tomas

Powershell在检查文件的所有路径后发送一封电子邮件

  •  0
  • Tomas  · 技术社区  · 11 月前

    我有一个脚本,它检查几个文件夹中是否有新文件,如果有新文件的话,它会发送一封包含日志文件的电子邮件。

    $source = Get-Content "C:\AAA\source.txt"
    $logDate = get-date -format yyyy-MM-dd
    $logtime = get-date -format HH:mm
    $log = "C:\AAA\$logdate.log"
    
    $mailkomu = "[email protected]"
    $mailod = "[email protected]"
    $subject = "New files in DDD"
    $zprava = "Message"
    
    ForEach($path in $source){
        Test-path $path
        Write-Output "$logtime - $path - ok" | Out-File $log -Append 
        If ((Get-ChildItem -Path $path -Force | Measure-Object).Count -eq 0) {
            Write-Output "no files" | Out-File $log -Append
        }
        Else { 
            Write-Output "new files" | Out-File $log -Append
            $data = Get-ChildItem -Path $path -Recurse | Select-Object DirectoryName, Name
            $data | Out-File $log -Append
            Send-MailMessage -From $mailod -To $mailkomu -Subject $subject -Body $zprava -SmtpServer $SMTPServer -Attachments $log
            }
    }
    

    source.txt中有4个不同的路径,但可以有更多的路径。计数并不重要。 检查新文件非常有效。

    问题是,如果第二条路径上有新文件,它会立即发送电子邮件,而不会等待其他路径被检查。

    Example:
    first path - no file - sends no email
    second path - new file - sends email with log only for the first and the second path
    third path - new file - sends email with log only for the first, second and the third path
    fourth path - no file - sends no email
    

    有没有一种方法可以让它检查所有路径,如果有新文件,它会在检查所有路径后只发送一封带有日志的电子邮件?

    1 回复  |  直到 11 月前
        1
  •  2
  •   Mathias R. Jessen    11 月前

    您只需要一个变量来跟踪是否找到了任何新文件,然后移动 Send-MailMessage 跳出循环-等待循环完成后,您一次最多会收到一封电子邮件,其中包含整个日志:

    $source = Get-Content "C:\AAA\source.txt"
    $logDate = get-date -format yyyy-MM-dd
    $logtime = get-date -format HH:mm
    $log = "C:\AAA\$logdate.log"
    
    $mailkomu = "[email protected]"
    $mailod = "[email protected]"
    $subject = "New files in DDD"
    $zprava = "Message"
    
    # define variable to track whether new files have been found
    $newFilesFound = $false
    
    foreach ($path in $source) {
        # start by actually acting on whether the path describes an existing item
        $folderStatus = Test-Path $path
        if (-not $folderStatus) {
            # log failure to locate source folder, then continue on to the next source folder in the loop
            Write-Output "$logtime - $path - not found" | Out-File $log -Append 
            continue
        }
    
        Write-Output "$logtime - $path - ok" | Out-File $log -Append 
    
        # path exists, let's fetch all sub-items up front
        $childItems = @(Get-ChildItem -Path $path -Force -Recurse)
    
        if ($childItems.Count -eq 0) {
            Write-Output "no files" | Out-File $log -Append
        }
        else { 
            Write-Output "new files" | Out-File $log -Append
            $data = Get-ChildItem -Path $path -Recurse | Select-Object DirectoryName, Name
            $data | Out-File $log -Append
    
            # set the indicator that files where found
            $newFilesFound = $true
        }
    }
    
    # only send email with log file if any new files were found
    if ($newFilesFound) {
        Send-MailMessage -From $mailod -To $mailkomu -Subject $subject -Body $zprava -SmtpServer $SMTPServer -Attachments $log
    }