none
Cmdlet Get-Mailbox gives empty result when running in script in Task Scheduler RRS feed

  • Question

  • Hello,

    I would like to collect some mailbox information using the PS script running from Task Scheduler.
    The script contains a line (truncated):

    $MBS=Get-Mailbox | Out-String

    the result of which is used to send as an attachment.

    If the script is running from the command line (as administrator), everything works as expected.
    If the script runs as a scheduled task, it looks like the Get-Mailbox passes a blank result.

    The script is simple:

    Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn;

    Function Try-SendMail (...)

    $MBS = Get-Mailbox | Out-String

    .
    .
    .

    $Attachments=@(

    $([System.Net.Mail.Attachment]::CreateAttachmentFromString($MBS, "MBStatistic.txt"))

    )

    Try-SendMail ...


    Task Scheduler Task:

    Action:

    %SystemRoot%\System32\WindowsPowerShell\v1.0\ powershell.exe

    Argument:

    -NonInteractive -WindowStyle Hidden -command ". 'C:\Program Files\Microsoft\Exchange Server\V15\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; C:\_\Skripts\SendMBStat.ps1"

    General:

    Run as Administrator, Run if user is logged on or not and Run with the highest privileges.

    Any suggestions? I do not know what else...

    Thank you.


    Petr


    • Edited by petr coja Monday, January 7, 2019 3:08 PM
    Monday, January 7, 2019 1:46 PM

Answers

All replies

  • Are your mailboxes stored on a on-prem Exchange mailbox server or are they in the Office 365 cloud? Also, the account you are using to run the schedule task - does it have permissions set so it can access the mailboxes in Exchange?
    Monday, January 7, 2019 3:13 PM
  • We are using on-premise Exchange mailbox server.

    There is the same account used to access the mailboxes from commandline or scheduled task.


    Petr

    Monday, January 7, 2019 6:33 PM
  • Hi Petr,

    If the script has been verified from EMS, the next step is to verify the script can run from the RemoteExchange.ps1 environment using an elevated command prompt. Here's an example for an Exchange 2013 command where Exchange is installed in the default location on C: and the output is piped to a local file.

    C:\Windows\System32\WindowsPowerShell\v1.0\Powershell.exe  -noExit -Command ".'C:\Program Files\Microsoft\Exchange Server\V15\Bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; <script>"

    Furthermore, you can check if there is related error logs in Event Viewer. If any, you can post it here.

    1. Open Event Viewer.
    2. Navigate to Applications and Services Logs > Microsoft > Windows > TaskScheduler/Operational.  If the task scheduler reports a result code of 0, that means the task ran successfully. 

    For task creation steps and more troubleshooting methods, please refer to How to use Task Scheduler for Exchange scripts.

    Regards,

    Dawn Zhou


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnsf@microsoft.com.

    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Wednesday, January 9, 2019 8:06 AM
    Moderator
  • Hi,

    I apologize for the delay.

    When I wrote that the script was tested from the command line, I meant the RemoteExchange.ps1 enviroment (elevated CL of course), sorry for the inaccuracy.
    A scheduled task, triggered manually or in a planned case, always ends with output 0.

    The problem is not a run-time error of scheduled task, but a different behavior in an RemoteExchange.ps1 enviroment using an elevated CL or running it via Task Scheduler.

    To complement, we use Exchange 2016.

    Thanks for your attention.


    Petr

    Thursday, January 10, 2019 12:49 PM
  • Hi,

    I apologize for the delay.

    When I wrote that the script was tested from the command line, I meant the RemoteExchange.ps1 enviroment (elevated CL of course), sorry for the inaccuracy.
    A scheduled task, triggered manually or in a planned case, always ends with output 0.

    The problem is not a run-time error of scheduled task, but a different behavior in an RemoteExchange.ps1 enviroment using an elevated CL or running it via Task Scheduler.

    To complement, we use Exchange 2016.

    Thanks for your attention.


    Petr

    This is not supported with Exch 2013 and beyond

    Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn;

    You need to remote to Exchange in your script

    https://docs.microsoft.com/en-us/powershell/exchange/exchange-server/connect-to-exchange-servers-using-remote-powershell?view=exchange-ps

    Thursday, January 10, 2019 1:59 PM
    Moderator
  • Thank you, I try it as soon as possible.

    Petr

    Thursday, January 10, 2019 3:08 PM
  • The problem was solved, it was located between chair and keyboard...
    The scheduled task was run under the local administrator's account, but the domain administrator needed to use it. All this time I overlooked this setting.
    After changing the scheduled task setting, the script and task work as expected.
    Thanks to everyone for their efforts.



    Petr

    Friday, January 11, 2019 7:31 PM
  • The problem was solved, it was located between chair and keyboard...
    The scheduled task was run under the local administrator's account, but the domain administrator needed to use it. All this time I overlooked this setting.
    After changing the scheduled task setting, the script and task work as expected.
    Thanks to everyone for their efforts.



    Petr

    and you changed the connection to use remoting instead of the PSsnapin right? :) 

    Friday, January 11, 2019 7:58 PM
    Moderator
  • The problem was solved, it was located between chair and keyboard...
    The scheduled task was run under the local administrator's account, but the domain administrator needed to use it. All this time I overlooked this setting.
    After changing the scheduled task setting, the script and task work as expected.
    Thanks to everyone for their efforts.




    and you changed the connection to use remoting instead of the PSsnapin right? :) 

    Remote access setup or PSsnapin was no longer needed.

    Petr

    Friday, January 11, 2019 8:11 PM
  • The problem was solved, it was located between chair and keyboard...
    The scheduled task was run under the local administrator's account, but the domain administrator needed to use it. All this time I overlooked this setting.
    After changing the scheduled task setting, the script and task work as expected.
    Thanks to everyone for their efforts.




    and you changed the connection to use remoting instead of the PSsnapin right? :) 

    Remote access setup or PSsnapin was no longer needed.

    Petr

    hmm. how are you running get-mailbox then?
    Friday, January 11, 2019 9:14 PM
    Moderator
  • Script (and task) are running on the server with Exchange server.

    Petr

    Friday, January 11, 2019 9:36 PM
  • Script (and task) are running on the server with Exchange server.

    Petr

    Right, but you still have remote to that server, even if you are on it directly or running a script on it. 

    https://docs.microsoft.com/en-us/exchange/release-notes-for-exchange-2013-exchange-2013-help

     Warning

    Loading the Microsoft.Exchange.Management.PowerShell.SnapIn Windows PowerShell snap-in and running cmdlets other than the *-TransportAgent cmdlets is not supported and may result in irreparable damage to your Exchange deployment.

    • Marked as answer by petr coja Saturday, January 12, 2019 11:19 AM
    Friday, January 11, 2019 9:41 PM
    Moderator
  • Thanks for the warning and explanation, I'm a newbie in Exchange and sometimes I'm not in the context.


    Petr



    • Edited by petr coja Saturday, January 12, 2019 11:38 AM
    Saturday, January 12, 2019 11:19 AM