I have a function defined as follows:
function Get-LatestProjects
{
[CmdletBinding()]
Param
(
[Parameter(ValueFromPipeline = $true, Position = 0)]
[Company.Project.Entities.Project[]] $Projects
)
Begin {}
Process
{
...
}
End {}
}
I’m calling this function from C# and the parameter in the code is an array of that type, but I’m getting this error:
Cannot process argument transformation on parameter ‘Projects’. Cannot convert the “Company.Project.Entities.Project[]” value of type “Company.Project.Entities.Project[]” to type “Company.Project.Entities.Project”.
The C# code that makes the call:
var script = @". \\server01\Runspace.ps1; Get-LatestProjects $args";
var args = _pmRepository.GetAllProjects().ToArray(); // GetAllProjects returns List<Project>
using (Runspace runSpace = RunspaceFactory.CreateRunspace())
{
runSpace.Open();
var ps = PowerShell.Create();
ps.Runspace = runSpace;
ps.AddScript(script);
ps.AddArgument(args);
Collection<PSObject> results = ps.Invoke();
...
}
Do I need to do any conversion of the objects before I pass them to the powershell function? Or can I not add a type constraint in the powershell function for the $Projects parameter?
It looks like _pmRepository.GetAllProjects() return only one project. Can’t you just declare $Projects without type and use $Projects.gettype to have a look to the type.
—-Edited——
You can avoid your array to be wraped into another array by writting.