locked
Scheduled Task in Powershell RRS feed

  • Question

  • From a remote pc with the 2010 exch mgmt console, I ran the following command to schedule a task:
    schtasks /create /tn "MoveMailboxes" /tr "c:\mailboxmoves.bat" /sc once /st 12:00:00 /ru System

    When I run this command it says the scheduled task has successfully been created.  The task does run, but when I type get-moverequest it does not show the progress of my scheduled task.  When I look in Windows --> Task Scheduler it has an 0xFF error and the mailboxes do not move.  Is this a permissions issue, or something else? I have no problem when I paste the batch file in powershell.
    Thursday, February 11, 2010 7:37 PM

Answers

  • Schedule the task using PowerShell.exe and pass your code as a parameter. For example, your scheduled task action would look something like this:

    c:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe -command ". 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; Import-CSV C:\importusers.csv | New-MoveRequest -TargetDatabase 'ISS Mailbox DB'"

    or you could put the code in a .ps1 file and call it that way:

    c:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe -command ". 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; c:\movemailbox.ps1"
    Thursday, February 11, 2010 10:19 PM

All replies

  • Hi,

    Could be a permission issue but also a script error so please post the content of the batch file.

    Regards,

    Johan
    Exchange-blog: www.johanveldhuis.nl
    Thursday, February 11, 2010 8:18 PM
  • Hi,

    Could be a permission issue but also a script error so please post the content of the batch file.

    Regards,

    Johan
    Exchange-blog: www.johanveldhuis.nl

    Here is the content of my batch file.

    Import-CSV "C:\importusers.csv" | New-MoveRequest -TargetDatabase "ISS Mailbox DB"

    My csv file is one column called identity (AD username).
    Thursday, February 11, 2010 8:38 PM
  • I believe that you will need to ensure that the contents of the batch file is run through Exchange Management Shell.  Where is this running currently?  It won't work in cmd...
    Thursday, February 11, 2010 9:16 PM
  • If I paste the contents of the batch file into Exchange Manangement Shell it works.  How would I ensure the contents go through the EMS when I schedule the task?
    Thursday, February 11, 2010 9:23 PM
  • Schedule the task using PowerShell.exe and pass your code as a parameter. For example, your scheduled task action would look something like this:

    c:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe -command ". 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; Import-CSV C:\importusers.csv | New-MoveRequest -TargetDatabase 'ISS Mailbox DB'"

    or you could put the code in a .ps1 file and call it that way:

    c:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe -command ". 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; c:\movemailbox.ps1"
    Thursday, February 11, 2010 10:19 PM
  • Schedule the task using PowerShell.exe and pass your code as a parameter. For example, your scheduled task action would look something like this:

    c:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe -command ". 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; Import-CSV C:\importusers.csv | New-MoveRequest -TargetDatabase 'ISS Mailbox DB'"

    or you could put the code in a .ps1 file and call it that way:

    c:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe -command ". 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; c:\movemailbox.ps1"

    Aha (The light finally dawns)!  The first option completely worked.  I used Windows Task Scheduler for powershell.exe command and then used the rest as a parameter.  Thanks so much for getting back to me.

    Regards,
    Kelli
    Friday, February 12, 2010 2:01 PM