title: Common Language Runtime detected an invalid program
body: <p>The wpf app is working well on a computer where it was developed. The files from bin/Release are used to run the app.
  There is System.Management.Automation.dll used to work with powershell remotely on the Exchange Server 2010 to create new mailboxes. It's setup with Local = True so the dll is added into the files in the bin/release.</p>
  
  <p>However, if the same files from bin/release are copied on another computer then all functionalities on Active Directory are working well except the mail-box creation part as it gives Error: Common Language Runtime detected an invalid program.</p>
  
  <p>I could find a few advises as to uncheck 'Optimize Code' and rebuild it in VS but it seems as it's not helping.</p>
  
  <p>As I said before all is working well on developer's computer. So, I run the app on another computer where VS 2013 was also installed and got an error:"The term 'Enable-Mailbox' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again."
  Here is the code:</p>
  
  <pre><code>public static class  ManageMailBox
  {
      public static bool CreateMailBox(string strUserID, string admin_user, string pword) //, string str_upn)
      {
          try
          {
              string strExchangeServer = Constants.ExchangeServer; 
              Uri uri = new Uri(@"http://" + strExchangeServer + @"/powershell?serializationLevel=Full"); // orig works
  
              /// must pass secure string
              char[] passwordChars = pword.ToCharArray();
              SecureString password = new SecureString();
              foreach (char c in passwordChars)
              {
                  password.AppendChar(c);
              }
              PSCredential credential = new PSCredential("DOMAIN\\" + admin_user, password);
              Runspace runspace = RunspaceFactory.CreateRunspace();
              PowerShell powershell = PowerShell.Create();
              PSCommand command = new PSCommand();
  
              command.AddCommand("New-PSSession");
              command.AddParameter("ConfigurationName", "Microsoft.Exchange");
              command.AddParameter("ConnectionUri", uri);
              command.AddParameter("Credential", credential);
              command.AddParameter("Authentication", "Default");
              PSSessionOption sessionOption = new PSSessionOption();
              sessionOption.SkipCACheck = true;
              sessionOption.SkipCNCheck = true;
              sessionOption.SkipRevocationCheck = true;
              command.AddParameter("SessionOption", sessionOption);
  
              powershell.Commands = command;
              try
              {
                  // open the remote runspace
                  runspace.Open();
  
                  // associate the runspace with powershell
                  powershell.Runspace = runspace;
  
                  // invoke the powershell to obtain the results
                  Collection&lt;PSSession&gt; result = powershell.Invoke&lt;PSSession&gt;();
  
                  foreach (ErrorRecord current in powershell.Streams.Error)
                  {
                      throw new Exception("Exception: " + current.Exception.ToString());
                      throw new Exception("Inner Exception: " + current.Exception.InnerException);
                  }
  
                  if (result.Count != 1)
                      throw new Exception("Unexpected number of Remote Runspace connections returned.");
  
                  // Set the runspace as a local variable on the runspace
                  powershell = PowerShell.Create();
                  command = new PSCommand();
                  command.AddCommand("Set-Variable");
                  command.AddParameter("Name", "ra");
                  command.AddParameter("Value", result[0]);
                  powershell.Commands = command;
                  powershell.Runspace = runspace;
  
                  powershell.Invoke();
  
                  // First import the cmdlets in the current runspace (using Import-PSSession)
                  powershell = PowerShell.Create();
                  command = new PSCommand();
                  //command.AddScript("Set-ExecutionPolicy -Unrestricted");
                  command.AddScript("Import-PSSession -Session $ra");
                  powershell.Commands = command;
                  powershell.Runspace = runspace;
                  powershell.Invoke();
  
                  // Now run get-ExchangeServer
                  powershell = PowerShell.Create();
                  command = new PSCommand();
  
                  command.AddCommand("Enable-Mailbox");
                  command.AddParameter("Identity", strUserID);
                  command.AddParameter("Alias", strUserID);
                  command.AddParameter("Database", "IAP Mailbox Database 0948752629");
                  powershell.Commands = command;
                  powershell.Runspace = runspace;
                  powershell.Invoke(); // ERROR !!! The term 'Enable-Mailbox' is not recognized as the name of a cmdlet, function
  
                  return true;
              }
              catch(Exception ex) { 
                      throw new Exception(ex.Message.ToString()); }
              finally
              {
                  // dispose the runspace and enable garbage collection
                  runspace.Dispose();
                  runspace = null;
  
                  // Finally dispose the powershell and set all variables to null to free
                  // up any resources.
                  powershell.Dispose();
                  powershell = null;
              }
          }
          catch (Exception argex)
          {
              throw new ArgumentException(argex.Message.ToString());
          }
  
      }
  }
  </code></pre>