Utils/Copy-File.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<#
.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"
      }
    }
  }
}