Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a PowerShell script that works when entered directly into the PowerShell. The PowerShell program is run with the shorcut:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\bin\exshell.psc1" -noexit -command ". 'C:\Program Files\Microsoft\Exchange Server\bin\Exchange.ps1'"

I have tried about 10 different ways in Windows Task Scheduler to first open the shortcut above and then run my saved script file but it won't work. It hangs on 'Running'.

The script is located here:

Z:\Exchange PowerShell\MailboxesApproachingOverQuota.ps1

Note that script content can be copied and pasted into the PowerShell shortcut above and it runs absolutely fine and emails me the output as it should.

Note that the PowerShell instance I use is from the shortcut shown above which already has arguments in it - hence why I'm stumped what to put where in Task Scheduler.

Any help please?

Thanks :)


EDIT When I use the following directly in CMD:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\bin\exshell.psc1" -noexit -command ". 'C:\Program Files\Microsoft\Exchange Server\bin\Exchange.ps1'; &'Z:\Exchange Powershell\MailboxesApproachingOverQuota.ps1'"

I get: http://tinypic.com/r/n1fkmg/8

When I use the following:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\bin\exshell.psc1" -noexit -command ". 'C:\Program Files\Microsoft\Exchange Server\bin\Exchange.ps1'; &'Z:\Exchange Powershell\MailboxesApproachingOverQuota.ps1'"

I get: http://tinypic.com/r/5bbac9/8

Running this:

powershell -PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\bin\exshell.psc1" -noexit -exec bypass -command ". 'C:\Program Files\Microsoft\Exchange Server\bin\Exchange.ps1'"

Opens the Exchange Management Shell fine: http://tinypic.com/r/2dglulf/8

share|improve this question
    
I assume you mean Task Scheduler... If there are spaces in paths, you need quote marks around the path. –  Davidw Feb 17 at 23:50
    
Your assumption would be correct. Ok I'll try that shortly. –  Logan Feb 18 at 0:36
    
Have you tried actually running the whole command from a command prompt and seeing what happens? –  Massimo Feb 18 at 0:37
    
No because I'm unsure which is the correct way to run it using the two pieces of information provided above. As the shortcut to the PowerShell I use contains arguments I am confused as to the order and format of what I'm trying to do in Task Scheduler –  Logan Feb 18 at 0:40
    
Task Scheduler has its own quirks (especially with spaces and quotes); try first to get the full command worked out, then scheduling it. –  Massimo Feb 18 at 10:16

3 Answers 3

up vote 4 down vote accepted

First, let me tell you how you would run the script from a task:

Program/script: 
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

Add arguments (optional): 
-PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\bin\exshell.psc1" -command ". 'C:\Program Files\Microsoft\Exchange Server\bin\Exchange.ps1'; &'Z:\Exchange Powershell\MailboxesApproachingOverQuota.ps1'"

Also note that, the user id you are using to run the script may need to be granted permissions on the local server.

update: Removed -noexit

share|improve this answer
    
Hi there, the environment was already setup before I came here a week or so ago. There is a shortcut to Exchange Management Shell using the first piece of code in the initial question at the top. The second piece of code is the script I want to run. The script contains the exact text I put into the Exchange Management Shell box and it runs fine. I just need to know how to put all of this into an event that can run on a schedule using Task Scheduler. –  Logan Feb 18 at 1:05
    
updating answer. –  Adil Hindistan Feb 18 at 3:20
    
Hey Adil, I still get the same thing, its status changes to Running but nothing happens - assuming I should see the PowerShell box pop up? I've left it for a good 4-5 minutes and nothing, stays on Running. I also added in '-ExecutionPolicy Bypass' but same result - nothing. Any idea's? –  Logan Feb 18 at 3:46
    
MORE Using Program/script: "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" and this in Add arguments (optional): "-ExecutionPolicy Bypass -PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\bin\exshell.psc1" -noexit -command ". 'C:\Program Files\Microsoft\Exchange Server\bin\Exchange.ps1'; &'Z:\Exchange Powershell\MailboxesApproachingOverQuota.ps1'"" the task will complete but nothing appears on screen and script obviously doesn't run as I receive no email. –  Logan Feb 18 at 3:53
    
Logon is that the exact line? It should have closed like '" not with "" –  Adil Hindistan Feb 18 at 4:02

I'm guessing this script isn't signed. Have you enabled execution of unsigned scripts using the Set-ExecutionPolicy -ExecutionPolicy Unrestricted cmdlet?

share|improve this answer
    
Agreed. Pasting the script into PowerShell is a completely different thing from actually invoking the script file, as long as the execution policy is concerned. –  Massimo Feb 18 at 0:36
    
@Massimo - Thanks for the edit. Typing too fast tonight... –  Evan Anderson Feb 18 at 0:38
    
No the script isn't signed and the current policy is restricted. What do I put in the Program/Script field and in the Add arguments (optional) field though? As the PowerShell I use has arguments in the shortcut. Outlined above. Also unsure where I put what you mentioned above, is that a one off to change the policy or is that added into my script or is it added as an argument when calling the script? Extremely new to this and learning as I go. Thank you. –  Logan Feb 18 at 0:40
    
Call powershell.exe with -ExecutionPolicy Bypass to override the restricted execution policy. –  jscott Feb 18 at 1:08
    
Using this in Program/script: "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" and this in Add arguments (optional): "-ExecutionPolicy Bypass -PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\bin\exshell.psc1" -noexit -command ". 'C:\Program Files\Microsoft\Exchange Server\bin\Exchange.ps1'; &'Z:\Exchange Powershell\MailboxesApproachingOverQuota.ps1'"" the task will run and complete but nothing appears on screen and the script obviously doesn't run as I receive no email. –  Logan Feb 18 at 3:52

I was having the same problem and found the easiest way around it was to call the command via a bat file. I could run the entire command from a command line but if I tried to use the scheduled task it would not work no matter what I did.

Single line batch file fixed it.

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

I set the transcript in my PS script so I could see if it completed and what commands it ran. I also ran the get-mailboxparmission -identity username:\calendar to confirm the changes were reflected.

Cheers

Pete

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.