Deploying website files to multiple folders with exclusions
When automating deployments it is sometimes needed to create copy of your website/folder in different locations with some excluded files e.g. configuration files or temp folders.
The script below will help you to achieve that. It can be triggered automatically on your post-deployment action (possibly in your CI server) or executed manually. Script just copies all files from source folder into multiple locations, except for the files and sub-folders you specify. Files with the same names will be overridden in target locations.
param ( ##main paths [string]$Source_wwwroot = "c:\inetpub\wwwroot\sourceApp", [string]$Copy1_wwwroot = "c:\inetpub\wwwroot\copy1App", [string]$Copy2_wwwroot = "c:\inetpub\wwwroot\copy2App", ##copy exceptions $exclude = @("Web.config","Web.Debug.config","Web.Release.config"), $excludeMatch = @("TempFiles","TempImages","obj") ) if(-not(Test-Path $Source_wwwroot)) { "Source folder not found!"; return;} if(-not(Test-Path $Copy1_wwwroot)) { "Copy1 folder not found!"; return;} if(-not(Test-Path $Copy2_wwwroot)) { "Copy2 folder not found!"; return;} If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { "You need to run this script as admin!"; sleep 3000 return; } Write-Host "`n#######################`nThis script will copy files from: `n$Source_wwwroot `nto: `n$Copy1_wwwroot and`n$Copy2_wwwroot `n"; Write-Host "except files: $exclude"; Write-Host "except folders: $excludeMatch"; Read-host -prompt "`n#######################`nPress enter to start copying the files" function CopyFiles($from, $to){ [regex] $excludeMatchRegEx = ‘(?i)‘ + (($excludeMatch |foreach {[regex]::escape($_)}) –join “|”) + ‘’ Get-ChildItem -Path $from -Recurse -Exclude $exclude | where { $excludeMatch -eq $null -or $_.FullName.Replace($from, "") -notmatch $excludeMatchRegEx} | Copy-Item -Destination { if ($_.PSIsContainer) { Join-Path $to $_.Parent.FullName.Substring($from.length) } else { Join-Path $to $_.FullName.Substring($from.length) } } -Force -Exclude $exclude } "copying files for copy1..." CopyFiles $Source_wwwroot $Copy1_wwwroot "copying files for copy2..." CopyFiles $Source_wwwroot $Copy2_wwwroot "Done!"; sleep 10000
Enjoy!
Copy_websites