Powershell脚本,用于检查文件是否存在X天,并通过电子邮件通知我

Powershell脚本,用于检查文件是否存在X天,并通过电子邮件通知我,powershell,Powershell,我有下面的脚本,它的工作,我希望它能够给我发送电子邮件,如果我的文件是2天的旧。我不知道如何让电子邮件部分工作。 多谢各位 $fullPath = "\\test\avvscan.dat" $numdays = 2 $numhours = 1 $nummins = 1 function ShowOldFiles($path, $days, $hours, $mins) { $files = @(get-childitem $path -include *.* -recurse | whe

我有下面的脚本,它的工作,我希望它能够给我发送电子邮件,如果我的文件是2天的旧。我不知道如何让电子邮件部分工作。 多谢各位

$fullPath = "\\test\avvscan.dat"
$numdays = 2
$numhours = 1
$nummins = 1
function ShowOldFiles($path, $days, $hours, $mins)
{
    $files = @(get-childitem $path -include *.* -recurse | where 
{($_.LastWriteTime -lt (Get-Date).AddDays(-$days).AddHours(-$hours).AddMinutes(-$mins)) -and 
($_.psIsContainer -eq $false)})
    if ($files -ne $NULL)
    {
        for ($idx = 0; $idx -lt $files.Length; $idx++)
        {
            $file = $files[$idx]
            write-host ("Old: " + $file.Name) -Fore Red
        }
    }
}
ShowOldFiles $fullPath $numdays $numhours $nummins

也许是这样的:

$fullPath = "\\test\avvscan.dat"
$numdays = 2
$numhours = 1
$nummins = 1

function Get-OldFiles($path, $days, $hours, $mins) {
    $refDate = (Get-Date).AddDays(-$days).AddHours(-$hours).AddMinutes(-$mins)
    Get-ChildItem $path -Recurse -File | 
        Where-Object {($_.LastWriteTime -lt $refDate)} |
        ForEach-Object {
            Write-Host ("Old: " + $_.FullName) -ForegroundColor Red
            # emit an object containing the interesting parts for your email
            [PSCustomObject]@{
                'File'          = $_.FullName
                'LastWriteTime' = $_.LastWriteTime
            }
        }
}

$oldFiles = @(Get-OldFiles $fullPath $numdays $numhours $nummins)
if ($oldFiles.Count) {
    # send an email if there are old files found
    $body = $oldFiles | Format-Table -AutoSize | Out-String
    # look for more options here: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/send-mailmessage?view=powershell-3.0
    Send-MailMessage -From "someone@yourdomain.com" -To "you@yourdomain.com" -SmtpServer "your.smtp.server" -Subject "Old files in $fullPath" -Body $body
}

你的PowerShell版本是什么?如果您有v3或更高版本,只需使用