-
Notifications
You must be signed in to change notification settings - Fork 1
/
Download-and-Extract-AzCopy-to-specified-folder.ps1
135 lines (91 loc) · 6.7 KB
/
Download-and-Extract-AzCopy-to-specified-folder.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<#
.SYNOPSIS
A script used to download and extract the latest AzCopy Windows 64-bit executable (.exe) file into a specified foler (e.g. C:\Wbin\ folder).
.DESCRIPTION
The latest AzCopy Windows 64-bit executable (.exe) file will be downloaded from a static download link.
The downloaded .zip file will be extracted, and made availabele into the specified folder provided by the user when running the script.
At the end the directory location of the AzCopy executable will also be added to the system path for ease of use.
.NOTES
Filename: Download-and-Extract-AzCopy-to-specified-folder.ps1
Created: 02/03/21
Last modified: 10/01/22
Author: Wim Matthyssen
PowerShell: 5.1
Requires: -RunAsAdministrator
OS: Windows 10, Windows 11, Windows Server 2016, Windows Server 2019 and Windows Server 2022
Version: 2.0
Action: Change variables were needed to fit your needs.
Disclaimer: This script is provided "As Is" with no warranties.
.EXAMPLE
.\Download-and-Extract-AzCopy-to-specified-folder.ps1
.LINK
https://wmatthyssen.com/2021/03/03/powershell-azcopy-windows-64-bit-download-and-silent-installation/
#>
## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
## Variables
$managementToolsFolderName = Read-Host "Please enter your management or administrator tools foldername (e.g. Wbin or AdminTools)" # Example: Wbin or AdminTools
$managementToolsFolderPath = "C:\" + $managementToolsFolderName +"\"
$itemType = "Directory"
$azCopyFolderName = "AzCopy"
$azCopyFolder = $managementToolsFolderPath + $azCopyFolderName
$azCopyUrl = "https://aka.ms/downloadazcopy-v10-windows"
$azCopyZip = $managementToolsFolderPath + "azcopy.zip"
$global:currenttime= Set-PSBreakpoint -Variable currenttime -Mode Read -Action {$global:currenttime= Get-Date -UFormat "%A %m/%d/%Y %R"}
$foregroundColor1 = "Red"
$foregroundColor2 = "Yellow"
$writeEmptyLine = "`n"
$writeSeperatorSpaces = " - "
## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
## Check if PowerShell is running as Administrator, otherwise exit the script
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
$isAdministrator = $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
if ($isAdministrator -eq $false) {
Write-Host ($writeEmptyLine + "# Please run PowerShell as Administrator" + $writeSeperatorSpaces + $currentTime)`
-foregroundcolor $foregroundColor1 $writeEmptyLine
exit
}
## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
## Start script execution
Write-Host ($writeEmptyLine + "# Script started. Without any errors, it will need around 1 minute to complete" + $writeSeperatorSpaces + $currentTime)`
-foregroundcolor $foregroundColor1 $writeEmptyLine
## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
## Create the specfied "management or tools folder" folder if not exists (e.g. Wbin or AdminTools)
If(!(test-path $managementToolsFolderPath))
{
New-Item -Path "C:\" -Name $managementToolsFolderName -ItemType $itemType -Force | Out-Null
}
Write-Host ($writeEmptyLine + "# $managementToolsFolderName folder available under the C: drive" + $writeSeperatorSpaces + $currentTime)`
-foregroundcolor $foregroundColor2 $writeEmptyLine
## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
## Delete AzCopy folder if already exists in $adminToolsFolder folder
If(test-path $azCopyFolder)
{
Remove-Item $azCopyFolder -Recurse | Out-Null
}
Write-Host ($writeEmptyLine + "# $azCopyFolderName folder not available or existing one deleted" + $writeSeperatorSpaces + $currentTime)`
-foregroundcolor $foregroundColor2 $writeEmptyLine
## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
## Download, extract and cleanup the latest AzCopy zip file
Invoke-WebRequest $azCopyUrl -OutFile $azCopyZip
Expand-Archive -LiteralPath $azCopyZip -DestinationPath $managementToolsFolderPath -Force
Remove-Item $azCopyZip
Write-Host ($writeEmptyLine + "# azcopy.exe available in extracted folder" + $writeSeperatorSpaces + $currentTime)`
-foregroundcolor $foregroundColor2 $writeEmptyLine
## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
## Rename AzCopy version folder to AzCopy
$azCopyOriginalFolderName = Get-ChildItem -Path $managementToolsFolderPath -Name azcopy*
$azCopyFolderToRename = $managementToolsFolderPath + $azCopyOriginalFolderName
$azCopyFolderToRenameTo = $managementToolsFolderPath + $azCopyFolderName
Rename-Item $azCopyFolderToRename $azCopyFolderToRenameTo
Write-Host ($writeEmptyLine + "# $azCopyOriginalFolderName folder renamed to $azCopyFolderName" + $writeSeperatorSpaces + $currentTime)`
-foregroundcolor $foregroundColor2 $writeEmptyLine
## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
## Add the AzCopy folder to the PATH System variable
[Environment]::SetEnvironmentVariable("Path", $env:Path + ";$azCopyFolderToRenameTo", "Machine")
Write-Host ($writeEmptyLine + "# The directory location of the AzCopy executable is added to the system path" + $writeSpace + $writeSeperatorSpaces + $currentTime)`
-foregroundcolor $foregroundColor2 $writeEmptyLine
## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
## Write script completed
Write-Host ($writeEmptyLine + "# Script completed" + $writeSeperatorSpaces + $currentTime)`
-foregroundcolor $foregroundColor1 $writeEmptyLine
## ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------