Skip to content

Instantly share code, notes, and snippets.

@grokk-it
Created June 5, 2019 16:38
Show Gist options
  • Save grokk-it/c2a700ffdcb141834111735dca01021c to your computer and use it in GitHub Desktop.
Save grokk-it/c2a700ffdcb141834111735dca01021c to your computer and use it in GitHub Desktop.
function Copy-RCSSFile {
<#
.SYNOPSIS
Copy files from a given path to a remote computer.
.PARAMETER ComputerName
Target remote computer
.PARAMETER Path
File path for the local file to be copied
.PARAMETER Destination
File path for the destination file on the target computer
.PARAMETER DestinationPath
Destination directory on the target computer
#>
[CmdletBinding()]
param (
[Parameter( Mandatory=$True, ValueFromPipeline=$True, ValueFromPipelineByPropertyName=$True )
]
[string]$computername,
[Parameter( Mandatory=$True, ValueFromPipelineByPropertyName=$True )
]
[string]$path,
[Parameter( Mandatory=$True, ValueFromPipelineByPropertyName=$True )
]
[string]$destination,
[Parameter( ValueFromPipelineByPropertyName=$True )
]
[string]$destinationfolder
) # param
BEGIN {
# Setting up DestinationFolder
Write-Verbose "Setting $destinationfolder variable"
$destinationfolder = "C:\_Code"
} #BEGIN
PROCESS {
foreach ( $computer in $ComputerName ) {
# Setting up Session to remote computer
Write-Verbose "Connecting to $computer to copy items"
$session = New-PSSession -ComputerName $computer
Write-Verbose "Checking to see if $destinationfolder exists on $computer"
If (-not (Test-Path -LiteralPath $destinationfolder)) {
Try {
Write-Verbose "Creating $destinationfolder on $computer"
New-Item -Path $destinationfolder -ItemType Directory -ErrorAction Stop
} Catch {
Write-Error -Message "Unable to create directory $destinationfolder. Error was: $_" -ErrorAction Stop
}
} Else {
Write-Verbose "Directory already exists"
} # If
Write-Verbose "Copying files to $computer"
Copy-Item -Path $path -Destination $destination -ToSession $session
Write-Verbose "Closing $computer session"
Remove-PSSession -Session $session
} # foreach
} #PROCESS
END {
} #END
} #function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment