using powershell to send mail only when excel has data

Wu Yuki 81 Reputation points
2021-03-10T03:03:58.367+00:00

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
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,388 questions
{count} votes

Accepted answer
  1. Ian Xue (Shanghai Wicresoft Co., Ltd.) 30,376 Reputation points Microsoft Vendor
    2021-03-10T09:21:12.817+00:00

    Hi @Wu Yuki ,

    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.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Rich Matheisen 45,096 Reputation points
    2021-03-10T20:52:52.217+00:00

    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
    }
    
    0 comments No comments