title: PowerShell Functions and not repeating code
body: <p>these last couple of days I've been breaking my head on functions and parameters. I would really like to be able to enhance my function to delete files by having it run on the remote server with the <code>Invoke-Command</code> but without repeating the code.</p>
  
  <p>What I would like to be able to do within my script is to call the function like this:</p>
  
  <ul>
  <li><code>Delete-OldFiles "\\Domain\Share\Dir1" "10" "Auto_Clean.log"</code> </li>
  <li><code>Delete-OldFiles "SERVER1" "C:\Share\Dir1" "10" "Auto_Clean.log"</code> </li>
  </ul>
  
  <p>The first call of the function works perfectly and executes the function on the local server. With the second call I want to pass the server name to the function so it will execute the script on the remote server.</p>
  
  <p>I'm sure there must be an option to enable this <code>Invoke-Command</code>within the function when the server name is provided and without having to write the code twice... But I just can't seem to get my head around it.</p>
  
  <p><code>Invoke-Command -ComputerName $_.Server -ScriptBlock ${Function:Delete-OldFiles} -ArgumentList ($_.Server,$_.Path,$_.OlderThanDays,"Auto_Clean.log")</code></p>
  
  <p><strong>My function</strong></p>
  
  <pre><code>Function Delete-OldFiles {
  [CmdletBinding(SupportsShouldProcess=$True)] # Add -WhatIf support for dry run
  Param(
      [Parameter(Mandatory=$False,Position=1)]
      [String]$Server,
      [Parameter(Mandatory=$True,Position=2)]
      [ValidateScript({Test-Path $_})]
      [String]$Target,
      [Parameter(Mandatory=$True,Position=3)]
      [Int]$OlderThanDays,
      [Parameter(Mandatory=$True,Position=4)]
      [String]$LogName
        )
  
  if ($PSVersionTable.PSVersion.Major -ge "3") {
  
  # PowerShell 3+ Remove files older than (FASTER)
      Get-ChildItem -Path $Target -Exclude $LogName -Recurse -File | 
      Where-Object { $_.CreationTime -lt (Get-Date).AddDays(-$OlderThanDays) } | ForEach {
          $Item = $_.FullName
          Remove-Item $Item -Recurse -Force -ErrorAction SilentlyContinue
          $Timestamp = (Get-Date).ToShortDateString()+" | "+(Get-Date).ToLongTimeString()      
          # If files can't be removed
          if (Test-Path $Item)
              { "$Timestamp | FAILLED: $Server $Item (IN USE)" } 
          else
              { "$Timestamp | REMOVED: $Server $Item" }  
          } | Tee-Object $Target\$LogName -Append } # Output file names to console &amp; logfile at the same time
  
  Else {               
  
  # PowerShell 2 Remove files older than
      Get-ChildItem -Path $Target -Exclude $LogName -Recurse | 
      Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt (Get-Date).AddDays(-$OlderThanDays) } | ForEach {
          $Item = $_.FullName
          Remove-Item $Item -Recurse -Force -ErrorAction SilentlyContinue
          $Timestamp = (Get-Date).ToShortDateString()+" | "+(Get-Date).ToLongTimeString()      
          # If files can't be removed
          if (Test-Path $Item)
              { 
              Write-Host "$Timestamp | FAILLED: $Server $Item (IN USE)"
              "$Timestamp | FAILLED: $Server $Item (IN USE)"
               } 
          else
              { 
              Write-Host "$Timestamp | REMOVED: $Server $Item"
              "$Timestamp | REMOVED: $Server $Item"
              }  
          } |  Out-File $Target\$LogName -Append } 
  }
  </code></pre>
  
  <p>Thank you for your help.</p>