2017-09-08 489 views
0

wpfアプリケーションは、開発されたコンピュータで正常に動作しています。 bin/Releaseのファイルは、アプリケーションの実行に使用されます。 System.Management.Automation.dllは、新しいメールボックスを作成するためにExchange Server 2010上でリモートでpowershellを使用するために使用されます。 Local = Trueの設定であるので、dllはbin/releaseのファイルに追加されます。共通言語ランタイムが無効なプログラムを検出しました

ただし、bin/releaseの同じファイルが別のコンピュータにコピーされている場合、メールボックス作成部分を除いてActive Directoryのすべての機能が正常に動作しています。エラー:Common Language Runtimeが無効なプログラムを検出しました。

「コードの最適化」のチェックを外してVSで再構築することについてアドバイスをいくつか見つけることができますが、それは役に立たないようです。

私が前に言ったように、すべては開発者のコ​​ンピュータでうまくいきます。したがって、私はVS 2013もインストールされている別のコンピュータでこのアプリケーションを実行し、エラーが発生しました。「コマンドレット、関数、スクリプトファイル、または実行可能プログラムの名前として「Enable-Mailbox」という用語は認識されません。またはパスが含まれている場合は、パスが正しいことを確認して、もう一度やり直してください。ここで コードです:

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<PSSession> result = powershell.Invoke<PSSession>(); 

       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()); 
     } 

    } 
} 
+0

まず、アプリケーションが異なる権限セットで実行されていると思われます。クライアントマシンのadminとして実行してみてください。 – Karolis

+0

アプリは、高い優先度のアカウントで「別のユーザーとして実行」として開かれており、問題のない別のマシンで新しいユーザーをアクティブディレクトリに作成することができます。しかし、Exhange Server 2010上に新しいメールボックスを作成するために、開発者のマシンでうまくいき、他のコンピュータでエラーを出すだけです。このメールボックスの部分では、ユーザーのログインとパスワードはポップアップウィンドウから送信され、Exchange Server 2010と連携するためにpowershellに渡されます。 – user5237764

+0

ログを追加します。もう一度実行してください。ネットワークの制限があります。 – Karolis

答えて

0

これはいくつかのことかもしれないように思え、あなたのシナリオでは、原因が何であるかを知ることは困難です。


reinstall the .net framework (make sure the version is correct)

Common Language Runtime detected an invalid program in Visual Studio


For C# projects go to the project properties and under the Build tab un-check "Optimize Code".

Common Language Runtime detected an invalid program?


Try enabling 32-bit applications in your application pool advanced settings.

:まず、私はこれらの事をしようとするだろうことを失敗し、管理者として実行してみます

または

I finally managed to solve this issue. I unchecked code optimization in C# Express and that solved the issues.

InvalidProgramException/Common Language Runtime detected an invalid program

+0

あなたのお返事ありがとうございます。私の最初の記事で述べたように、a)すべてが開発者のコ​​ンピュータ上のVSでうまくいく。 b)開発者のコ​​ンピュータ上のbin/releaseフォルダのファイルを使用してうまく動作します。 c)ソリューションをビルドして再構築するための「最適化コード」オプションの選択を解除しようとしました。しかし、別のコンピュータにbin/releaseファイルをコピーすると、エラーが発生します。私はその別のコンピュータ上でフレームワークをチェックしましたが、それは同じです。だから、私は開発者のコ​​ンピュータにフレームワークを再インストールしようとし、32bitで「最適化コード」の追加と解除をやり直します。 – user5237764

+0

これまでに述べたように、開発者用コンピュータではうまくいきます。だから、私は別のコンピュータでVS 2013を使用し、VSからアプリを実行します。 エラーが発生しました。「Enable-Mailbox」という用語は、コマンドレット、関数、スクリプトファイル、または実行可能なプログラムの名前として認識されません。名前のスペルを確認するか、パスが含まれている場合は、パスが正しいことを確認してから、もう一度やり直してください。 – user5237764

0

それは解決しています。 PowerShellスクリプトがリモートからcmdletコマンドを使用するのは、権限の問題でした。

編集:メールボックスを作成できなかった可能性がありますが、このエラーは別の設定の別のコンピュータにありました。

「共通言語ランタイムエラー」が表示されたコンピュータでは、PowerShellの古いバージョンが問題でした。

関連する問題