0

Have the requirement wherein there are 2 outputs which we need to do a union of below expression into an Array

a) $PipelineDataset.Activities.Name

b) ([regex]::Matches((Get-Content $TempFilePath),$pattern).groups|Where-Object Name -EQ '1').Value

enter image description here

IF ($PipelineDataset.Activities.Name.GetType().Name -eq 'String')
{$OuterActivityNames=$PipelineDataset.Activities.Name.Split(",")}
else
{$OuterActivityNames=$PipelineDataset.Activities.Name}



$ActivitiesName= $OuterActivityNames + ([regex]::Matches((Get-Content $TempFilePath),$pattern).groups|Where-Object Name -EQ '1').Value

There are scenarios wherein sometime there is only 1 value returned for this output : $PipelineDataset.Activities.Name thereby making it as a String type. So I am using the If else activity to convert it into array. Is that the right approach ?

Also, for the union I am using + operator and in case if there is no value present for ([regex]::Matches((Get-Content $TempFilePath),$pattern).groups|Where-Object Name -EQ '1').Value , it is still appending a value in the output array. example : lets say the 1st gives 1 element and 2nd expression has no value, the length of the overall array is coming as 2.

So how to avoid it?

Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
Nandan
  • 3,939
  • 2
  • 8
  • 21
  • 1
    Try `@($OuterActivityNames) + ...`. The `@()` forces $OuterActivityNames to be an array. – Theo Sep 06 '22 at 13:13
  • Thank you @Theo, the array conversion to string helps :). But what about the 2nd scenario wherein if there is no value in the 2nd expression, the union + operation output count is displaying as 2 rather than 1. So is the null value getting added and being count out – Nandan Sep 06 '22 at 13:44
  • Then capture that part in a variable first and only `if ($theCapturedStuff)` append it to the rest of the activities – Theo Sep 07 '22 at 12:10

0 Answers0