wingetautoupdate/Winget-AutoUpdate/functions/Get-AZCopy.ps1

50 lines
2.1 KiB
PowerShell
Raw Normal View History

2023-03-31 15:56:07 +00:00
#Function to get AZCopy, if it doesn't exist and update it, if it does
2023-03-07 15:17:40 +00:00
2023-03-31 15:56:07 +00:00
Function Get-AZCopy ($WingetUpdatePath) {
2023-03-07 15:17:40 +00:00
2023-03-25 19:12:51 +00:00
$AZCopyLink = (Invoke-WebRequest -Uri https://aka.ms/downloadazcopy-v10-windows -UseBasicParsing -MaximumRedirection 0 -ErrorAction SilentlyContinue).headers.location
2023-03-07 15:17:40 +00:00
$AZCopyVersionRegex = [regex]::new("(\d+\.\d+\.\d+)")
$AZCopyLatestVersion = $AZCopyVersionRegex.Match($AZCopyLink).Value
if ($null -eq $AZCopyLatestVersion -or "" -eq $AZCopyLatestVersion) {
$AZCopyLatestVersion = "0.0.0"
}
if (Test-Path -Path "$WingetUpdatePath\azcopy.exe" -PathType Leaf) {
$AZCopyCurrentVersion = & "$WingetUpdatePath\azcopy.exe" -v
2023-03-31 15:56:07 +00:00
$AZCopyCurrentVersion = $AZCopyVersionRegex.Match($AZCopyCurrentVersion).Value
Write-Log "AZCopy version $AZCopyCurrentVersion found"
2023-03-07 15:17:40 +00:00
}
else {
2023-03-31 15:56:07 +00:00
Write-Log "AZCopy not already installed"
2023-03-07 15:17:40 +00:00
$AZCopyCurrentVersion = "0.0.0"
}
if (([version] $AZCopyCurrentVersion) -lt ([version] $AZCopyLatestVersion)) {
2023-03-31 15:56:07 +00:00
Write-Log "Installing version $AZCopyLatestVersion of AZCopy"
2023-03-25 19:12:51 +00:00
Invoke-WebRequest -Uri $AZCopyLink -UseBasicParsing -OutFile "$WingetUpdatePath\azcopyv10.zip"
2023-03-07 15:17:40 +00:00
Write-Log "Extracting AZCopy zip file"
Expand-archive -Path "$WingetUpdatePath\azcopyv10.zip" -Destinationpath "$WingetUpdatePath" -Force
2023-03-31 15:56:07 +00:00
$AZCopyPathSearch = Resolve-Path -path "$WingetUpdatePath\azcopy_*"
2023-03-07 15:17:40 +00:00
if ($AZCopyPathSearch -is [array]) {
$AZCopyEXEPath = $AZCopyPathSearch[$AZCopyPathSearch.Length - 1]
}
else {
$AZCopyEXEPath = $AZCopyPathSearch
}
Write-Log "Copying 'azcopy.exe' to main folder"
Copy-Item "$AZCopyEXEPath\azcopy.exe" -Destination "$WingetUpdatePath\"
Write-Log "Removing temporary AZCopy files"
Remove-Item -Path $AZCopyEXEPath -Recurse
Remove-Item -Path "$WingetUpdatePath\azcopyv10.zip"
$AZCopyCurrentVersion = & "$WingetUpdatePath\azcopy.exe" -v
$AZCopyCurrentVersion = $AZCopyVersionRegex.Match($AZCopyCurrentVersion).Value
Write-Log "AZCopy version $AZCopyCurrentVersion installed"
}
}