我目前正在try 获取我通过PowerShell运行的一个程序的标准输出.
我有这样的东西:
using var ps = PowerShell.Create();
ps.AddCommand("Start-Process")
.AddParameter("FilePath", somePath)
.AddParameter("ArgumentList", someArguments)
.AddParameter(...); //Adding some more parameters
var result = ps.Invoke<Process>();
result[0].StandardOutput //Always null
现在我想要标准yields ,但我好像买不到. 生成的流程有一个公共属性"StandardOutput",但它始终为空.
我知道我可以通过添加一个完整的PowerShell脚本来实现这一点,而不是添加PowerShell类提供的其他函数.
$pinfo = New-Object System.Diagnostics.ProcessStartInfo
$pinfo.FileName = "SomeFileName"
$pinfo.RedirectStandardOutput = $true
$pinfo.UseShellExecute = $false
$pinfo.Arguments = "someArguments"
$p = New-Object System.Diagnostics.Process
$p.StartInfo = $pinfo
$p.Start() | Out-Null
$p.WaitForExit()
$stdout = $p.StandardOutput.ReadToEnd()
Write-Host $stdout
不过,我不想这样做,因为在我的用例中,我将根据需要使用参数构建这些命令,并且我不想求助于字符串操作来构建我的命令.