OpsaC - Operating as PowerShell code
published: March 7, 2019 author: Tinu tags: PowerShell categories: PowerShell-Remoting
Copy items from a local to a remote computer using a psremote-session.
function Copy-ToVirtualMachine{
<#
.SYNOPSIS
Copy to remote computer
.DESCRIPTION
Copy content from the local computer to a remote computer using psremote-session
.PARAMETER ComputerName
Remote computer to copy to
.PARAMETER creds
Credentials of the remote computer
.PARAMETER Source
Source folder on the local computer
.PARAMETER Destination
Destination folder on the remote computer
.EXAMPLE
Copy-ToVirtualMachine -ComputerName 'Server01' - creds $creds -Source 'C:\Export\*' -Destination 'C:\Import'
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[String] $ComputerName,
[Parameter(Mandatory=$true)]
[PSCredential] $creds,
[Parameter(Mandatory=$false)]
[String] $Source = 'C:\Export\*',
[Parameter(Mandatory=$false)]
[String] $Destination = 'C:\Import'
)
$function = $($MyInvocation.MyCommand.Name)
Write-Host "$(Get-Date -f 'yyyy-MM-dd HH:mm:ss.ffff') Starting $function" -ForegroundColor Yellow
$rsession = New-PSSession -ComputerName $ComputerName -Credential $creds
Copy-Item -ToSession $rsession -Path $Source -Destination $Destination -Force -Recurse
Remove-PSSession -Session $rsession
Write-Host "$(Get-Date -f 'yyyy-MM-dd HH:mm:ss.ffff') End $function" -ForegroundColor Yellow
}
function Copy-FromVirtualMachine{
<#
.SYNOPSIS
Copy from a remote computer
.DESCRIPTION
Copy content from a remote computer to the local computer using psremote-session
.PARAMETER ComputerName
Remote computer to copy from
.PARAMETER creds
Credentials of the remote computer
.PARAMETER Source
Source folder on the remote computer
.PARAMETER Destination
Destination folder on the local computer
.EXAMPLE
Copy-FromVirtualMachine -ComputerName 'Server01' - creds $creds -Source 'C:\Export\*' -Destination 'C:\Import'
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[String] $ComputerName,
[Parameter(Mandatory=$true)]
[PSCredential] $creds,
[Parameter(Mandatory=$false)]
[String] $Source = 'C:\Export\*',
[Parameter(Mandatory=$false)]
[String] $Destination = 'C:\Import'
)
$function = $($MyInvocation.MyCommand.Name)
Write-Host "$(Get-Date -f 'yyyy-MM-dd HH:mm:ss.ffff') Starting $function" -ForegroundColor Yellow
$rsession = New-PSSession -ComputerName $ComputerName -Credential $creds
Copy-Item -FromSession $rsession -Path $Source -Destination $Destination -Force -Recurse
Remove-PSSession -Session $rsession
Write-Host "$(Get-Date -f 'yyyy-MM-dd HH:mm:ss.ffff') End $function" -ForegroundColor Yellow
}
$remotehost = 'W2K16RC-SRV03'
$remotecreds = Get-Credential -Message "Enter the credentials to connect to $remotehost" -UserName 'Administrator'
Copy-ToVirtualMachine -ComputerName $remotehost - creds $remotecreds -Source 'C:\Export\*' -Destination 'C:\Import'
Copy-FromVirtualMachine -ComputerName $remotehost - creds $remotecreds -Source 'C:\Export\*' -Destination 'C:\Import'
New-PSSession and Copy-Item on Microsoft Docs.