【发布时间】:2021-11-02 04:45:33
【问题描述】:
C#9 中的 Powershell 对象问题
我正在尝试使用 system.management.powershell 对象在 C# 中检索物理磁盘。 当我尝试使用 cmdlet“Get-Process”时,powershell 对象会正确检索所有进程。
using (PowerShell PowerShellInstance = PowerShell.Create())
{
// this works
//PowerShellInstance.AddCommand("Get-Process");
PowerShellInstance.AddScript("Get-PhysicalDisk");
//PowerShellInstance.AddCommand("Out-String -Width 240 -Stream");
Collection<PSObject> outlist = PowerShellInstance.Invoke();
}
当我尝试使用 cmdlet“Get-PhysicalDevice”时,没有返回数据,也没有引发错误。
我也尝试过使用Runspace和管道来执行get-process,所有进程都正确返回。
Runspace rs = RunspaceFactory.CreateRunspace();
rs.Open();
Pipeline p = rs.CreatePipeline();
p.Commands.Add("get-process");
// p.Commands.Add("get-physicaldisk");
Collection<PSObject> results = p.Invoke();
rs.Close();
当我尝试使用运行空间获取物理磁盘时,程序会抛出命令未找到异常。 我正在将服务器监视器从 VB 转移到 C#。 Runspace (get-physicaldisk) 使用 .net framework 4.1.7 在 VB 中正常工作。
我正在使用 System Management 5.0、..Automation 7.1.4 和 Powershell SDK 7.1.4。
【问题讨论】:
标签: c# powershell runspace