Avatar of khanfe
khanfe
 asked on

Exchange Management Shell Query

Hi Folks,

I'm very new to Exchange, so please excuse if there are obvious mistakes in my approach.

We are currently running Exchange 2010 in our Organisation and I need to create a Scheduled Task to run an Exchange query.  I've tried following the article below to create the task, however I wanted to test the query from the Exchange Management Shell first to see if it works:

http://www.msexchange.org/kbase/ExchangeServerTips/ExchangeServer2013/Powershell/scheduling-exchange-powershell-task.html

The query is specified as part of an Argument in the Scheduled Task as below:

-version 2.0 -NonInteractive -WindowStyle Hidden -command ". 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; Set-MailboxJunkEmailConfiguration “feroz.khan” -TrustedSendersAndDomains @{Add="grey.black@def21.com"}"

Open in new window


Should I be able to run the above from the Exchange Management Shell? I ask because I am seeing the following error:

Missing expression after unary operator '-'.
At line:1 char:2
+ - <<<< version 2.0 -NonInteractive -WindowStyle Hidden -command ". 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect
-ExchangeServer -auto; Get-Mailbox -Server slkbrhmbx01 | Select DisplayName,RecipientLimits | Export-CSV C:\Output\MaximumRecipient-Limits.csv -NoTypeInformation"
    + CategoryInfo          : ParserError: (-:String) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingExpressionAfterOperator
ExchangeShell Scripting

Avatar of undefined
Last Comment
khanfe

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Thomas Wheeler

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
khanfe

ASKER
Okay, I attempted to run the full command (as below) from a command prompt first and also from Powershell separately on the mailbox server:

-version 2.0 -NonInteractive -WindowStyle Hidden -command ". 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; Set-MailboxJunkEmailConfiguration “feroz.khan” -TrustedSendersAndDomains @{Add="grey.black@def21.com"}"

Command Prompt response:
=======================
'-version' is not recognized as an internal or external command,
operable program or batch file.

Powershell response:
=================
Missing expression after unary operator '-'.
At line:1 char:2
+ - <<<< version 2.0 -NonInteractive -WindowStyle Hidden -command ". 'C:\Program Files\Microsoft\Exchange Server\V14\bi
n\RemoteExchange.ps1'; Connect-ExchangeServer -auto; Set-MailboxJunkEmailConfiguration "feroz.khan" -TrustedSendersAndD
omains @{Add="grey.black@def21.com"}"
    + CategoryInfo          : ParserError: (-:String) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingExpressionAfterOperator
Thomas Wheeler

In the cmd did you type powershell.exe -version 2.0 .... ? See the task scheduler runs via a cmd so you command is starting a powershell from a cmd with the -version 2.0 option. Is that making sense?
Thomas Wheeler

It looks like you are missing the powershell.exe part of the command
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Thomas Wheeler

The actual exchange powershell command is

Set-MailboxJunkEmailConfiguration “feroz.khan” -TrustedSendersAndDomains @{Add="grey.black@def21.com"}"

The first part just starts powershell from a cmd prompt and runs the remote exchange powershell script. This loads the exchange module so you can run the cmd. It's like opening the exchange management console on the server
khanfe

ASKER
Exactly, so I can run the following command directly from the Exchange Management Shell successfully

Set-MailboxJunkEmailConfiguration “feroz.khan” -TrustedSendersAndDomains @{Add="grey.black@def21.com"}"

However when I run the following from an elevated Command Prompt with the powershell.exe included:

powershell.exe -version 2.0 -NonInteractive -WindowStyle Hidden -command ". 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; Set-MailboxJunkEmailConfiguration “feroz.khan” -TrustedSendersAndDomains @{Add="grey.black@def21.com"}"

The command prompt disappears?
Thomas Wheeler

Yes because the command is saying to hide the window and the window is noninteractive. If you want to see it remove those two options
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Thomas Wheeler

The reason for that is when you have a scheduled task you do not want it popping up windows when it runs
khanfe

ASKER
Thanks for your assistance! - I seem to be getting a little further.

Just as you mentioned, it starts powershell from a cmd prompt and runs the remote exchange powershell script.  It did however come back with the following errors?

SD1.jpgSD2.jpgSD3.jpg
Qlemo

Execution of parts of the script  are denied because they are not signed, and execution policy requires that. So we have to bypass the policy (no restrictions).
powershell.exe -version 2.0 -NonInteractive -WindowStyle Hidden -ExecutionPolicy ByPass -command # and so on ...

Open in new window

All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
khanfe

ASKER
Thanks Qlemo!  That resolved that issue.

Would anyone know why it is returning with the following error, please?

SD4.jpg
Many thanks.
SOLUTION
Qlemo

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
khanfe

ASKER
Thanks QLemo - that all works now!