title: Powershell to Exchange 2013 - Restricted language mode error
body: <p>I'm 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.</p>
  
  <p>I connect via a runspace created like this</p>
  
  <pre><code>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);
  </code></pre>
  
  <p>Using this runspace I am able to run basic powershell commands on the server.</p>
  
  <pre><code>get-mailbox -ResultSize unlimited
  </code></pre>
  
  <p>But running more complicated commands gives me errors (this does work if run directly through powershell)</p>
  
  <pre><code>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.
  </code></pre>
  
  <p>After searching I found that I might have to register a new PSSessionConfiguration and make sure the scripts are running under PSLanguageMode = FullLanguage. <a href="https://stackoverflow.com/a/18356159">See this post</a></p>
  
  <p>I tried to do this, but as soon as I change the shellUri to <code>http://schemas.microsoft.com/powershell/MyConfigName</code> I get the following error.</p>
  
  <pre><code>The WS-Management service cannot process the request.
  Cannot find the MyConfigName session configuration in the WSMan: drive on the ComputerName computer.
  </code></pre>
  
  <p>Using the following shelllUri gave me the same error <code>http://schemas.microsoft.com/powershell/Microsoft.Powershell</code></p>
  
  <p>This led me to try the following directly through powershell on the exchange server</p>
  
  <pre><code>&gt; Get-PSSessionConfiguration | format-list -property name
  result:
  Name : MyConfigName
  Name : microsoft.powershell
  Name : microsoft.powershell.workflow
  Name : microsoft.powershell32
  Name : microsoft.windows.servermanagerworkflows
  
  &gt; $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."
  
  &gt; $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."
  
  &gt; $session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $uri -Authentication Kerberos
  result:
  nothing, meaning $session variable set correctly
  </code></pre>
  
  <p>Like from the C# code, I can only use the Microsoft.Exchange configuration, but this configuration doesn't exist according to <code>Get-PSSessionConfiguration</code>, and the configurations that do exist in that list won't work.</p>
  
  <p>I am now wondering how to add a configuration with FullLanguage that I can use when calling powershell from code.
  I might also be completely wrong, and my issue is not related to PSSessionConfigurations at all, but then I still wonder why I can't see the Microsoft.Exchange configuration anywhere.</p>