Answered by:
Schedule a Powershell Exchange script

Question
-
HI,
I have a powershell script that moves mailboxes in Excvhange 2010. I logon to the exchange server as my admin user and run the script in powershell and it runs fine.
I need to create a scheduled task that runs the script so that we start moving mailboxes out of hours.
Task Settings:
I have entered my admin credentials and "run with highest privileges", "run whether the user is logged on or not" and "do not store password" are set on the task
Program/Script: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Add Arguments: -command “. ‘C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1′; Connect-ExchangeServer -auto; "C:\Installation Software\Powershell\MoveMailboxes\MoveMailbox_Batch.ps1"
The problem is that when the scheduled task is run the script appears to run ok as I have added lines to output text to a log file. This works. However it seems that any exchange related commands are not working.
The start of my powershell script includes the following line
Add-PSSnapIn Microsoft.Exchange.Management.PowerShell.E2010
As a test I have replaced my mailbox move line with the following just to see if it works (It doesn't):
get-exchangeserver ExchServer | out-file -filepath $Logfile
Running the code from within a powershell window I get a file created with info about the exchange server, from the scheduled task the file does not get created.
I have added the following lines of code before and after the exchange line of code and these add text to a file as I expect when the scheduled task is run. The exchange line of code does not appear to run.
write-output "$(get-date): Before Get-ExchangeServer" | out-file -filepath $Emailfile -append
get-exchangeserver ExchServer | out-file -filepath $Logfile
write-output "$(get-date): After Get-ExchangeServer" | out-file -filepath $Emailfile -append
Any help would be greatly appreciated as the first mailbox moves are tonight.
Regards
Matt
Wednesday, October 1, 2014 11:44 AM
Answers
-
Hi,
Thanks for your reply Willard, but I found the answer last night I just didn't have chance to update the thread.
The problem was on the scheduled task I had configured the "do not store password" option which I thought was reasonable.
However, while in despair, I re checked the task again yesterday as I had done many times but then read the crucial piece of information which finally sank in..... "Do not store password. The task will only have access to local computer resources". Yes will only have access to local computer resources. I thought this would be ok, but doing plently of reading on the subject powershell does remote calls to the Exchange system.
As soon as I unticked this, the task worked and all the exchange commands worked a treat.
Job was a goodun
- Marked as answer by Matt Ridley Thursday, October 2, 2014 7:12 AM
Thursday, October 2, 2014 7:12 AM
All replies
-
Hi,
I have slightly changed the Add-Arguments to be:
-command "& {. 'C:\Installation Software\Powershell\MoveMailboxes\MoveMailbox_Batch.ps1' }"
which still runs the powershell script but not the exchange section.
I have now also wrapped my script between the following two lines of code so that I can see what is happening when the scheduled task is running:
start-transcript -path $ScriptLog -append
<My code goes here>
stop-transcript
So doing the above I do seem to be getting an error see below:
"Get-ExchangeServer : Value cannot be null.
Parameter name: serverSettings
At C:\Installation Software\Powershell\MoveMailboxes\MoveMailbox_Batch.ps1:34 c
har:19
+ get-exchangeserver <<<< Exchserver | out-file -filepath $Logfile
+ CategoryInfo : NotSpecified: (:) [Get-ExchangeServer], Argument
NullException
+ FullyQualifiedErrorId : System.ArgumentNullException,Microsoft.Exchange"Wednesday, October 1, 2014 12:12 PM -
Hi,
As an update it seems that whatever Exchange command I enter I get a similar error.
So for
Get-exchangeserver
I get:
Get-ExchangeServer : Value cannot be null.
Parameter name: serverSettings
At C:\Installation Software\Powershell\MoveMailboxes\MoveMailbox_Batch.ps1:35 c
har:19
+ get-exchangeserver <<<<
+ CategoryInfo : NotSpecified: (:) [Get-ExchangeServer], Argument
NullException
+ FullyQualifiedErrorId : System.ArgumentNullException,Microsoft.Exchange.Now from the powershell prompt I can enter the above command no problem and it will show me a list of exchange servers.
Regards
Matt
Wednesday, October 1, 2014 12:25 PM -
What happens if you run "Get-ExCommand" in your script?Wednesday, October 1, 2014 6:07 PM
-
Hi,
Thanks for your reply Willard, but I found the answer last night I just didn't have chance to update the thread.
The problem was on the scheduled task I had configured the "do not store password" option which I thought was reasonable.
However, while in despair, I re checked the task again yesterday as I had done many times but then read the crucial piece of information which finally sank in..... "Do not store password. The task will only have access to local computer resources". Yes will only have access to local computer resources. I thought this would be ok, but doing plently of reading on the subject powershell does remote calls to the Exchange system.
As soon as I unticked this, the task worked and all the exchange commands worked a treat.
Job was a goodun
- Marked as answer by Matt Ridley Thursday, October 2, 2014 7:12 AM
Thursday, October 2, 2014 7:12 AM -