2011-07-11 6 views
2

こんにちは私は、C#プログラムを使用して、リモートのrunspaceでExchange 2010 powershellコマンドレットを呼び出しています。 psコマンドは次のとおりです。C#の "format-list"と "out-file"パイプラインでpowershellコマンドを呼び出す方法は?

「は、Get-MailboxDatabase -Server EX2010SVR1 -Status |形式-リスト アイデンティティ、GUID、マウント、CircularLoggingEnabled、回復|アウトファイル 'C:\ db.txt' -Encoding UTF8 「幅8192」。

私のコードのようになります。


static int Main(string[] args) 
{ 

    const string SHELL_URI = "http://schemas.microsoft.com/powershell/Microsoft.Exchange"; 
    const string COMMAND = "Get-MailboxDatabase -Server EX2010SVR1 -Status | Format-List Identity,Guid,mounted,CircularLoggingEnabled,Recovery | Out-File 'C:\db.txt' -Encoding UTF8 -Width 8192"; 

    System.Uri serverUri = new Uri("http://EX2010SVR1/powershell?serializationLevel=Full"); 
    PSCredential creds = (PSCredential)null; // Use Windows Authentication 
    WSManConnectionInfo connectionInfo = new WSManConnectionInfo(serverUri, SHELL_URI, creds); 

    try 
    { 
     using (Runspace rs = RunspaceFactory.CreateRunspace(connectionInfo)) 
     { 
      rs.Open(); 
      PowerShell psh = PowerShell.Create(); 
      psh.Runspace = rs; 
      psh.AddCommand(COMMAND); 
      Collection results = psh.Invoke(); 
      rs.Close(); 
     } 
    } 
    catch (Exception ex) 
    { 
     System.Console.WriteLine("exception: {0}", ex.ToString()); 
    } 
    return 0; 
} 

私はExchange 2010サーバーをホストしているWin2008 R2上のC#のプログラムを実行すると、私は常に例外を取得:


    System.Management.Automation.RemoteException: The term 'Format-List' 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. 
    at System.Management.Automation.PowerShell.CoreInvoke[TOutput](IEnumerable input, PSDataCollection`1 output, PSInvocationSettings settings) 
    at System.Management.Automation.PowerShell.Invoke(IEnumerable input, PSInvocationSettings settings) 
    at System.Management.Automation.PowerShell.Invoke() 
    at RemotePS.Program.Main(String[] args) 

プログラムです"Format-List"と "Out-File"パイプラインなしでうまく動作します。 2010年の管理シェルでもコマンド全体がうまく機能しています。私はまた、システム上でpowershell 2.0であることを確認しました。

何が起こっているのか把握するのに役立つでしょうか?どんな助けでも大歓迎です。

トム

+0

Format-Listコマンドレットを使用せずにコマンドをececuteした場合、最終的には "formatting"コマンドレットとなり、テスト上の目的でAmortしようとします。 ? – Tigran

+0

もう一つのこと:オンラインドキュメントを見ると、Format-Listが見つかります。Windows PowerShell 2.0。 "サーバー側"のコマンドシェルが2.0でない可能性があります – Tigran

+0

"Format-List"と "Out-File"パイプラインがなくてもプログラムは正常に動作しています。 2010年の管理シェルでもコマンド全体がうまく機能しています。 – TomF

答えて

1

私が書いた最初の埋め込まPowerShellを使用し、同じ問題を持っています。私は痕跡を探しますが、私はもうそれを見つけることができません。ここで

何かが、私はあなたのコードに適応することを私のために働いている:

static void Main(string[] args) 
{ 
    const string SHELL_URI = "http://schemas.microsoft.com/powershell/Microsoft.PowerShell"; 
    const string COMMAND = @"get-process | format-List | Out-File -file c:\temp\jpb.txt"; 
    System.Uri serverUri = new Uri("http://WM2008R2ENT/powershell?serializationLevel=Full"); 
    PSCredential creds = (PSCredential)null; // Use Windows Authentication 
    WSManConnectionInfo connectionInfo = new WSManConnectionInfo(false, 
                   "WM2008R2ENT", 
                   5985, 
                   "/wsman", 
                   SHELL_URI, 
                   creds); 
    try 
    { 
    using (Runspace rs = RunspaceFactory.CreateRunspace(connectionInfo)) 
    { 
     rs.Open(); 

     Pipeline pipeline = rs.CreatePipeline(); 

     string cmdLine; 
     cmdLine = string.Format("&{{{0}}}", COMMAND); 

     pipeline.Commands.AddScript(cmdLine); 

     Collection<PSObject> results = pipeline.Invoke(); 

     rs.Close(); 
    } 
    } 
    catch (Exception ex) 
    { 
    System.Console.WriteLine("exception: {0}", ex.ToString()); 
    } 
    return; 
} 

は気をつけて、I'am私はパイプラインを使用する例では取引所のPowerShell

を使用していない、おそらくあなたの問題がから来ていますあなたがコマンドを渡す方法。

+0

ありがとうございました。できます。 – TomF

+0

質問に答えることができますか? – JPBlanc

0

「Command'-Object」を使って作業できます。ここにも

Runspace rs = RunspaceFactory.CreateRunspace(); 
PowerShell ps = PowerShell.Create(); 

Pipeline pipeline = rs.CreatePipeline(); 

Command cmd1 = new Command("Get-MailboxDatabase"); 
cmd1.Parameters.Add("Server", "EX2010SVR1"); 
cmd1.Parameters.Add("Status"); 
pipeline.Commands.Add(cmd1); 

Command cmd2 = new Command("Format-List"); 
cmd2.Parameters.Add("Property", "Identity, Guid, mounted, CircularLoggingEnabled, Recovery"); 
pipeline.Commands.Add(cmd2); 

Command cmd3 = new Command("Format-List"); 
cmd3.Parameters.Add("FilePath", "C:\db.txt"); 
cmd3.Parameters.Add("Encoding", "UTF8"); 
cmd3.Parameters.Add("Width", "8192"); 
pipeline.Commands.Add(cmd3); 

Collection<PSObject> output = pipeline.Invoke(); 

参照:Invoking powershell cmdlets from C#

0

彼らはしかし、短い、私は、これは古いスレッドです実現が、私は私の調査結果を提示したかったです。

私は同僚の同僚とまったく同じ問題に直面しました。行方不明になった問題を追跡することができました。また、Microsoft.Exchangeのランスペースに接続しなければならず、実行するときにFormat-Listコマンドレットは使用できなくなりました。ランスペースを使用しない場合、コマンドレットはうまく動作します。

まだ解明できませんでしたが、Runspaceの代わりにRunspacePoolを使用する可能性を探究し、両方のコマンドレットをパイプラインで実行できるようにするつもりです。