Passing fileinfo to function

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

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

    this is my function
    function Write-Log
    # Param1 help description


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

    $FullName | Out-File -Append -FilePath $logfile
    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

    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

    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.