send e-mail report via powershell

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
kuzumAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

basil2912Commented:
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
Jayaraja JayaramanSharepoint Architect ConsultantCommented:
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
kuzumAuthor Commented:
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
ON-DEMAND: 10 Easy Ways to Lose a Password

Learn about the methods that hackers use to lift real, working credentials from even the most security-savvy employees in this on-demand webinar. We cover the importance of multi-factor authentication and how these solutions can better protect your business!

Jayaraja JayaramanSharepoint Architect ConsultantCommented:
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
SubsunCommented:
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
Jayaraja JayaramanSharepoint Architect ConsultantCommented:
@Author, Subsun's advice would be lot easier approach and does fit to what exactly you are looking for
0
kuzumAuthor Commented:
thanks guys,

@subsun- DO I need to run this on AD or Exchange server or simply on my Windows 7 machine?
0
kuzumAuthor Commented:
I strugling with this pit, I'm trying to do this on DC server with my admin account ?
task-scheduler.PNG
0
Jayaraja JayaramanSharepoint Architect ConsultantCommented:
i dont think import module active directory is available on windows 7
0
Jayaraja JayaramanSharepoint Architect ConsultantCommented:
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
SubsunCommented:
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
kuzumAuthor Commented:
@Jayaraja Jayaraman - adding accounts is greyed out on me. I don't think you can manage local policies on GCs ?
0
Jayaraja JayaramanSharepoint Architect ConsultantCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kuzumAuthor Commented:
so I am guessing I can do the whole process on GC server with no issue instead?
0
kuzumAuthor Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.