Created
June 27, 2017 22:55
-
-
Save BryanWilhite/3324664ead7ab3b236d61017b7e5c843 to your computer and use it in GitHub Desktop.
deploys to target folder from ZIP archive
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
trap | |
{ | |
Write-Output $_ | |
exit 1 | |
} | |
$id = [System.Security.Principal.WindowsIdentity]::GetCurrent() | |
Write-Output "Running as $($id.Name)..." | |
Add-Type -AssemblyName "System.IO" | |
Add-Type -AssemblyName "System.IO.Compression" | |
Add-Type -AssemblyName "System.IO.Compression.FileSystem" | |
$archiveFile = ".\web-deploy-package\DEV\My.Deployment.zip" | |
$root = "/PackageTmp/" | |
$target = "\\MyDev\target" | |
if(-not(Test-Path -Path $archiveFile -PathType Leaf)) | |
{ | |
Write-Output "The expected archive file ($archiveFile) is not here." | |
exit 1 | |
} | |
$archiveFile = Resolve-Path -Path $archiveFile | |
Write-Output "Resolved archive file: $archiveFile." | |
if(-not(Test-Path -Path $target -PathType Container)) | |
{ | |
Write-Output "The expected target folder ($target) is not here." | |
exit 1 | |
} | |
$fileMode = [System.IO.FileMode]::Open | |
$zipArchiveMode = [System.IO.Compression.ZipArchiveMode]::Read; | |
$leaveOpen = $false; | |
$overwrite = $true | |
$stream = New-Object System.IO.FileStream -ArgumentList @($archiveFile, $fileMode) | |
$zipFile = New-Object System.IO.Compression.ZipArchive -ArgumentList @($stream, $zipArchiveMode, $leaveOpen) | |
try | |
{ | |
$zipFile.Entries | | |
Where-Object { $_.FullName -like "*$root*" } | | |
ForEach-Object { | |
$start = $_.FullName.IndexOf($root) + $root.Length | |
$p = $_.FullName.Substring($start) | |
if($p.Length -eq 0) { return } | |
$p = [System.IO.Path]::Combine($target, $p) | |
$p = [System.IO.Path]::GetFullPath($p) | |
$p | |
if($p.EndsWith([System.IO.Path]::DirectorySeparatorChar)) { | |
if( -Not (Test-Path $p)) { | |
New-Item -ItemType directory -Path $p | Out-Null | |
} | |
return | |
} | |
[System.IO.Compression.ZipFileExtensions]::ExtractToFile($_, $p, $overwrite) | |
} | |
} | |
finally | |
{ | |
if ($null -ne $stream -and $stream -is [System.IDisposable]) | |
{ | |
$stream.Dispose() | |
} | |
if ($null -ne $zipFile -and $zipFile -is [System.IDisposable]) | |
{ | |
$zipFile.Dispose() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment