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-ToLog "AZCopy version $AZCopyCurrentVersion found"
2023-03-07 15:17:40 +00:00
}
else {
Write-ToLog "AZCopy not already installed"
2023-03-07 15:17:40 +00:00
$AZCopyCurrentVersion = "0.0.0"
}
if (([version] $AZCopyCurrentVersion) -lt ([version] $AZCopyLatestVersion)) {
Write-ToLog "Installing version $AZCopyLatestVersion of AZCopy"
2023-03-25 19:12:51 +00:00
Invoke-WebRequest -Uri $AZCopyLink -UseBasicParsing -OutFile "$WingetUpdatePath\azcopyv10.zip"
Write-ToLog "Extracting AZCopy zip file"
2023-03-07 15:17:40 +00:00
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-ToLog "Copying 'azcopy.exe' to main folder"
2023-03-07 15:17:40 +00:00
Copy-Item "$AZCopyEXEPath\azcopy.exe" -Destination "$WingetUpdatePath\"
Write-ToLog "Removing temporary AZCopy files"
2023-03-07 15:17:40 +00:00
Remove-Item -Path $AZCopyEXEPath -Recurse
Remove-Item -Path "$WingetUpdatePath\azcopyv10.zip"
$AZCopyCurrentVersion = & "$WingetUpdatePath\azcopy.exe" -v
$AZCopyCurrentVersion = $AZCopyVersionRegex.Match($AZCopyCurrentVersion).Value
Write-ToLog "AZCopy version $AZCopyCurrentVersion installed"
2023-03-07 15:17:40 +00:00
}
}