Invocando cmdlets do PowerShell do C #
Eu estou tentando aprender como chamar cmdlets PS de c # e se deparar com a classe PowerShell. Funciona bem para uso básico, mas agora eu queria executar este comando PS:
Get-ChildItem | where {$_.Length -gt 1000000}
Eu tentei construir isso através da classe powershell, mas não consigo fazer isso. Este é o meu código até agora:
PowerShell ps = PowerShell.Create();
ps.AddCommand("Get-ChildItem");
ps.AddCommand("where-object");
ps.AddParameter("Length");
ps.AddParameter("-gt");
ps.AddParameter("10000");
// Call the PowerShell.Invoke() method to run the
// commands of the pipeline.
foreach (PSObject result in ps.Invoke())
{
Console.WriteLine(
"{0,-24}{1}",
result.Members["Length"].Value,
result.Members["Name"].Value);
} // End foreach.
Eu sempre recebo uma exceção quando executo isso. É possível executar o cmdlet Where-Object dessa forma?