Last active
November 11, 2020 15:38
-
-
Save JohnL4/32a45545321f8611d1981f6f0c7e76e8 to your computer and use it in GitHub Desktop.
Mirror a directory of a static web site from one Windows machine to another, using a PowerShell Session instead of CIFS/SMB
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Get a handle to the directory. (This won't be a string, but a filesystem object, so you can do it however you need to.) | |
# ('gi' is Get-Item) | |
$dirToPush = gi work\Sunrise\DischargeDisposition\Defect\4960223-2nd-tab-bool-set-activity-not-completed | |
# Get the full path WITHOUT the drive specifier (i.e., rip off the "C:" part) | |
$dirToPushNoq = $dirToPush.FullName | split-path -noq | |
# Start a new PowerShell session (default idle timeout for these things is 2 hours, apparently) | |
# ('nsn' is New-PSSession) | |
$vm = nsn NedDevSrvJ6L | |
# ------------------------------------------ Push entire directory subtree ------------------------------------------- | |
# (Use if you created a new subdirectory, because the incremental approach below won't work for the new subdirectory.) | |
# Do the copy | |
cp $dirToPush (join-path "c:\inetpub\wwwroot" (split-path -parent $dirToPushNoq)) -ToSession $vm -rec -for -excl "*~" | |
# OR (if you don't want to manage another variable)... | |
cp $dirToPush (join-path "c:\inetpub\wwwroot" $($dirToPush.FullName ` | |
| split-path -noq ` | |
| split-path -parent)) ` | |
-ToSession $vm -rec -for -excl "*~" | |
<# | |
(Explanation of the above command line: | |
'join-path' is pretty forgiving, so we don't have to worry about leading slashes. | |
We use 'split-path -parent' to rip off the last directory name in the destination, since we're copying the source | |
directory by name. | |
'-rec -for' is -recursive -force, to overwrite whatever was already there (probably old versions of the files). | |
'-excl' specifies a LIST (comma-delimited) of wildcard patterns to be excluded from the copy. If you only have one | |
pattern, you can probably use -filter instead, since that's supposed to be more efficient. In the case of a | |
smallish directory, it probably doesn't make any difference? | |
#> | |
# ------------------------------------------------ Incremental update ------------------------------------------------ | |
# See https://github.com/JohnL4/PowerShell/blob/master/Copy-UpdatedFilesToSessionWwwroot.ps1 before embarking on the below; | |
# it's an improvement. | |
# Update w/recent changes; use 'tee' to capture what got sent and display later (because -pass doesn't work on 'cp' cmd | |
# w/-tosession). Only ship files, not entire recently-updated directories. | |
ls $dirToPush -rec ` | |
| ? {$_.GetType() -eq [IO.FileInfo]} ` | |
| ? {$_.LastWriteTime -ge (Get-Date).AddMinutes( -60)} ` | |
| tee -var sentFiles ` | |
| cp -dest (join-path "c:\inetpub\wwwroot" $dirToPushNoq) -tosession $vm -rec -for -excl "*~" ` | |
; $sentFiles | |
# OR (because the above doesn't properly recurse and mirror the entire tree to the destination, if you have a tree) | |
# (BUT NOTE: if you created a new subdirectory, this trick won't work, because it only works on files)... | |
pushd (Split-Path $dirToPush -qual) # -relative option on Resolve-Path affected by cwd, apparently. | |
ls $dirToPush -rec ` | |
| ? {$_.GetType() -eq [IO.FileInfo]} ` | |
| ? {-not ($_.FullName -match '/\.git/')} ` | |
| ? {-not ($_.Name -match '~$')} ` | |
| ? {-not ($_.Name -match '^#.*#$')} ` | |
| ? {-not ($_.Name -match '^\.#')} ` | |
| ? {$_.LastWriteTime -ge (Get-Date).AddMinutes( -15)} ` | |
| Resolve-Path -relative ` | |
| tee -var sentFiles ` | |
| % { cp $_ (join-path "c:\inetpub\wwwroot" $_) -tosession $vm -for -pass} | |
echo $sentFiles | |
popd | |
# Clean up by closing/removing the remote session | |
# ('rsn' is Remove-PSSession) | |
# (You can also remove sessions by id) | |
rsn $vm | |
# Verify by listing current sessions. List returned will include ids, so you can clean up by id if you don't have variables any more. | |
# ('gsn' is Get-PSSession) | |
gsn | |
# For a solution using powershell workflow to copy files in parallel, see also: https://stackoverflow.com/a/52139587/370611 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
(Looks like it's about twice as fast as CIFS/SMB.)