Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $server = #list of servers
- $username = #username
- $password = #password
- $files = #list of files path to be copied
- foreach($server in $servers) {
- $pw = ConvertTo-SecureString $password -AsPlainText -Force
- $cred = New-Object Management.Automation.PSCredential ($username, $pw)
- $s = New-PSSession -computerName $server -credential $cred
- foreach($item in $files){
- $regex = $item | Select-String -Pattern '(^.*)\(.*)$'
- $destinationPath = $regex.matches.groups[1]
- $filename = $regex.matches.groups[2]
- #check if the file exists on local system
- if(Test-Path $item){
- #check if the path/file already exists on remote machine
- #First convert the path to UNC format before checking it
- $regex = $item | Select-String -Pattern '(^.*)\(.*)$'
- $filename = $regex.matches.groups[2]
- $fullPath = $regex.matches.groups[1]
- $fullPath = $fullPath -replace '(.):', '$1$'
- $unc = '\' + $server + '' + $fullPath
- Write-Host $unc
- Test-Path $unc #This always returns false even if file/path exists
- if(#path exists){
- Write-Host "Copying $filename to server $server"
- Copy-Item -ToSession $s -Path $item -Destination $destinationPath
- }
- else{
- #create the directory and then copy the files
- }
- }
- else{
- Write-Host "$filename does not exists at the local machine. Skipping this file"
- }
- }
- Remove-PSSession -Session $s
- }
- Test-Path '\10.207.xxx.XXXC$TEST'
- False
- Test-Path '\10.207.xxx.xxxC$TEST'
- True
- Test-Path '\localhostC$TEST'
- True
- $existsOnRemote = Invoke-Command -Session $s {param($fullpath) Test-Path $fullPath } -argumentList $item.Fullname;
- if(-not $existsOnRemote){
- Copy-Item -Path $item.FullName -ToSession $s -Destination $item.Fullname;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement