Help Required | PowerShell script to send output as email attachment.

  • 552 Views
  • Last Post 04 January 2017
syam posted this 02 January 2017

Dear All,

 

Good Day!

I am almost new to PowerShell, I have a script here which can get the count of computer objects in each OU and write the output into a CSV file.

While going through the report, I thought it would be good if I can get the CSV file as an attachment send to a specified email ID. (I know the SMTP details to get the email ID delivered). 

 

Could someone please help me modify the below script to send the output CSV to an email ID.

Any help is very much appreciated.

 Regards,Syam. 
 ###############################################################################################

 

version 3.0 - counts

computers in each ou, allows output in the table

 

Import-Module activeDirectory 

 

$output = Read-Host "'Y' for output to file or any key for output in GUI table view" -foreground Cyan

$fqdn = Read-Host "Enter FQDN domain" 

$cred = Get-Credential 

 

Write-Host "Contacting $fqdn domain..." -ForegroundColor Yellow 

 

$domain = (get-addomain $fqdn -Credential $cred | select distinguishedName,pdcEmulator,DNSroot,DomainControllersContainer) 

 

Write-Host "Completed. Enumerating OUs.." -ForegroundColor Yellow 

 

$OUlist = @(Get-ADOrganizationalUnit -filter * -Credential $cred -SearchBase $domain.distinguishedName -SearchScope Subtree -Server $domain.DNSroot) 

Write-Host "Completed. Counting computers..." -ForegroundColor Yellow 

 

for($i = 1; $i -le $oulist.Count; $i++) 

    {write-progress -Activity "Collecting OUs" -Status "Finding OUs $i" -PercentComplete ($i/$OUlist.count*100)} 

$newlist = @{} 

 

 

 

foreach ($_objectitem in $OUlist) 

    { 

    $getComputer = Get-ADComputer -Filter * -Credential $cred -SearchBase $_objectItem.DistinguishedName -SearchScope OneLevel -Server $domain.pdcEmulator | measure | select Count 

    for($i = 1; $i -le $getComputer.Count; $i++) 

    {write-progress -Activity "Counting computers" -Status "Finding computers $i in $_objectitem" -PercentComplete ($i/$getComputer.count*100)} 

     

    $newlist.add($_objectItem.DistinguishedName, $getComputer.Count)     

    } 

 

 if ($output -eq "Y")

 {

 $newlist | ft -AutoSize | Out-File .\OUcomputerCount.csv

 Write-Host "All done!" -ForegroundColor yellow  

 }

 else

 {

 $newList | Out-GridView

 } 
##############################################################################################

Order By: Standard | Newest | Votes
Dima Razbornov posted this 02 January 2017

Hi Syam,

If you want to add an attachment to email, you could use simle Send-Mailmessage cmdlet in your final part.

 

 else

 {

 $newList | Out-GridView

 } 
##############################################################################################

$From = "Powershellcoolrobot@domain.com"
$To = "AnotherEmail@YourDomain.com"
$Cc = "YourBoss@YourDomain.com"
$Attachment = "\OUcomputerCount.csv"
$Subject = "OU list"
$Body = "Insert body text here"
$SMTPServer = "exchangeserver.yourdomain.com"
Send-MailMessage -From $From -to $To -Cc $Cc -Subject $Subject `
-Body $Body -SmtpServer $SMTPServer -Attachments $Attachment

ElasticSky posted this 04 January 2017

Hi Syam,

 

Not sure if you got a response as sometimes our SPAM filter selectively removes messages to this list ;)

 

The cmdlet called Send-MailMessage is your friend and as a quick and dirty if you add this line after you create the output file it should do the trick:

 

Send-MailMessage

–From

“User01 <user01@xxxxxxxxxxxxxxxx>”

–To

“User02 <user02@xxxxxxxxxxxxxxxx>”

–Subject

“Count of Computer Objects by OU”

–Body

“Here is a CSV containing counts of computer objects”

–Attachments

“OUComputerCount.csv” –SmtpServer

smtp.example.com

 

Again apology’s if somebody else has already responded to you on this.

 

Have a great day,

 

Glen

 

show

syam posted this 04 January 2017

Hi Glen,
Thanks a ton for the reply :)In fact I was worried as there was no response yet. Yours is the first response for my query. Certainly I will try this now and will get back to you. 
Have a great day ahead Glen,Syam. 


show

syam posted this 04 January 2017

Hi Glen,
It worked!!!!
You made my day. If you happened to be in Bangalore, India I will certainly buy you beer :)A Million thanks Glen.
Syam. 


show

Close