C# Pipeline.Invoke() not returning powershell collection

%3CLINGO-SUB%20id%3D%22lingo-sub-1951433%22%20slang%3D%22en-US%22%3EC%23%20Pipeline.Invoke()%20not%20returning%20powershell%20collection%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1951433%22%20slang%3D%22en-US%22%3E%3CP%3EOn%20Windows%2010%2C%20in%20C%23%20I%20am%20trying%20to%20call%20a%20powershell%20script%20via%20Pipeline.Invoke()%20to%20retrieve%20all%20the%20disks%20on%20a%20network%20server%2C%20for%20each%20server%20on%20a%20network.%20But%20can't%20get%20it%20to%20return%20the%20data%20to%20C%23%20when%20there%20are%20multiple%20disks%20on%20a%20server.%3C%2FP%3E%3CP%3EI%20have%20the%20following%20C%23%20code%3A%3CBR%20%2F%3E%60%60%60%3CBR%20%2F%3Eforeach%20(var%20server%20in%20_collServers)%3CBR%20%2F%3E%7B%3CBR%20%2F%3E%26nbsp%3B%20Pipeline%20pipeline2%20%3D%20runspace.CreatePipeline()%3B%3CBR%20%2F%3E%26nbsp%3B%20pipeline2%20%3D%20runspace.CreatePipeline()%3B%3CBR%20%2F%3E%26nbsp%3B%20scriptCommand%20%3D%20new%20Command(%40%22C%3A%5CDEV%5CMonitor%5CMonitor%5Cbin%5Cscripts%5CGetDisks.ps1%22)%3B%3CBR%20%2F%3E%26nbsp%3B%20pipeline2.Commands.Add(scriptCommand)%3B%3CBR%20%2F%3E%26nbsp%3B%20var%20collDisks%20%3D%20pipeline2.Invoke()%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%20foreach%20(var%20item%20in%20collDisks)%3CBR%20%2F%3E%26nbsp%3B%20%7B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20var%20s%20%3D%20item.ToString()%3B%3CBR%20%2F%3E%26nbsp%3B%20%7D%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%60%60%60%3C%2FP%3E%3CP%3Ewhich%20executes%20the%20following%20powershell%20script%20(GetDisks.ps1)%2C%20with%20ComputerName%20hardcoded%20to%20a%20specific%20server%20for%20now%3A%3CBR%20%2F%3E%60%60%60%3CBR%20%2F%3E%24disks%20%3D%20Get-CimInstance%20-ClassName%20CIM_LogicalDisk%20-ComputerName%20SERVER01%20%7C%20Where-Object%20%7B%26nbsp%3B%20%26nbsp%3B(%24_.DeviceID%20-ge%20'C')%20-and%20(%24_.DriveType%20-eq%203)%7D%20%7C%20Select-Object%20DeviceID%2C%20VolumeName%2C%20Size%2C%20Freespace%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%20%24disksout%20%3D%20%5BSystem.Collections.Generic.List%5BPSObject%5D%5D%3A%3ANew()%3CBR%20%2F%3E%26nbsp%3B%20ForEach(%24d%20in%20%24disks)%3CBR%20%2F%3E%26nbsp%3B%20%7B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%24disk%20%3D%20%5BPSObject%5D%20%40%7B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20'DeviceID'%20%3D%20%24d.DeviceID%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20'VolumeName'%20%3D%20%24d.VolumeName%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20'Size'%20%3D%20%24d.Size%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20'Freespace'%20%3D%20%24d.Freespace%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%7D%3CBR%20%2F%3E%26nbsp%3B%20%24disksout.Add(%24disk)%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%24disksout%3CBR%20%2F%3E%60%60%60%3CBR%20%2F%3Ebut%20it%20doesnt%20return%20a%20PSObjects%20collection%20into%20collDisks.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22ps_disks_server.jpg%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F237470i049621D60E762456%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22ps_disks_server.jpg%22%20alt%3D%22ps_disks_server.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%E2%80%83%3C%2FP%3E%3CP%3E%3CBR%20%2F%3EIt%20works%20in%20the%20ISE%20(and%20every%20re-arrangement%20I%20try%20seems%20to%20work%20there).%3C%2FP%3E%3CP%3EIt%20works%20if%20I%20run%20it%20for%20my%20local%20machine%20by%20removing%20-ComputerName%20SERVER01%3CBR%20%2F%3E%60%60%60%3CBR%20%2F%3E%24disks%20%3D%20Get-CimInstance%20-ClassName%20CIM_LogicalDisk%20%7C%20Where-Object%20%7B%20(%24_.DeviceID%20-ge%20'C')%20-and%20(%24_.DriveType%20-eq%203)%7D%20%7C%20Select-Object%20DeviceID%2C%20VolumeName%2C%20Size%2C%20Freespace%3CBR%20%2F%3E%60%60%60%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22ps_disks_local.jpg%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F237471i56DF2415260E3F5F%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22ps_disks_local.jpg%22%20alt%3D%22ps_disks_local.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%E2%80%83%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIt%20doesnt%20make%20any%20difference%20if%20I%20try%20using%20PSCustomObject%20instead%2C%20or%20creating%20the%20object%20collection%20different%20ways.%20I%20suspected%20its%20a%20type%20conversion%20issue%2C%20but%20using%20GetType()%20in%20various%20locations%20havent%20found%20the%20issue.%20Would%20appreciate%20if%20anyone%20else%20can%20cast%20light%20on%20the%20problem.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1951433%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EVisual%20Studio%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Frequent Visitor

On Windows 10, in C# I am trying to call a powershell script via Pipeline.Invoke() to retrieve all the disks on a network server, for each server on a network. But can't get it to return the data to C# when there are multiple disks on a server.

I have the following C# code:
```
foreach (var server in _collServers)
{
  Pipeline pipeline2 = runspace.CreatePipeline();
  pipeline2 = runspace.CreatePipeline();
  scriptCommand = new Command(@"C:\DEV\Monitor\Monitor\bin\scripts\GetDisks.ps1");
  pipeline2.Commands.Add(scriptCommand);
  var collDisks = pipeline2.Invoke();

 

  foreach (var item in collDisks)
  {
    var s = item.ToString();
  }
}
```

which executes the following powershell script (GetDisks.ps1), with ComputerName hardcoded to a specific server for now:
```
$disks = Get-CimInstance -ClassName CIM_LogicalDisk -ComputerName SERVER01 | Where-Object {   ($_.DeviceID -ge 'C') -and ($_.DriveType -eq 3)} | Select-Object DeviceID, VolumeName, Size, Freespace

 

  $disksout = [System.Collections.Generic.List[PSObject]]::New()
  ForEach($d in $disks)
  {
    $disk = [PSObject] @{
      'DeviceID' = $d.DeviceID
      'VolumeName' = $d.VolumeName
      'Size' = $d.Size
      'Freespace' = $d.Freespace
    }
  $disksout.Add($disk)
}
$disksout
```
but it doesnt return a PSObjects collection into collDisks.

 

ps_disks_server.jpg


It works in the ISE (and every re-arrangement I try seems to work there).

It works if I run it for my local machine by removing -ComputerName SERVER01
```
$disks = Get-CimInstance -ClassName CIM_LogicalDisk | Where-Object { ($_.DeviceID -ge 'C') -and ($_.DriveType -eq 3)} | Select-Object DeviceID, VolumeName, Size, Freespace
```

ps_disks_local.jpg

 

It doesnt make any difference if I try using PSCustomObject instead, or creating the object collection different ways. I suspected its a type conversion issue, but using GetType() in various locations havent found the issue. Would appreciate if anyone else can cast light on the problem.

 

0 Replies