question

WuYuki-5257 avatar image
0 Votes"
WuYuki-5257 asked ·

using powershell to send mail only when excel has data

Hi ,


I has created a script to get data from AD a specified group and export the .CSV file.

now , no matter the .CSV if has data or not, the email will be sent automatically by PowerShell

I wan to ask , how could I set only when the export .csv has the data then the email be sent ?


Thanks for your advice and great help in advance!

windows-server-powershell
· 2
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @WuYuki-5257 ,

could you please share your script. It's easier to help and find a solution for your script.


Kind regards
Andreas Baumgarten

0 Votes 0 ·
WuYuki-5257 avatar image WuYuki-5257 AndreasBaumgarten ·

Hi AndreasBaumgarten ,

Thanks ,attached script pls help to check how to avoid send email when the out put Excel without any data in it.
76174-script.txt


0 Votes 0 ·
script.txt (1.1 KiB)
IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered ·

Hi @WuYuki-5257 ,

You can check the csv file like below

 if(Get-Content $FileName){
     send-MailMessage -From xxxxx@abc.com -to xxxxx@abc.com -Subject "XXX_Check" -Body $mailbody -attachment $FileName -SmtpServer smtp.XXX.com -Encoding Unicode
 }
 else{
     Write-Host 'The csv file has no data in it.'
 }

Best Regards,
Ian Xue
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered ·

Does this work for you?

 $MIMNewOu = 'OU=Starters,OU=xx,DC=xx, DC=xx,DC=xx,DC=com'
 $date = Get-Date -Format "yyyyMMdd"
 $FileName = "D:\powershell test\MIM_EMAIL_ALERT\KR\"+"xxxx"+$Date + ".CSV"
    
 $Mailbody=@"
    
 Dear All,
    
 Thanks.
 "@
    
 $MailProps = @{
     From = "xxxxx@abc.com"
     To = "xxxxx@abc.com"
     Subject = "XXX_Check"
     Body = $mailbody
     Attachment = $FileName
     SmtpServer = "smtp.XXX.com"
     Encoding = "Unicode"
 }
    
 $p = "city","displayname","CanonicalName","manager","office","country","whencreated","SamAccountName"
 $DataProps = @{
     "Display Name" = ""
     "Canonical Name" = ""
      City = ""
      Country = ""
 }
    
 $DataCount = 0
 Get-ADUser -SearchBase $MIMNewOu -Properties $p -Filter {country -like "KR"} |
     ForEach-Object{
         $DataCount++
         $props."Display Name" = $_.displayname
         $props."Canonical Name" = $_.CanonicalName
         $props.City = $_.city
         $props.Country = $_.country
         [PSCustomObject]$DataProps
 } | Export-Csv $FileName -NoTypeInformation -Encoding UTF8
 if ($DataCount -gt 0){
     Send-MailMessage @MailProps
 }
·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.