powershell script task in task manager won't stop

I have a powershell script that I am trying to run from task scheduler. The script looks in the ou "domain user accounts" for the extensionAttribute1 value and then exports the list to a csv. The script works fine when executed from powershell but when running it from task scheduler it never finishes running. The exported csv is created however.

Here is the script-
add-pssnapin Microsoft.Exchange.Management.PowerShell.E2010

$OU='OU=domain user accounts,DC=domain,DC=local'
Get-Mailbox -OrganizationalUnit $OU|
Where {($_.customAttribute1 -eq $ext14)} | export-csv c:\userstest.txt

Open in new window

The task is running as domain\administrator with highest privileges.
It is set to run powershell with the arguments-
-ExecutionPolicy Bypass c:\scripts\exportOWA.ps1

Thanks in advance.
Who is Participating?
rawales2Connect With a Mentor Author Commented:
ok. Changed the arguments to
-NoExit -command ". 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; .  c:\Scripts\exportOWA.ps1"

Open in new window

and it's working now. Apparently you need to connect to the exchange server first even though it's being run on the exchange server. Makes sense.
QlemoBatchelor and DeveloperCommented:
Make sure you manually refresh the Task Scheduler display. It does often not reflect the actual state of tasks.
rawales2Author Commented:
Thanks, but I have been manually refreshing.
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

QlemoConnect With a Mentor Batchelor and DeveloperCommented:
Don't see any reason for that, so you'll need to check several things. I'll assume you are running the task on the same machine as you have run the script manually.

Run the task with your own account, and by adding -NoExit as first switch  - if the script runs and finishs, you should have a powershell prompt now without errors. Close it, and rollback the changes made.

Add another command creating a file to the script, like  dir | export-csv c:\dir.txt, and see if that file is created too.
Is the PowerShell process visible in Task Manager after the task should have finished?

Did you check the task's log?
rawales2Author Commented:
ok. when I add the no exit argument it just brings up powershell with a prompt... I've also tried -NoExit -command ". 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; c:\Scripts\exportOWAps1"

This loads the exchange tools and connects to my exchange server but then just sits..
QlemoBatchelor and DeveloperCommented:
The script is not expected to provide any feedback, so an empty box is ok.
Did you run as your user or as admin?
rawales2Author Commented:
yes. it's all good.
rawales2Author Commented:
Given credit to you for the -NoExit - that was really helpful.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.