How to send email

%3CLINGO-SUB%20id%3D%22lingo-sub-1382524%22%20slang%3D%22en-US%22%3EHow%20to%20send%20email%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1382524%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20All%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20new%20to%20PS%20and%20i%20need%20a%20email%20message%20where%20it%20sends%20me%20by%20identifying%20recent%20file%20names%20with%20date%20and%20time%20in%20particular%20folder%20on%20the%20server%3C%2FP%3E%3CP%3Ebelow%20is%20the%20simple%20commands%20i%20set%20in%20the%20script%20where%20i%20get%20empty%20body%20message%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%24email%20%3D%20Get-ChildItem%20-path%20D%3A%5CTFTP-Root%20%7C%20Where-Object%20%7B%24_.LastWriteTime%20-gt%20(Get-Date).AddDays(-1)%7D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESend-MailMessage%20-To%20'bhuvankumar%40gmail.com'%20-SmtpServer%26nbsp%3B%20'smtp.com'%20-Subject%20'Test%20Email'%20-Body%20%24email%20-From%20%3CA%20href%3D%22mailto%3Acesappfml01%40conagrafoods.com%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Etets01%40ymail.com%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBelow%20output%20i%20need%20to%20get%20in%20email%20body%20message%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Directory%3A%20D%3A%5CTFTP-Root%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EMode%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20LastWriteTime%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Length%20Name%3C%2FP%3E%3CP%3E----%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20-------------%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20------%20----%3C%2FP%3E%3CP%3E-a----%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%205%2F11%2F2020%26nbsp%3B%26nbsp%3B%209%3A43%20PM%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B226632%20testa03-cfg.2020-05-11-22.00.12%3C%2FP%3E%3CP%3E-a----%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%205%2F11%2F2020%26nbsp%3B%26nbsp%3B%209%3A44%20PM%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20227116%20testa04-cfg.2020-05-11-22.00.14%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%2C%3C%2FP%3E%3CP%3EBhuvan%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1382524%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EPowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1391014%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20send%20email%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1391014%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F664633%22%20target%3D%22_blank%22%3E%40kdvbhuvankumar%3C%2FA%3E%26nbsp%3BAlmost%20there%20my%20friend!%20Check%20the%20script%20below.%20I%20hope%20it%20helps%20you%20take%20the%20next%20step!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EGrtz%2C%20Manfred%20de%20Laat%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%23%20Get%20the%20Files%0A%24items%20%3D%20Get-ChildItem%20-path%20D%3A%5CTFTP-Root%20%7C%20Where-Object%20%7B%24_.LastWriteTime%20-gt%20(Get-Date).AddDays(-1)%7D%0A%20%0A%23%20Create%20a%20mailbody%0A%24mailbody%20%3D%20%22%22%20%0Aforeach(%24item%20in%20%24items)%7B%0A%20%20%20%20%24mailbody%20%2B%3D%20%24mailbody%20%2B%20%22File%3A%20%22%20%2B%20%24item.FullName%20%2B%20%22%60n%22%20%2B%20%22Size%20(KB)%3A%20%22%20%2B%20%24item.Length%20%2B%20%22%60r%60n%60n%22%0A%7D%0A%0A%23%20Add%20the%20mailbody%20%0ASend-MailMessage%20-To%20'xxxxx%40gmail.com'%20-SmtpServer%20%20'smtp.com'%20-Subject%20'Files%20and%20Sizes'%20-Body%20%24mailbody%20-From%20%22yyyyy%40gmail.com%22%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1496925%22%20slang%3D%22en-US%22%3ERe%3A%20How%20to%20send%20email%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1496925%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F664633%22%20target%3D%22_blank%22%3E%40kdvbhuvankumar%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYou%20cannot%20send%20an%20object%20as%20mail%20body%2C%20you%20need%20to%20convert%20to%26nbsp%3B%20%26nbsp%3Bstring%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%5B%5B-Body%5D%20%3CSTRING%3E%5D%3C%2FSTRING%3E%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24emailObject%20%3D%20Get-ChildItem%20-path%20D%3A%5CTFTP-Root%20%7C%20Where-Object%20%7B%24_.LastWriteTime%20-gt%20(Get-Date).AddDays(-1)%7D%0A%0A%24emailcsvtxt%3D%20%24emailObject%20%7Cselect%20Mode%20%2CLastWriteTime%20%2CLength%20%2CName%7CConvertTo-Csv%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
Occasional Visitor

Hi All,

 

I am new to PS and i need a email message where it sends me by identifying recent file names with date and time in particular folder on the server

below is the simple commands i set in the script where i get empty body message

 

$email = Get-ChildItem -path D:\TFTP-Root | Where-Object {$_.LastWriteTime -gt (Get-Date).AddDays(-1)}

 

Send-MailMessage -To 'bhuvankumar@gmail.com' -SmtpServer  'smtp.com' -Subject 'Test Email' -Body $email -From tets01@ymail.com

 

Below output i need to get in email body message 

    Directory: D:\TFTP-Root

 

Mode                LastWriteTime         Length Name

----                -------------         ------ ----

-a----        5/11/2020   9:43 PM         226632 testa03-cfg.2020-05-11-22.00.12

-a----        5/11/2020   9:44 PM         227116 testa04-cfg.2020-05-11-22.00.14

 

Thanks,

Bhuvan

2 Replies
Highlighted

@kdvbhuvankumar Almost there my friend! Check the script below. I hope it helps you take the next step!

 

Grtz, Manfred de Laat

# Get the Files
$items = Get-ChildItem -path D:\TFTP-Root | Where-Object {$_.LastWriteTime -gt (Get-Date).AddDays(-1)}
 
# Create a mailbody
$mailbody = "" 
foreach($item in $items){
    $mailbody += $mailbody + "File: " + $item.FullName + "`n" + "Size (KB): " + $item.Length + "`r`n`n"
}

# Add the mailbody 
Send-MailMessage -To 'xxxxx@gmail.com' -SmtpServer  'smtp.com' -Subject 'Files and Sizes' -Body $mailbody -From "yyyyy@gmail.com"

 

Highlighted

@kdvbhuvankumar 

You cannot send an object as mail body, you need to convert to   string    [[-Body] <string>]

$emailObject = Get-ChildItem -path D:\TFTP-Root | Where-Object {$_.LastWriteTime -gt (Get-Date).AddDays(-1)}

$emailcsvtxt= $emailObject |select Mode ,LastWriteTime ,Length ,Name|ConvertTo-Csv