![](/img/trans.png)
[英]Using WMI from PowerShell or C# - how can I get remote host time, including milliseconds?
[英]Using the PowerShell host from C# how can I get the output?
具有以下 NET 5 控制台應用程序:
static void Main(string[] args)
{
using var ps = PowerShell.Create();
// ps.AddCommand("Get-Service");
ps.AddStatement().AddCommand("Get-Service");
ps.Invoke();
}
調用似乎沒有錯誤地執行,但是 output 去哪里了? 我已經嘗試了AddCommand
和AddStatement
,還檢查了調試器中的ps
變量,沒有 output 的跡象。 我確實發現了ps.Streams.Information
但它是空的。
如PowerShell.Invoke方法的文檔中所述,它返回一個集合,其中包含 PowerShell 操作產生的任何結果。
因此,你可以寫
static void Main(string[] args)
{
using var ps = PowerShell.Create();
ps.AddStatement().AddCommand("Get-Service");
var results = ps.Invoke();
foreach (var result in results)
{
Console.WriteLine(result);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.