locked
Powershell script windows scheduling error RRS feed

  • Question

  • Hello,

    Trying to schedule the powershell script to get the mailbox count on monthly bases. But when i schedule the script, it throws me an error in in the command prompt.

     

    Powershell Script "c:\scripts\exch1.ps1"

    $smtpServer = "server.testlab.local"
    $smtpFrom = "mahess@testlab123.local"
    $smtpTo = "jeevananthans@testlab.local"
    $messageSubject = "TestLab Mailboxcount"

    $message = New-Object System.Net.Mail.MailMessage $smtpfrom, $smtpto
    $message.Subject = $messageSubject
    $message.IsBodyHTML = $true

    $con = Get-Mailbox | Group-Object -Property:ServerName | Select-Object

    name,count | convertto-html

    $message.Body = $con
    $smtp = New-Object Net.Mail.SmtpClient($smtpServer)
    $smtp.Send($message)

     

    Schedule Task Command:


    C:\Scripts>Test.bat


    C:\Scripts>powershell -command "& 'c:\scripts\exch1.ps1' "

     

    Error Message:

    C:\Scripts>powershell -command "& 'c:\scripts\exch1.ps1' "
    The term 'Get-Mailbox' is not recognized as a cmdlet, function, operable progra
    m, or script file. Verify the term and try again.
    At C:\scripts\exch1.ps1:10 char:19
    + $con = Get-Mailbox  <<<< | Group-Object -Property:ServerName | Select-Object
    name,count | convertto-html

     

    why this get-mailbox is not recognized, but in shell its running fine with out any error.

     

    Regards

    Jeevananthan S


    Thanks& Regards Jeevananthan S
    Sunday, December 25, 2011 5:38 PM

Answers

  • You should point to Exchange cmdlets module.

    In Exchange 2007 you should add proper file

    PowerShell.exe -PSConsoleFile "C:\Program Files\Exchsrvr\Bin\ExShell.psc1" -Command ". 'c:\scripts\exch1.ps1'"

    In Exchange 2010 you should add to the script cmdlet:

    add-pssnapin Microsoft.Exchange.Management.PowerShell.E2010


    Regards, Konrad Sagala, MCT, MCSE+M, MCITP: Exchange 2007/2010
    • Marked as answer by Jeevananthan S Monday, December 26, 2011 4:54 AM
    Sunday, December 25, 2011 9:07 PM

All replies

  • You should point to Exchange cmdlets module.

    In Exchange 2007 you should add proper file

    PowerShell.exe -PSConsoleFile "C:\Program Files\Exchsrvr\Bin\ExShell.psc1" -Command ". 'c:\scripts\exch1.ps1'"

    In Exchange 2010 you should add to the script cmdlet:

    add-pssnapin Microsoft.Exchange.Management.PowerShell.E2010


    Regards, Konrad Sagala, MCT, MCSE+M, MCITP: Exchange 2007/2010
    • Marked as answer by Jeevananthan S Monday, December 26, 2011 4:54 AM
    Sunday, December 25, 2011 9:07 PM
  • Hi Konrad Sagala,

     

    its working for me ...

     

    Thanks


    Thanks& Regards Jeevananthan S
    Monday, December 26, 2011 4:53 AM