Author Posts

September 3, 2013 at 8:09 pm

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

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

September 4, 2013 at 8:56 am

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