Compare file creation date with current date and send email

Tagged: 

This topic contains 1 reply, has 2 voices, and was last updated by Profile photo of Dave Wyatt Dave Wyatt 3 years, 9 months ago.

  • Author
    Posts
  • #9908
    Profile photo of nidhinck
    nidhinck
    Participant

    Hi Experts,

    I used below script to create DFSR health check files and saved those files to a shared drive. Now i need to send the latest log files to my email ID daily. Could someone help me with posershell code for sending the latest created file from a folder ?

    ———————————————————————————————————–
    ———————————————————————————————————–
    I used below script to create heath check logs.

    @echo off

    set CURRDATE=%TEMP%\CURRDATE.TMP
    set CURRTIME=%TEMP%\CURRTIME.TMP

    DATE /T > %CURRDATE%
    TIME /T > %CURRTIME%

    set PARSEARG="eol=; tokens=1,2,3,4* delims=/, "
    for /F %PARSEARG% %%i in (%CURRDATE%) Do SET DDMMYYYY=%%j-%%k-%%l

    set PARSEARG="eol=; tokens=1,2,3* delims=:, "
    for /F %PARSEARG% %%i in (%CURRTIME%) Do Set HHMM=%%i%%j%%k

    set RG1_Report=\\DC001\SYSVOL\DC001-%DDMMYYYY%-%HHMM%.html

    @echo on

    dfsradmin health new /rgname:"Domain System Volume" /refmemname:asiadomain.com\DC001 /domain:asiadomain.com /ReportName:\\server01.com\DFSRHealthCheck\ASIA-DFSR_Health_Check_Report-%DDMMYYYY%-%HHMM%.html

    echo RG1 %RG1Report% > %TEMP%\healthMessageBody.txt

    ———————————————————————————————————–
    ———————————————————————————————————–

  • #9914
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    For sending an email, use the Send-MailMessage cmdlet (run Get-Help Send-MailMessage -Full to learn about its options). I'm not sure which file you want to send, but here's a generic example for finding the newest file in a folder:

    $folder = 'c:\path\to\something\'
    
    Get-ChildItem -Path $folder |
    Where-Object { $_.PSIsContainer -eq $false } |
    Sort-Object -Descending -Property LastWriteTime |
    Select-Object -First 1
    

You must be logged in to reply to this topic.