Powershell to Exchange 2013 - restricted language mode error

I am working on a C # web service that will be deployed on an Exchange 2013 server. This service will be responsible for running powershell commands to configure Exchange.

I connect through the workspace created in this way

const string shellUri = "http://schemas.microsoft.com/powershell/microsoft.exchange";
var uri = new Uri(_exchangeConnectionUri);
var credentials = (PSCredential)null; // Windows authentication
var connectionInfo = new WSManConnectionInfo(uri, shellUri, credentials);
connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Kerberos;
var runspace = RunspaceFactory.CreateRunspace(connectionInfo);

Using this working environment, I can run basic command line commands on the server.

get-mailbox -ResultSize unlimited

But running more complex commands gives me errors (this works if you run directly through powershell)

get-mailbox -ResultSize unlimited | where {$_.emailaddresses -like "*test.com"}

At line:1 char:43
+ get-mailbox -ResultSize unlimited | where {$_.emailaddresses -like "*test.com ...
+                                           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Script block literals are not allowed in restricted language mode or a Data section.

At line:1 char:44
+ get-mailbox -ResultSize unlimited | where {$_.emailaddresses -like "*test.com ...
+                                            ~~~~~~~~~~~~~~~~~
Property references are not allowed in restricted language mode or a Data section.

At line:1 char:44
+ get-mailbox -ResultSize unlimited | where {$_.emailaddresses -like "*test.com ...
+                                            ~~
A variable that cannot be referenced in restricted language mode or a Data section is being referenced. Variables that can be referenced include the following: $PSCulture, $PSUICulture, $true, $false, and  $null.

After searching, I found that I might have to register a new PSSessionConfiguration and make sure the scripts are running under PSLanguageMode = FullLanguage. See this post

I tried to do this, but as soon as I changed shellUri to http://schemas.microsoft.com/powershell/MyConfigName, I get the following error.

The WS-Management service cannot process the request.
Cannot find the MyConfigName session configuration in the WSMan: drive on the ComputerName computer.

shelllUri http://schemas.microsoft.com/powershell/Microsoft.Powershell

powershell

> Get-PSSessionConfiguration | format-list -property name
result:
Name : MyConfigName
Name : microsoft.powershell
Name : microsoft.powershell.workflow
Name : microsoft.powershell32
Name : microsoft.windows.servermanagerworkflows

> $session = New-PSSession -ConfigurationName MyConfigName -ConnectionUri $uri -Authentication Kerberos
result:
error "Cannot find the MyConfigName session configuration in the WSMan: drive on the ComputerName computer."

> $session = New-PSSession -ConfigurationName Microsoft.Powershell -ConnectionUri $uri -Authentication Kerberos
result:
error "Cannot find the Microsoft.Powershell session configuration in the WSMan: drive on the ComputerName."

> $session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $uri -Authentication Kerberos
result:
nothing, meaning $session variable set correctly

#, Microsoft.Exchange, Get-PSSessionConfiguration, , , .

, FullLanguage, powershell . , , PSSessionConfigurations, , Microsoft.Exchange .

+3
1

Microsoft, , . Powerhell powershell, . .

,

var runspace = RunspaceFactory.CreateRunspace();
runspace.Open();

object psSessionConnection;

// Create a powershell session for remote exchange server
using (var powershell = PowerShell.Create())
{
    var command = new PSCommand();
    command.AddCommand("New-PSSession");
    command.AddParameter("ConfigurationName", "Microsoft.Exchange");
    command.AddParameter("ConnectionUri", new Uri(_exchangeConnectionUri));
    command.AddParameter("Authentication", "Kerberos");
    powershell.Commands = command;
    powershell.Runspace = runspace;

    // TODO: Handle errors
    var result = powershell.Invoke();
    psSessionConnection = result[0];
}

// Set ExecutionPolicy on the process to unrestricted
using (var powershell = PowerShell.Create())
{
    var command = new PSCommand();
    command.AddCommand("Set-ExecutionPolicy");
    command.AddParameter("Scope", "Process");
    command.AddParameter("ExecutionPolicy", "Unrestricted");
    powershell.Commands = command;
    powershell.Runspace = runspace;

    powershell.Invoke()
}

// Import remote exchange session into runspace
using (var powershell = PowerShell.Create())
{
    var command = new PSCommand();
    command.AddCommand("Import-PSSession");
    command.AddParameter("Session", psSessionConnection);
    powershell.Commands = command;
    powershell.Runspace = runspace;

    powershell.Invoke()
}
+8

All Articles