Utils/Copy-File.ps1

<#
.NOTES
===========================================================================
 Created with: SAPIEN Technologies, Inc., PowerShell Studio 2019 v5.6.160
 Created on: 4/29/2019 4:51 PM
 Created by: jisodl0
 Organization: J.B. Hunt
 Filename: Copy-File.ps1
===========================================================================
.DESCRIPTION
    A script written by Matthew Storm (jisoms1) to help verify a single file or
 directory is copied to a list of servers.
#>


<#
.SYNOPSIS
  Copies a file or directory and it's contents to a given computer/server.

.DESCRIPTION
  Takes the given file and copies it to the given collection of servers.
Used for quickly ensuring the same file is on various servers.

.PARAMETER DestinationComputers
  The names of the computers to deploy the file to. Accepts an array of names
or a path to a file that contains a server name for each line.

.PARAMETER SourcePath
  The path to the source file/directory you wish to copy.

.PARAMETER DestinationPath
  The path on the remote machine(s) you wish for the files to be copied to.

.EXAMPLE
  PS C:\> Copy-FileToServers -Computers 'WINX-16674' -Source 'C:\tmp\tmp.war' -Destination 'appl\tmp\'

  Copies the tmp.war file from your C drive to the WINX-16674 machine in
appl\tmp\.

.EXAMPLE
  PS C:\> Copy-FileToServers -Computers '.\ServerList.txt' -Source '.\tmp\tmp.war' -Destination 'c$\tmp\'

  Copies the tmp.war file to the C:\tmp directory of every server in the
ServerList.txt file.
#>

function Copy-FileToServers {
  [CmdletBinding()]
  param
  (
    [Parameter(Mandatory = $true,
               Position = 0)]
    [Alias('Servers', 'Computers', 'Computer', 'Server')]
    [System.String[]]
    $DestinationComputers,
    
    [Parameter(Mandatory = $true,
               Position = 1)]
    [Alias('Source')]
    [System.String]
    $SourcePath,
    
    [Parameter(Mandatory = $true,
               Position = 2)]
    [Alias('Destination')]
    [System.String]
    $DestinationPath
  )
  
  if (Test-Path -Path $DestinationComputers) {
    $ComputerNames = Get-Content $DestinationComputers
    foreach ($Computer in $ComputerNames) {
      if ((Test-Path -Path \\$Computer\$DestinationPath)) {
        Write-Verbose "Copying source file(s) to $Computer..."
        Copy-Item $SourcePath -Destination \\$Computer\$DestinationPath -Recurse
      } else {
        Write-Error "\\$Computer\$DestinationPath is not reachable or does not exist"
      }
    }
  } else {
    foreach ($Computer in $DestinationComputers) {
      if ((Test-Path -Path \\$Computer\$DestinationPath)) {
        Write-Verbose "Copying source file(s) to $Computer..."
        Copy-Item $SourcePath -Destination \\$Computer\$DestinationPath -Recurse
      } else {
        Write-Error "\\$Computer\$DestinationPath is not reachable or does not exist"
      }
    }
  }
}