1

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

Logan
  • 27
  • 1
  • 2
  • 8
  • I assume you mean Task Scheduler... If there are spaces in paths, you need quote marks around the path. – Davidw Feb 17 '14 at 23:50
  • Your assumption would be correct. Ok I'll try that shortly. – Logan Feb 18 '14 at 00:36
  • Have you tried actually running the whole command from a command prompt and seeing what happens? – Massimo Feb 18 '14 at 00: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 '14 at 00: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 '14 at 10:16
  • Please check question update at the bottom, any ideas? T hanks – Logan Feb 18 '14 at 22:16
  • The -bypass parameter for Set-ExecutionPolicy is missing the "-" before bypass. Also, I would add at the end of the script itself, a -verbose parameter, which will actually show you what it's doing. – Davidw Feb 19 '14 at 01:34
  • Still doesn't work, produces the same result, using: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -SetExecutionPolicy -Bypass -PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\bin\exshell.psc1" -noexit -command ". 'C:\Program Files\Microsoft\ExchangeServer\bin\Exchange.ps1'; &'Z:\Exchange Powershell\MailboxesApproachingOverQuota.ps1'" -verbose – Logan Feb 19 '14 at 01:49
  • The -verbose needs to be inside the script itself. – Davidw Feb 19 '14 at 02:32

4 Answers4

5

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

Massimo
  • 70,200
  • 57
  • 200
  • 323
Evan Anderson
  • 141,881
  • 20
  • 196
  • 331
  • 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 '14 at 00:36
  • @Massimo - Thanks for the edit. Typing too fast tonight... – Evan Anderson Feb 18 '14 at 00: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 '14 at 00:40
  • Call powershell.exe with `-ExecutionPolicy Bypass` to override the restricted execution policy. – jscott Feb 18 '14 at 01: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 '14 at 03:52
5

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

Adil Hindistan
  • 419
  • 4
  • 8
  • 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 '14 at 01:05
  • updating answer. – Adil Hindistan Feb 18 '14 at 03: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 '14 at 03: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 '14 at 03:53
  • Logon is that the exact line? It should have closed like '" not with "" – Adil Hindistan Feb 18 '14 at 04:02
  • Please note I have wrapped each string in quotation marks so minus 1 quotation mark off of the end of each of the two strings shown above. – Logan Feb 18 '14 at 04:03
  • C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe **AND** -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'" **WERE USED** – Logan Feb 18 '14 at 04:04
  • forget about scheduled tasks now, and do that from command line... does it work? – Adil Hindistan Feb 18 '14 at 04:06
  • http://tinypic.com/r/5bbac9/8 – Logan Feb 18 '14 at 04:09
  • let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/13100/discussion-between-adil-hindistan-and-logan) – Adil Hindistan Feb 18 '14 at 04:18
  • Chat doesn't work for me it keeps saying that I need to login to talk - I'm already logged in. Also mentions I need 20 reputation which I don't think I have? – Logan Feb 18 '14 at 19:27
  • hmm, are you copy pasting? could you try typing it to see if you have some weird space somewhere? What happens if you just have the code before ; does it work then? – Adil Hindistan Feb 18 '14 at 19:48
  • Yes copy pasting but I have double checked it all and it's fine no weird spaces or anything. I tried it without that execution policy bit and I get this: http://tinypic.com/r/n1fkmg/8 (getting warmer?) – Logan Feb 18 '14 at 21:47
  • is this working? powershell -PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\bin\exshell.psc1" -noexit -exec bypass -command ". 'C:\Program Files\Microsoft\Exchange Server\bin\Exchange.ps1' " – Adil Hindistan Feb 19 '14 at 01:29
  • Yes that appears to open Exchange Management Shell, see: http://tinypic.com/r/2dglulf/8 – Logan Feb 19 '14 at 01:53
  • Ok, now can you create a simple script (let it just print something on the screen), save it in a local folder (say c:\temp\test.ps1) then add it to that like you tried before: powershell -PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\bin\exshell.psc1" -noexit -exec bypass -command ". 'C:\Program Files\Microsoft\Exchange Server\bin\Exchange.ps1' ; &'c:\temp\test.ps1'" does that work? – Adil Hindistan Feb 19 '14 at 02:00
  • Nope no luck didn't work, the last piece threw it off, see: http://tinypic.com/r/2njxdtw/8 – Logan Feb 19 '14 at 02:05
  • huh!, but now that you are in the shell, it works if you just type &'c:\temp\test.ps1' ? – Adil Hindistan Feb 19 '14 at 02:09
  • Nope :-/ http://tinypic.com/r/15hjsz/8 – Logan Feb 19 '14 at 02:26
  • You did create the powershell file C:\temp\test.ps1, right? – Adil Hindistan Feb 19 '14 at 02:34
  • so, we know that script works; the syntax works; but if you put it into Z: drive it does not. Is that b/c you are using a different user acc for scheduler? If you are, for example, using NT System account; it wont have access to 'z:' drive, as that's a network drive your user account is mapping. In terms of what you need to put to scheduler; we already answered that. Everything starting with -PSconsole goes to arguments... To test, I suggest you use your own account and pwd while testing – Adil Hindistan Feb 19 '14 at 03:11
  • I moved the original ps1 I created to C:\ drive and now from CMD it works.... I had it in a network drive I assumed that would work. So it's working from CMD using: powershell -PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\bin\exshell.psc1" -noexit -exec bypass -command ". 'C:\Program Files\Microsoft\Exchange Server\bin\Exchange.ps1' ; &'c:\temp\MailboxesApproachingOverQuota.ps1'" - The question now is what do I put into Task Scheduler? Oh and I'd also like the CMD box to close after running instead of staying open. Thanks. – Logan Feb 19 '14 at 03:13
  • Ok I'm using 'powershell' in the program field and the rest in the arguments field. I had to remove -noexit otherwise the task wouldn't stop running. It now works fine, emails me, and status changes to ready shortly after. Looks like I'm all sorted - thank you very very much! If you want to update your answer I can mark it. Thanks. – Logan Feb 19 '14 at 03:18
  • Well, my answer was correct all along Logan :) anyway I will mention Z was probably not accessible – Adil Hindistan Feb 19 '14 at 03:25
0

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

  • Keep noted that the trasncript option might no longer working on an OS higher then Windows 2008 R2 when using inside a PS1 file and not directly entered during a normal session. More infos can be found [here](https://connect.microsoft.com/PowerShell/feedback/details/483975/start-transcript-does-not-echo-commands-when-used-in-a-script). – BastianW Jul 27 '17 at 15:33
0

Simple script file work through task scheduler. When I use the -Sendmail:$true parameter, then this is working. I made a .bat file, and run that file instead of the Powershell script in task scheduler. This .bat file working on my Exchange 2016.

PowerShell.exe -noexit -command ". 'C:\Program Files\Microsoft\Exchange Server\V15\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; . C:\Script\Get-ExchangeEnvironmentReport.ps1 -HTMLReport C:\Script\Get-exchangeReport.html -SendMail:$true -MailFrom:Admin@domain.com -MailTo:user1@domain.com -MailServer:mail.domain.com"
Cory Knutson
  • 1,876
  • 13
  • 20