Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

send e-mail report via powershell

Posted on 2014-08-20
15
Medium Priority
?
409 Views
Last Modified: 2014-08-21
Hi,

I have this code I am running for inactive AD accounts and I now need to get this code run every month automaticly and send e-mail to a specified email address with specified subject. How can I do that please?

Search-ADAccount -AccountInactive -DateTime 20/07/2014 | where {$_.ObjectClass -eq 'user'} | FT Name,ObjectClass –A |out-file C:\temp\30days_inactive_accounts.csv
0
Comment
Question by:kuzum
  • 6
  • 6
  • 2
  • +1
15 Comments
 
LVL 5

Expert Comment

by:basil2912
ID: 40273247
You can run a task that runs monthly a script that creates the report and sends it using send-mailmessage

http://technet.microsoft.com/en-us/library/hh849925.aspx
0
 
LVL 8

Expert Comment

by:Jayaraja Jayaraman
ID: 40273252
you can create a task scheduler to run it on every month and call powershell.exe and then execute this powershell script you have.Now that the script is created you can set it up as a scheduled task.

On the system that the task will be run from, open the Windows Task Scheduler. This can be found in the Start menu, under Start > Administrative Tools.

In the Task Scheduler, select the Create Task option under the Actions heading on the right-hand side.

Enter a name for the task, and give it a description (the description is optional and not required).

In the General tab, go to the Security options heading and specify the user account that the task should be run under. Change the settings so the task will run if the user is logged in or not.

Next, select the Triggers tab, and click New to add a new trigger for the scheduled task. This new task should use the On a schedule option. The start date can be set to a desired time, and the frequency and duration of the task can be set based on your specific needs. Click OK when your desired settings are entered.

The below example has the task running every week on Sunday morning at 1 AM, but will stop the task if it runs longer than 4 hours.

Next, go to the Actions tab and click New to set the action for this task to run. Set the Action to Start a program.

In the Program/script box enter "PowerShell."

In the Add arguments (optional) box enter the value ".\[Your PowerShell Script Name]." For example, if your PowerShell Script is named "Migration1.ps1" then you would enter ".\Migration1.ps1" as the value.

Then, in the Start in (optional) box, add the location of the folder that contains your PowerShell script. In this example, the script is in a folder called "Script" that is off the root C: drive.

Note: The location used in the Start in box will also be used for storing the scheduled task run times, the job history for the copies, and any additional logging that may occur.
Click OK when all the desired settings are made.

Next, set any other desired settings in the Conditions and Settings tabs. You can also set up additional actions, such as emailing an Administrator each time the script is run.

Once all the desired actions have been made (or added), click OK. The task will be immediately set, and is ready to run.

The scheduling of this task is complete, and is now ready to run based on the entered settings.


Add-PSSnapin Microsoft.Exchange.Management.Powershell.Admin -erroraction silentlyContinue
Search-ADAccount -AccountInactive -DateTime 20/07/2014 | where {$_.ObjectClass -eq 'user'} | FT Name,ObjectClass –A |out-file C:\temp\30days_inactive_accounts.csv
$file = "C:\temp\30days_inactive_accounts.csv"
$mailboxdata = (Get-MailboxStatistics | select DisplayName, TotalItemSize,TotalDeletedItemSize, ItemCount, LastLoggedOnUserAccount, LastLogonTime)
$mailboxdata | export-csv "$file"
$smtpServer = "127.0.0.1"
$att = new-object Net.Mail.Attachment($file)
$msg = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$msg.From = "emailadmin@test.com"
$msg.To.Add("administrator1@test.com")
$msg.To.Add("administrator2@test.com")
$msg.Subject = "Notification from email server"
$msg.Body = "Attached is the email server mailbox report"
$msg.Attachments.Add($att)
$smtp.Send($msg)
$att.Dispose()

modify the emails and parameters accordingly
0
 

Author Comment

by:kuzum
ID: 40273280
thanks for the help guys, do Ireally need

"$mailboxdata = (Get-MailboxStatistics | select DisplayName, TotalItemSize,TotalDeletedItemSize, ItemCount, "

I simply just want powershell report to be e-mailed a specific (same) mailbox every month.

Also, do I need to set scheduled task on my exchange server or local windows 7 PC ?

thanks
0
Who's Defending Your Organization from Threats?

Protecting against advanced threats requires an IT dream team – a well-oiled machine of people and solutions working together to defend your organization. Download our resource kit today to learn more about the tools you need to build you IT Dream Team!

 
LVL 8

Expert Comment

by:Jayaraja Jayaraman
ID: 40273322
you need to schedule the task from the exchange server. because the get-mailboxstatics cmdlet gets info from exchange server.
$msg.From = "emailadmin@test.com"  #from email address from which the mail has to be sent
$msg.To.Add("administrator1@test.com") # to email address to which the mail has to be sent
$msg.To.Add("administrator2@test.com")#additional email address to which the mail has to be sent, you can ignore this if you dont want to add additional email on the to field
$msg.Subject = "Notification from email server" #subject of the email
$msg.Body = "Attached is the email server mailbox report" #body of the email
0
 
LVL 40

Assisted Solution

by:Subsun
Subsun earned 1000 total points
ID: 40273439
Create a schedule task to recur monthly with following code.. You need to replace from@domain.com, to@domain.com and smtp.domain.com as per your environment/requirement..

Import-Module Activedirectory
Search-ADAccount -AccountInactive -TimeSpan 30.00:00:00 | where {$_.ObjectClass -eq 'user'} | Select Name,ObjectClass | Export-csv C:\temp\30days_inactive_accounts.csv -nti
Send-MailMessage -From from@domain.com -To to@domain.com -SmtpServer smtp.domain.com -Attachments C:\temp\30days_inactive_accounts.csv -Subject "30 days inactive accounts" -Body "PFA 30 days inactive accounts"

Open in new window


How to Schedule a PowerShell Script
http://community.spiceworks.com/how_to/show/17736-run-powershell-scripts-from-task-scheduler

Edit : Added '-TimeSpan 30.00:00:00' parameter to Search-ADAccount, so that it return the 30 days inactive accounts..
0
 
LVL 8

Expert Comment

by:Jayaraja Jayaraman
ID: 40273639
@Author, Subsun's advice would be lot easier approach and does fit to what exactly you are looking for
0
 

Author Comment

by:kuzum
ID: 40274105
thanks guys,

@subsun- DO I need to run this on AD or Exchange server or simply on my Windows 7 machine?
0
 

Author Comment

by:kuzum
ID: 40274127
I strugling with this pit, I'm trying to do this on DC server with my admin account ?
task-scheduler.PNG
0
 
LVL 8

Expert Comment

by:Jayaraja Jayaraman
ID: 40274128
i dont think import module active directory is available on windows 7
0
 
LVL 8

Expert Comment

by:Jayaraja Jayaraman
ID: 40274138
you need to do this
Click Start,
In "Start Search" type secpol.msc and press Enter.
In "Local Security Policy" window, click to expand "Local Policy".
Click to open "User Rights Assignment".
In the right panel, right click on "Log on as a Batch job" then click on "Properties".
In "Log on as batch job Properties" window, click "Add User or Group" and include the user or group do you need.
Click OK, and close "Local Security Policy" window.
0
 
LVL 40

Expert Comment

by:Subsun
ID: 40274174
You just need AD module for this script.. You can add Active Directory module in PowerShell for Windows 7
Ref :  http://blogs.msdn.com/b/rkramesh/archive/2012/01/17/how-to-add-active-directory-module-in-powershell-in-windows-7.aspx

With win 2008 R2 or above DC, you just need to install Remote Server Administration Tools on Windows 7.
0
 

Author Comment

by:kuzum
ID: 40274209
@Jayaraja Jayaraman - adding accounts is greyed out on me. I don't think you can manage local policies on GCs ?
0
 
LVL 8

Accepted Solution

by:
Jayaraja Jayaraman earned 1000 total points
ID: 40274217
same situation like here http://social.technet.microsoft.com/Forums/windowsserver/en-US/760ee186-8074-40a5-95b0-35d17c2bdfab/log-on-as-batch-job-right
somethign fore found
If you server is under DC, you need Set Up "Log on as a Batch job" policy in the "Group Policy Management"
Click "Start", in "Start Search" type "gpmc.msc" and press Enter
Click to expand "Forest" > "Domain" > "Domain Name of your Organization"
Right Click in "Default Domain Policy" and click on "Edit"
In "Computer Configuration", click to expand "Policies" > "Windows Settings" > "Secutity Settings" > "Local Policies", then click in "User Right Assignment"
In the Policy panel seach "Log on as a batch job" policy, on "Log on as a batch job" policy right click and select properties.
In "log on as a batch job Properties" window, click to check "define these policy settings:"
after that click "Add User or Group" and include the user or group do you need..
and click OK to close all open windows.
0
 

Author Comment

by:kuzum
ID: 40275759
so I am guessing I can do the whole process on GC server with no issue instead?
0
 

Author Closing Comment

by:kuzum
ID: 40276491
do not perform this, rhis causes issues


 Click Start,
 In "Start Search" type secpol.msc and press Enter.
 In "Local Security Policy" window, click to expand "Local Policy".
 Click to open "User Rights Assignment".
 In the right panel, right click on "Log on as a Batch job" then click on "Properties".
 In "Log on as batch job Properties" window, click "Add User or Group" and include the user or group do you need.
 Click OK, and close "Local Security Policy" window.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s time for spooky stories and consuming way too much sugar, including the many treats we’ve whipped for you in the world of tech. Check it out!
Scripts are great for performing batch jobs against users, however sometimes the GUI is all you need.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Loops Section Overview
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question