Advertisement
Guest User

Untitled

a guest
Oct 4th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. $server = #list of servers
  2. $username = #username
  3. $password = #password
  4. $files = #list of files path to be copied
  5. foreach($server in $servers) {
  6. $pw = ConvertTo-SecureString $password -AsPlainText -Force
  7. $cred = New-Object Management.Automation.PSCredential ($username, $pw)
  8. $s = New-PSSession -computerName $server -credential $cred
  9. foreach($item in $files){
  10. $regex = $item | Select-String -Pattern '(^.*)\(.*)$'
  11. $destinationPath = $regex.matches.groups[1]
  12. $filename = $regex.matches.groups[2]
  13. #check if the file exists on local system
  14. if(Test-Path $item){
  15. #check if the path/file already exists on remote machine
  16. #First convert the path to UNC format before checking it
  17. $regex = $item | Select-String -Pattern '(^.*)\(.*)$'
  18. $filename = $regex.matches.groups[2]
  19. $fullPath = $regex.matches.groups[1]
  20. $fullPath = $fullPath -replace '(.):', '$1$'
  21. $unc = '\' + $server + '' + $fullPath
  22. Write-Host $unc
  23. Test-Path $unc #This always returns false even if file/path exists
  24. if(#path exists){
  25. Write-Host "Copying $filename to server $server"
  26. Copy-Item -ToSession $s -Path $item -Destination $destinationPath
  27. }
  28. else{
  29. #create the directory and then copy the files
  30. }
  31. }
  32. else{
  33. Write-Host "$filename does not exists at the local machine. Skipping this file"
  34. }
  35. }
  36. Remove-PSSession -Session $s
  37. }
  38.  
  39. Test-Path '\10.207.xxx.XXXC$TEST'
  40. False
  41.  
  42. Test-Path '\10.207.xxx.xxxC$TEST'
  43. True
  44. Test-Path '\localhostC$TEST'
  45. True
  46.  
  47. $existsOnRemote = Invoke-Command -Session $s {param($fullpath) Test-Path $fullPath } -argumentList $item.Fullname;
  48. if(-not $existsOnRemote){
  49. Copy-Item -Path $item.FullName -ToSession $s -Destination $item.Fullname;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement