Passing fileinfo to function

This topic contains 2 replies, has 3 voices, and was last updated by Profile photo of Daniel Krebs Daniel Krebs 1 year, 10 months ago.

  • Author
    Posts
  • #22295
    Profile photo of Itamar Tziger
    Itamar Tziger
    Participant

    this is my function
    function Write-Log
    {
    [CmdletBinding()]
    Param
    (
    # Param1 help description
    [Parameter(ValueFromPipelineByPropertyName=$true)]
    [System.IO.FileInfo[]]$FileInfo,
    [Parameter(ValueFromPipelineByPropertyName=$true)]
    [System.IO.DirectoryInfo[]]
    $DirectoryInfo

    )

    Begin
    {
    $logfile = "c:\$(get-date -format 'ddmmmmyyyy_hhmmtt').txt"

    }
    Process
    {
    $FullName | Out-File -Append -FilePath $logfile
    }
    End
    {
    }
    }
    I want to pass fileinfo object to this function but I get error:
    "
    Write-Log : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties
    do not match any of the parameters that take pipeline input."

    Please advice

  • #22296
    Profile photo of Tim Pringle
    Tim Pringle
    Participant

    Hi Itamar,

    I think it's because you don't appear to be defining $FullName.

    If you're trying to reference the FullName property of the $FileInfo object, you need to refer to it like this

    $FileInfo.FullName | Out-File -Append -FilePath $logfile
    
  • #22300
    Profile photo of Daniel Krebs
    Daniel Krebs
    Participant

    You will need to add ValueFromPipeline=$true to your parameters if you want something like below to work:

     
    Get-Item -Path C:\Windows\Explorer.exe | Write-Log
    

You must be logged in to reply to this topic.