Supporting pipeline input properly

Welcome Forums General PowerShell Q&A Supporting pipeline input properly

Viewing 0 reply threads
  • Author
    Posts
    • #243113
      Participant
      Topics: 3
      Replies: 2
      Points: 22
      Rank: Member

      I am tring to wrtie a function ‘Edit-ContentOrder’ that accepts input from the pipeline via get-childitem. It is expected that the pipeline will provide filesystem objects ie gci on a folder containing .jpgs. Since gci on a directory will return FileInfo objects which contains Name and Directory properties, I thought the correct definition of this function would be as follows:

      function Edit-ContentOrder {
        [CmdletBinding(SupportsShouldProcess)]
        [Alias(‘Reorder-Contents’)]
        param(
          [Parameter(ValueFromPipelineByPropertyName=$true)]
          [string[]]$Name,
          [Parameter(ValueFromPipelineByPropertyName=$true)]
          [string[]]$Directory

      )

      begin{}
      process {
      }
      end{}
      }

      What I have discovered is that this is not quite right. The first thing to address is for each element in the pipeline what actually are $Name and $Directory? As you can see from the definition, they are both defined as arrays of strings. However on each individual iteration I need to address each element 1 at a time. If I print the type of either $Name or $Directory, they show: “string[]”. If that is the case, how do I address individual elements? $_ actually refers to the FileInfo which in itelf is the item in the pipeline and and the Name and Directory properies. But if I use $_, then what is the point of defining Name and Directory as pipeline parameters?

      I suppose I could perform a loop on $Name, but then I couldnt easily get the corresponding $Directory. See the really strange thing is if I print out $Name and $Directory, they do indeed print out the individual elements as if $Name is an individual string:

      Write-Host “*** Reorder-Contents; Type: ‘$($Name.GetType())’, name: ‘$Name'”;
      Write-Host “=== Element: $_, Type: $($_.GetType())”
      displays multiples of these:
      ——————–
      *** Reorder-Contents; Type: ‘string[]’, name: ‘log-info_0110.jpg’
      === Element: C:\Users\PLASTIKFAN\dev\app-logs\colour-extractor\log-info_0110.jpg, Type: System.IO.FileInfo
      ——————–

      As you can see ‘log-info_0110.jpg’ is an individual string, but its type is still string[]. How is this right? Its odd because sometimes I can address $Name as though its an individual string:

      <hr />

          [Regex]$expressionObj = New-Object Regex($Expression, $RegexOptionsEnum);
          $m = $expressionObj.Match($Name);

      <hr />

      This works as one would want, ie we’re executing a regular expression against $Name (as in individual string)
      However, if I try and use Join-Path, using $Directory and $Name, join-path complains because they are defined as string[] and not string:

      <hr />

      $fullname = Join-Path -Path $Directory -ChildPath $Name;

      <hr />

      results in:

      <hr />

      Join-Path: C:\Users\PLASTIKFAN\dev\github\PoSh\UtilsModule\Public\Edit-ContentOrder.ps1:35:55
      Line |
      35 | $fullname = Join-Path -Path $Directory -ChildPath $Name;
      | ~~~~~
      | Cannot convert ‘System.String[]’ to the type ‘System.String’ required by parameter ‘ChildPath’. Specified method is
      | not supported.

      <hr />

      Confused!

       

      • This topic was modified 2 months ago by Plastikfan. Reason: formatting
      • This topic was modified 2 months ago by Plastikfan. Reason: formatting
Viewing 0 reply threads
  • You must be logged in to reply to this topic.