こんにちは私は、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であることを確認しました。
何が起こっているのか把握するのに役立つでしょうか?どんな助けでも大歓迎です。
トム
Format-Listコマンドレットを使用せずにコマンドをececuteした場合、最終的には "formatting"コマンドレットとなり、テスト上の目的でAmortしようとします。 ? – Tigran
もう一つのこと:オンラインドキュメントを見ると、Format-Listが見つかります。Windows PowerShell 2.0。 "サーバー側"のコマンドシェルが2.0でない可能性があります – Tigran
"Format-List"と "Out-File"パイプラインがなくてもプログラムは正常に動作しています。 2010年の管理シェルでもコマンド全体がうまく機能しています。 – TomF