Problem with Powershell object in C#9
I am trying to retrieve physical disks in C# using a system.management.powershell object. When I try using the cmdlet “Get-Process” the powershell object retrieves all processes correctly.
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();
}
When I try using the cmdlet “Get-PhysicalDevice” no data is returned, and no errors are thrown.
I have also tried using a Runspace and a pipeline to execute get-process, and all processes are returned correctly.
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();
When I try get-physicaldisk with a runspace the program throws a command not found exception. I am transferring a server monitor from VB to C#. The Runspace (get-physicaldisk) works correctly in VB using .net framework 4.1.7.
I am using System Management 5.0, ..Automation 7.1.4, and Powershell SDK 7.1.4.