none
need help in powershell scripting RRS feed

  • Question

  • need help in powershell scripting.
    
    My requirements.
    
        i have created a script which helps me getting all the send and receive email to user1@domain.com in the previous 24 hours that had an attachment
        Then the script will generate a mail to the specific user (user2@domain.com )
        In the body of the mail, it will include a count for all the send and receive email in the last 24 hours and the csv report with detail in step.2 will be the attachment for this mail.”
    
    Check the below script which helps me getting an attachment but it gives all the email from one particular mailbox but i need an email which only had an attachment send and receive to that mailbox
    
    # # Quick email report script #
    
    #Need Creds here - create account with access and embedded if scripting
    
    $credential = Get-Credential
    
    #Connect to ExchangeOnline
    
    $o365Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://outlook.office365.com/powershell-liveid/" -Credential $credential -Authentication "Basic" -AllowRedirection
    
    Import-PSSession $o365Session -DisableNameChecking
    
    # Get-AcceptedDomain | ft
    
    $date=(Get-Date -Format s ).Split("T")[0]
    
    #
    
    # Date needs to be US format for some reason or use date object
    
    # note the timezone for O365 is universal time
    
    # default limits to 1000 object increase if required.
    
    #
    
    $emailaddress="user1@domain.com"
    
    $emailreport=@()
    
    $emailreport+=Get-MessageTrace -SenderAddress $emailaddress -StartDate $(Get-Date).AddDays(-1).ToUniversalTime() -endDate $(Get-Date).ToUniversalTime() -PageSize 2000
    
    $emailreport+=Get-MessageTrace -RecipientAddress $emailaddress -StartDate $(Get-Date).AddDays(-1).ToUniversalTime() -endDate $(Get-Date).ToUniversalTime() -PageSize 2000
    
    $emailreport| sort received | Export-Csv -Path C:\temp\$date'APNInvoice.csv'
    
    $bodytext="total Emails to\From user1@domain.com: " + $emailreport.Count |Out-String
    
    $bodytext+=$emailreport| group senderaddress | sort count -Descending |select count,name |Out-String
    
    $emailaddress="user1@domain.com"
    
    $emailreport=@()
    
    $emailreport+=Get-MessageTrace -SenderAddress $emailaddress -StartDate $(Get-Date).AddDays(-1).ToUniversalTime() -endDate $(Get-Date).ToUniversalTime() -PageSize 2000
    
    $emailreport+=Get-MessageTrace -RecipientAddress $emailaddress -StartDate $(Get-Date).AddDays(-1).ToUniversalTime() -endDate $(Get-Date).ToUniversalTime() -PageSize 2000
    
    $emailreport| sort received | Export-Csv -Path C:\temp\$date'JDEInvoice.csv'
    
     
    
     
    
     
    
    Send-MailMessage -SmtpServer smtp.lc.local -Subject "JDE Report $date" -Body $bodytext  -to 'user2@domain.com' -From 'automated.reports@cpbcon.com.au'-Attachments C:\temp\$date'APNInvoice.csv',C:\temp\$date'JDEInvoice.csv'
    
     
    
    Del C:\temp\$date'APNInvoice.csv'
    
    Del C:\temp\$date'JDEInvoice.csv'
    
     
    
    # # End of Script #
    
    
    


    • Edited by fight to win Tuesday, March 14, 2017 2:11 PM
    • Moved by Bill_Stewart Friday, April 28, 2017 7:46 PM This is not "scripts on demand"
    Tuesday, March 14, 2017 10:59 AM

All replies