Running Exchange Powershell script from a batch file: command doesn't work. What's wrong with my syntax?
Try this (added dotsourcing of the second script):
PowerShell.exe -noexit -command ". 'C:\Program Files\Microsoft\Exchange Server\V15\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; . 'C:\Program Files\Microsoft\Exchange Server\V15\bin\enablesentforshared.ps1'"
Related videos on Youtube
![Daniel](https://i.stack.imgur.com/NNmnT.png?s=256&g=1)
Daniel
Updated on September 18, 2022Comments
-
Daniel almost 2 years
Coming from this question here: How to create a script for Exchange Powershell to modify settings for all shared mailboxes?
Here is my Powershell script (scroll right to see all):
$Mailboxes = Get-Mailbox -RecipientTypeDetails SharedMailbox Foreach ($Mailbox in $Mailboxes) { Set-Mailbox $Mailbox -MessageCopyForSentAsEnabled $True -MessageCopyForSendOnBehalfEnabled $True }
It works fine on its own if launched from an Exchange Management Shell window.
But I want to launch it from a batch file, so I found the following Microsoft page: https://technet.microsoft.com/en-us/library/bb123798%28v=exchg.150%29.aspx?f=255&MSPPError=-2147217396#RunScript
Following the guide there, I created this batch file (scroll right to see all):
PowerShell.exe -noexit -command ". 'C:\Program Files\Microsoft\Exchange Server\V15\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; 'C:\Program Files\Microsoft\Exchange Server\V15\bin\enablesentforshared.ps1'"
One thing I added which was not specified in the instructions is single quotes around the full path to the script I want to run. Without the single quotes I was getting an error about
C:\Program
not being a recognized cmdlet or script.Anyway, when I run this batch file, the terminal window opens, then changes to a Powershell window, and the path name to the script gets printed out to the screen, but it prints out BEFORE the
[PS]
prompt, and nothing actually gets executed.This is what my Powershell window looks like after executing the batch file:
VERBOSE: Connecting to exchangeserver.domain.com VERBOSE: Connected to exchangeserver.domain.com C:\Program Files\Microsoft\Exchange Server\V15\bin\enablesentforshared.ps1 [PS] C:\Users\Admin.domain>
And there is no indication that the script has actually run.
So what have I got wrong here? I tried changing the
.
to a&
based on some googling I did, but then that gives me an error aboutConnect-ExchangeServer
being an unrecognized command, so I think the.
is closer to being correct. -
Daniel almost 8 yearsYup, that little
.
did the trick...