3

a recent patch of Microsoft Exchange changed the way people can use Invoke-Command to an Microsoft Exchange server.

Invoke-Command -Session $ExOSessionVariable -ScriptBlock {Get-RemoteMailbox -Identity UserName ; Get-User -Identity UserName}

is returning:

The syntax is not supported by this runspace. This can occur if the runspace is in no-language mode.

This behavior has changed since : https://techcommunity.microsoft.com/t5/exchange-team-blog/released-april-2021-exchange-server-security-updates

Microsoft advices to use .AddScript() or .AddCommand(). can someone tell me how to use this commands with invoke-Command ?

Any help is appreciated.

Michael

Michael
  • 31
  • 2
  • You should show how you’re building your session. The variable name suggests exo prefix but that module just wraps graph api. – Doug Maurer Apr 28 '21 at 15:59

1 Answers1

0

I think the semicolon is causing the problem in your example.

So this is a workaround rather than a direct answer to your question.

If you replace your script block, for example:

$Scriptblock = {  
  Get-RemoteMailbox -Identity UserName
  Get-User -Identity UserName
}

Invoke-Command -Session $ExOSessionVariable -ScriptBlock $Scriptblock 

It should work.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77