C#内で次のpowershellコマンドを実行しようとしています
パワーシェル:
"Get-ADDomainController -Filter * | Select-Object Name,OperatingSystem,OperatingSystemServicePack,Site | Format-List"
ここに私のC#コードがあります:
using (PowerShell inst = PowerShell.Create())
{
inst.AddCommand("Import-Module").AddParameter("Name", "ActiveDirectory");
inst.AddScript(command);
inst.Commands.AddCommand("Out-String");
foreach (PSObject result in inst.Invoke())
{
Console.WriteLine("'{0}'", result);
}
Console.ReadLine();
}
これは正常に機能し、結果を出力しますが、私ができるようにしたいのは、情報を反復処理することです
たとえば、印刷された結果は次のようになります
名前: xxx オペレーティング システム: Windows Server 2008 オペレーティング システムServicePack: サービス パック 2 サイト: xxx
foreach を実行して、名前、オペレーティング システム、サイトをリストに追加できるようにしたいと考えています。
それが理にかなっていることを願っています