title: Write Powershell function, that can be called with pipeline input AND "normal" input"
body: <p>I've a simple function:</p>
  
  <pre><code>    function Write-Log {
          [CmdletBinding()]
          param (
              # Lines to log
              [Parameter(Mandatory , ValueFromPipeline )]
              [AllowEmptyString()]
              $messages
          )
  
          process {
              Write-Host $_ 
          }
      }
  </code></pre>
  
  <p>Based on <code>ValueFromPipeline</code> in can use the function with pipeline input, e.g.</p>
  
  <pre><code> "a","b", "c" | Write-Log
   a
   b
   c
  </code></pre>
  
  <p>That's ok. But if I want to use my function in that way:</p>
  
  <pre><code> Write-Log -message "a", "b", "c"
  </code></pre>
  
  <p>the automatic <code>$_</code> variable is empty, and nothing is printed. </p>
  
  <p>I also found these two stackoverflow links:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/885349/how-do-i-write-a-powershell-script-that-accepts-pipeline-input">Handling pipeline and parameter input in a Powershell function</a></li>
  <li><a href="https://stackoverflow.com/questions/9593656/handling-pipeline-and-parameter-input-in-a-powershell-function">How do I write a PowerShell script that accepts pipeline input?</a></li>
  </ul>
  
  <p>Both of them suggest the following pattern:</p>
  
  <pre><code>  function Write-Log {
          [CmdletBinding()]
          param (
              # Lines to log
              [Parameter(Mandatory , ValueFromPipeline )]
              [AllowEmptyString()]
              $messages
          )
  
          process {
              foreach ($message in $messages){
                  Write-Host $message
              }
          }
      }
  </code></pre>
  
  <p>Above pattern works for my use case. But from my point of view is feels weird to call <code>foreach</code> in the `process´ block, since (as far as I've understood) the process block is called for every pipeline item. As there a better cleaner way to write functions supporting both use cases?</p>
  
  <p>Thx.</p>