.net remote powershell with Exchange 2010 Cmdlets - Problem with "Script block literals are not allowed in restricted language mode or a Data section"

rated by 0 users
This post has 4 Replies | 3 Followers

Top 150 Contributor
Posts 8
OlivierR Posted: 12-03-2010 9:18 AM

Hi All, I am trying to run cmdlets from VB.NET. I managed to create a session to a remote session and to run either basic powershell commands or Exchange 2010 cmdlets. However I have a problem when trying to run more complex scripts, like

"Get-Mailbox | Where-Object {$_.database -match "database"}"

because I get the error

" Script block literals are not allowed in restricted language mode or a Data section".

 

This problem only happens with Exchange cmdlets, while using the "http://schemas.microsoft.com/powershell/Microsoft.Exchange" URI.

I hope my question is clear, let me know if you need more details. Thank you a lot for your help because I have been struggling for few days with that now.

I believe there migtht be a way to create a new PSSession and then import the exchange cmdlets locally but I would prefer avoiding that as it takes a long time to import the cmdlets.

 

Olivier.

Top 10 Contributor
Posts 248
Microsoft MVP
Top Contributor

Why don't you import only the Exchange cmdlets that you really need? You don't have to import them all.

Top 150 Contributor
Posts 8

That's an excellent suggestion, that I will surely use often. I did not know that was possible. I'll try that today.

 

However I have tested in the remote shell (not in .NET yet) to import the command while creating a new PS-SESSION but it still says that the "restricted language mode" prevent from runnign script blocks, or references to variables such as $_ .

How can we get rid of this restriction ?

 

Thank you.

Top 150 Contributor
Posts 8

Well, in order to remove the restriction I believe that one must create its own "PSSessionConfiguration" on the actual server using "register-PSSessionConfiguration" and then a startupscript and other options can be set.

However I could not manager to load the Exchange cmdlets in this way... 

Top 25 Contributor
Posts 39
Microsoft MVP
Top Contributor

Can you share the full New-PSSession command you're executing to connect to Exchange?

Page 1 of 1 (5 items) | RSS
Concentrated Tech NSoftware Dell Compellent Sponsored by Idera and Concentrated Tech and NSoftware and Dell Compellent
Copyright 2011 PowerShell.com. All rights reserved.