Tuesday 25 August 2020

powershell 17 transfer file between remote and local computers

 PS C:\Windows\System32> $session = new-pssession -computername 192.168.0.24 -credential $credential                     PS C:\Windows\System32>  $session

 Id Name            Transport ComputerName    ComputerType    State         ConfigurationName     Availability
 -- ----            --------- ------------    ------------    -----         -----------------     ------------            1 Runspace1       WSMan     192.168.0.24    RemoteMachine   Opened        Microsoft.PowerShell     Available 

remote desktop before transfer
#transfer file to remote
Copy-Item -Path C:\Users\bob\Desktop\redux.txt -Destination 'C:\Users\Public\Desktop' -ToSession $session

file copied to remote desktop
#check if file exist on remote
PS C:\Windows\System32> $fileExistOnRemote = invoke-command -session $session -scriptblock {Test-Path 'C:\users\public\desktop\redux.txt'}

PS C:\Windows\System32> $fileExistOnRemote
True 

#delete file on remote
PS C:\Windows\System32>  invoke-command -session $session -scriptblock {Remove-Item 'C:\users\public\desktop\redux.txt'}

file is deleted on remote

PS C:\Windows\System32> $fileExistOnRemote = invoke-command -session $session -scriptblock {Test-Path 'C:\users\public\desktop\redux.txt'}

PS C:\Windows\System32> $fileExistOnRemote
False 

remote D drive contains 2 png files
#transfer from remote to local
PS C:\Windows\System32> Copy-Item -FromSession $session -path 'd:\*.png' -Destination 'C:\Users\bob\Desktop'

all png files are copied to local desktop

reference:
copy file to remote

delete file on remote

check file existence

copy file from remote

copy file with specific extension

connect to remote

No comments:

Post a Comment