I would like to run powershell exchange scripts on the mid server. How would I go about doing this?

Damion McLaren
Tera Contributor

I would like to be able to write a step in my mid server script file to login to exchange online (Connect-ExchangeOnline) and run a simple line of powershell.

ie Add-MailboxFolderPermission -Identity john.doe@123.com ...........

From what I can tell, in the mid server script file, I would need a line of code to login to powershell exchange and then a line of code to run the above command.  The following post shows one way to run powershell exchange commands on the mid-server but was hoping there was a less tedious way of accomplishing this:

https://community.servicenow.com/community?id=community_question&sys_id=3e9d0f2ddb9cdbc01dcaf3231f9619f9

4 REPLIES 4

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi,

The following steps using Orchestration activity pack (different license required) seems to be doing something similar.

https://docs.servicenow.com/bundle/sandiego-servicenow-platform/page/product/orchestration/reference...

Following thread has a sample not using Orchestration activity pack. It's not using credentials in the script but it credentials can be hardcoded into the script, it offers a simpler solution.

https://community.servicenow.com/community?id=community_question&sys_id=88182169db4474101cd8a345ca96...

https://community.servicenow.com/community?id=community_question&sys_id=5f034fa1dbd8dbc01dcaf3231f96...

If the question is about writing a powershell script to connect to exchange and retrieve information, it's probably better to post the question to MS's exchange forum instead of to the ServiceNow forum.

TrevorK
Kilo Sage

I have previous used the John J Anderson Powershell utility and it worked great. Michel Conter went and updated it since, and has provided the update set in his post.

https://community.servicenow.com/community?id=community_question&sys_id=7fda0f6ddb5cdbc01dcaf3231f96197c

I think this will lead you down the path to what you're looking for. I find it an easy way to avoid RDP'ing into servers to execute Powershell commands (I hope my IT Security team doesn't read this lol).

Damion McLaren
Tera Contributor

Thanks both!

 

@Hitoshi Ozawa - The goal is to write a midserver script file in servicenow that logs into a powershell exchange module (Connect-ExchangeOnline) and runs a command.  I would then create an action in flow designer that I would use to add parameters (ie. Identity - "john doe") that the midserver script file would utiliize.

So instead of simply logging into the midserver account, SNOW would use the same midserver account credentials to login to the powershell exchange module and run a command.  With that said I am open to suggestions.

ex. 

I would create a connection and credential alias in ServiceNow for the exchange admin login that I could store in a variable such as $psCred in the bold text below and access in the midserver script file. 


MidServer script file would then execute the following:

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/-Credential $psCred -Authentication Basic -AllowRedirection;

$result = Import-PSSession $Session -AllowClobber -DisableNameChecking Add-MailboxFolderPermission -Identity jake.doe@microsoft:\Contacts -user josh.doe@microsoft.com -AccessRights Editor Credential $cred -Server $computer;

Did you ever get this working? Looking to do the same thing but we have the added obstacle of MFA.