none
Scheduling a task with powershell RRS feed

  • Question

  • Good morning,

    I have a powershell command that I want to schedule to run on Mondays and send an email to my email group, can someone assist me or provide a good resource to help out.

    The command is to search for uses who have been inactive for 30 days.

    Search-ADAccount -UsersOnly -AccountInactive -TimeSpan 30.00:00:00 | Where {$_.Enabled} | Sort Name | Get-ADUser -Prop DisplayName | Select Name,DisplayName | Out-File users.txt


    Chad

    Tuesday, February 18, 2014 4:40 PM

Answers

  • Hi Chad,

    You can use Send-MailMessage in your script to send the email:

    http://ss64.com/ps/send-mailmessage.html

    Here's one tutorial on setting up a scheduled task to run a PowerShell script (there's many more like this out there, this is just one example):

    http://www.sysadminsblog.com/microsoft/scheduling-powershell-scripts/


    Don't retire TechNet! - (Don't give up yet - 12,700+ strong and growing)

    • Marked as answer by Chadness1 Tuesday, February 18, 2014 5:47 PM
    Tuesday, February 18, 2014 5:03 PM
  • $body=Search-ADAccount -UsersOnly -AccountInactive -TimeSpan 30:0:0:0|
          ?{$_.Lastlogondate} |
          select name,lastlogondate |
          format-Table -auto | out-string
    Send-MailMessage -to -from -subject -body $body

    See also Register-ScheduledTask


    ¯\_(ツ)_/¯



    • Edited by jrv Tuesday, February 18, 2014 5:20 PM
    • Marked as answer by Chadness1 Tuesday, February 18, 2014 5:47 PM
    Tuesday, February 18, 2014 5:19 PM
  • I ran it in the run box, and this is what the output shows.

    The term 'Search-ADAccount' is not recognized as the name of a cmdlet, function
    , script file, or operable program. Check the spelling of the name, or if a pat
    h was included, verify that the path is correct and try again.
    At C:\temp\lastlogin.ps1:1 char:23
    + $body=Search-ADAccount <<<<  -UsersOnly -AccountInactive -TimeSpan 30.00:00:0
    0 |
        + CategoryInfo          : ObjectNotFound: (Search-ADAccount:String) [], Co
       mmandNotFoundException
        + FullyQualifiedErrorId : CommandNotFoundException

    Send-MailMessage : Cannot validate argument on parameter 'Body'. The argument i
    s null or empty. Supply an argument that is not null or empty and then try the
    command again.
    At C:\temp\lastlogin.ps1:5 char:184
    + send-mailmessage -to "Systems Engineering <chad_campbell@test.com>" -from
    "Inactive Accounts inactive_acounts@test.com" -subject "Accounts Inactive
    for at least 30 days" -body <<<<  $body -smtpServer relay.itserve.com
        + CategoryInfo          : InvalidData: (:) [Send-MailMessage], ParameterBi
       ndingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Power
       Shell.Commands.SendMailMessage


    Chad

    • Marked as answer by Chadness1 Wednesday, February 19, 2014 11:27 AM
    Wednesday, February 19, 2014 11:06 AM

All replies

  • Hi Chad,

    You can use Send-MailMessage in your script to send the email:

    http://ss64.com/ps/send-mailmessage.html

    Here's one tutorial on setting up a scheduled task to run a PowerShell script (there's many more like this out there, this is just one example):

    http://www.sysadminsblog.com/microsoft/scheduling-powershell-scripts/


    Don't retire TechNet! - (Don't give up yet - 12,700+ strong and growing)

    • Marked as answer by Chadness1 Tuesday, February 18, 2014 5:47 PM
    Tuesday, February 18, 2014 5:03 PM
  • $body=Search-ADAccount -UsersOnly -AccountInactive -TimeSpan 30:0:0:0|
          ?{$_.Lastlogondate} |
          select name,lastlogondate |
          format-Table -auto | out-string
    Send-MailMessage -to -from -subject -body $body

    See also Register-ScheduledTask


    ¯\_(ツ)_/¯



    • Edited by jrv Tuesday, February 18, 2014 5:20 PM
    • Marked as answer by Chadness1 Tuesday, February 18, 2014 5:47 PM
    Tuesday, February 18, 2014 5:19 PM
  • I am not able to run the powershell in scheduled task, this is what I have below.

    -Command "&{c:\temp\lastlogin.ps1"}


    Chad

    Wednesday, February 19, 2014 4:13 AM
  • Hi Chad,

    Try using -File "C:\Temp\lastlogin.ps1" instead of -Command etc etc etc. -Command is nice for running individual commands and such, but -File works best if you already have a script saved.


    Don't retire TechNet! - (Don't give up yet - 12,700+ strong and growing)

    Wednesday, February 19, 2014 4:58 AM
  • I ran it in the run box, and this is what the output shows.

    The term 'Search-ADAccount' is not recognized as the name of a cmdlet, function
    , script file, or operable program. Check the spelling of the name, or if a pat
    h was included, verify that the path is correct and try again.
    At C:\temp\lastlogin.ps1:1 char:23
    + $body=Search-ADAccount <<<<  -UsersOnly -AccountInactive -TimeSpan 30.00:00:0
    0 |
        + CategoryInfo          : ObjectNotFound: (Search-ADAccount:String) [], Co
       mmandNotFoundException
        + FullyQualifiedErrorId : CommandNotFoundException

    Send-MailMessage : Cannot validate argument on parameter 'Body'. The argument i
    s null or empty. Supply an argument that is not null or empty and then try the
    command again.
    At C:\temp\lastlogin.ps1:5 char:184
    + send-mailmessage -to "Systems Engineering <chad_campbell@test.com>" -from
    "Inactive Accounts inactive_acounts@test.com" -subject "Accounts Inactive
    for at least 30 days" -body <<<<  $body -smtpServer relay.itserve.com
        + CategoryInfo          : InvalidData: (:) [Send-MailMessage], ParameterBi
       ndingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Power
       Shell.Commands.SendMailMessage


    Chad

    • Marked as answer by Chadness1 Wednesday, February 19, 2014 11:27 AM
    Wednesday, February 19, 2014 11:06 AM
  • Import-Module ActiveDirectory

    at the beginning of the script.


    ¯\_(ツ)_/¯

    Wednesday, February 19, 2014 11:21 AM
  • Thanks that worked.

    Chad

    Wednesday, February 19, 2014 11:28 AM