Syntax not supported by this runspace in Powershell and C#

Question:

I am trying to get information about my users’ mailboxes using power shell integrated into C#, but i am getting below error:

Here is the code that I am using:

Any Suggestions on this? How can I overcome to this issue?

Answer:

Normally i like to leave the dead alone but in this case i feel the need to resurrect this old post because i encountered the exact problem, need a place to keep this code and hope this prevents anybody else from wasting too much time.

I have found connecting to O365 with WSManConnectionInfo problematic and after wasting far too much time trying to get it to work i won’t be using it unless i have no other choice.

That said the following code works for me and behaves the same as if i had opened a PowerShell prompt entered the commands i want to run 🙂 it makes testing a lot simpler.

The above code assumes username & password as string variables already in scope. Also an Extention method ToSecureString exists and is in scope.

As far as i understand PowerShell.Creste() provides an execution environment set to Restricted so you first need to change that. In this case becasue we scoped the Set-ExecutionPolicy to the process Admin rights are not required.

From what little i found on the subject it appears you need to remove the session when you are done or you may get throttled and lock yourself out of O365 for some time.

Source:

Syntax not supported by this runspace in Powershell and C# by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply