Welcome › Forums › General PowerShell Q&A › Empty powershell collection being returned from Pipeline.Invoke() in C#
- This topic has 2 replies, 2 voices, and was last updated 1 month, 2 weeks ago by
Participant.
-
AuthorPosts
-
-
December 3, 2020 at 4:09 am #276135
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:
PowerShell123456789101112131415foreach (var server in _collServers){Pipeline pipeline2 = runspace.CreatePipeline();pipeline2 = runspace.CreatePipeline();scriptCommand = new Command(@"C:\DEV\Monitor\Monitor\bin\scripts\GetDisks.ps1");CommandParameter commandParm = new CommandParameter("$server", server);scriptCommand.Parameters.Add(commandParm);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:
PowerShell1234567891011121314$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)}$disksoutbut it doesnt return a PSObjects collection into collDisks.
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
PowerShell1$disks = Get-CimInstance -ClassName CIM_LogicalDisk | Where-Object { ($_.DeviceID -ge 'C') -and ($_.DriveType -eq 3)} | Select-Object DeviceID, VolumeName, Size, FreespaceIt doesnt make any difference if I try using PSCustomObject instead, or creating the object collection different ways.
-
December 3, 2020 at 9:18 am #276225
Is there any error that you can capture from pipeline2 ?
-
December 4, 2020 at 6:51 am #276534
No. I checked through and couldn’t see any errors.
-
-
-
AuthorPosts
- You must be logged in to reply to this topic.