Last active
June 27, 2017 08:17
-
-
Save Bak-Jin-Hyeong/ab0647f067cf4698203ca3269531d539 to your computer and use it in GitHub Desktop.
DownloadAndUnzip.ps1 -Url "https://sourceforge.net/projects/glew/files/glew/2.0.0/glew-2.0.0.zip/download" -DownloadDir . -ExtractDir .
This file contains 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
param ( | |
[Parameter(Mandatory=$true)][string]$Uri, | |
[string]$DownloadDir, | |
[string]$ExtractDir, | |
[string]$DownloadFilePath | |
) | |
$invocation_dir = Split-Path $MyInvocation.MyCommand.Path | |
$response = Invoke-WebRequest -Uri $Uri -UserAgent [Microsoft.PowerShell.Commands.PSUserAgent]::FireFox | |
$response_path = $response.BaseResponse.ResponseUri.AbsolutePath.Split('/') | |
$filename_from_response = $response_path[$response_path.Length - 1] | |
if ($DownloadDir) | |
{ | |
if (![System.IO.Path]::IsPathRooted($DownloadDir)) | |
{ | |
$DownloadDir = Join-Path -Path $invocation_dir -ChildPath $DownloadDir | |
} | |
} | |
else | |
{ | |
$DownloadDir = $invocation_dir | |
} | |
if ($DownloadFilePath) | |
{ | |
if (![System.IO.Path]::IsPathRooted($DownloadFilePath)) | |
{ | |
$DownloadFilePath = Join-Path -Path $DownloadDir -ChildPath $DownloadFilePath | |
} | |
} | |
else | |
{ | |
$DownloadFilePath = Join-Path -Path $DownloadDir -ChildPath $filename_from_response | |
} | |
[IO.File]::WriteAllBytes($DownloadFilePath, $response.Content) | |
if ($ExtractDir) | |
{ | |
if (![System.IO.Path]::IsPathRooted($ExtractDir)) | |
{ | |
$ExtractDir = Join-Path -Path $invocation_dir -ChildPath $ExtractDir | |
} | |
Add-Type -AssemblyName System.IO.Compression.FileSystem | |
[System.IO.Compression.ZipFile]::ExtractToDirectory($DownloadFilePath, $ExtractDir) | |
} |
Author
Bak-Jin-Hyeong
commented
Jun 27, 2017
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment