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

60 lines
2.5 KiB
PowerShell
Raw Permalink Normal View History

# Function to get AZCopy, if it doesn't exist and update it, if it does
2023-09-15 14:38:54 +00:00
function Get-AZCopy
{
[CmdletBinding()]
param
(
2023-09-15 14:38:54 +00:00
[string]
$WingetUpdatePath
)
$AZCopyLink = (Invoke-WebRequest -Uri https://aka.ms/downloadazcopy-v10-windows -UseBasicParsing -MaximumRedirection 0 -ErrorAction SilentlyContinue).headers.location
$AZCopyVersionRegex = [regex]::new('(\d+\.\d+\.\d+)')
$AZCopyLatestVersion = $AZCopyVersionRegex.Match($AZCopyLink).Value
2023-09-15 14:38:54 +00:00
if ($null -eq $AZCopyLatestVersion -or '' -eq $AZCopyLatestVersion)
{
$AZCopyLatestVersion = '0.0.0'
}
2023-09-15 14:38:54 +00:00
if (Test-Path -Path ('{0}\azcopy.exe' -f $WingetUpdatePath) -PathType Leaf -ErrorAction SilentlyContinue)
{
$AZCopyCurrentVersion = & "$WingetUpdatePath\azcopy.exe" -v
$AZCopyCurrentVersion = $AZCopyVersionRegex.Match($AZCopyCurrentVersion).Value
2023-09-15 14:38:54 +00:00
Write-ToLog -LogMsg ('AZCopy version {0} found' -f $AZCopyCurrentVersion)
}
2023-09-15 14:38:54 +00:00
else
{
2023-09-15 14:38:54 +00:00
Write-ToLog -LogMsg 'AZCopy not already installed'
$AZCopyCurrentVersion = '0.0.0'
}
2023-09-15 14:38:54 +00:00
if (([version]$AZCopyCurrentVersion) -lt ([version]$AZCopyLatestVersion))
{
2023-09-15 14:38:54 +00:00
Write-ToLog -LogMsg ('Installing version {0} of AZCopy' -f $AZCopyLatestVersion)
$null = (Invoke-WebRequest -Uri $AZCopyLink -UseBasicParsing -OutFile ('{0}\azcopyv10.zip' -f $WingetUpdatePath))
2023-09-15 14:38:54 +00:00
Write-ToLog -LogMsg 'Extracting AZCopy zip file'
$null = (Expand-Archive -Path ('{0}\azcopyv10.zip' -f $WingetUpdatePath) -DestinationPath $WingetUpdatePath -Force -Confirm:$false)
$AZCopyPathSearch = (Resolve-Path -Path ('{0}\azcopy_*' -f $WingetUpdatePath))
2023-09-15 14:38:54 +00:00
if ($AZCopyPathSearch -is [array])
{
$AZCopyEXEPath = $AZCopyPathSearch[$AZCopyPathSearch.Length - 1]
}
2023-09-15 14:38:54 +00:00
else
{
$AZCopyEXEPath = $AZCopyPathSearch
}
2023-09-15 14:38:54 +00:00
Write-ToLog -LogMsg "Copying 'azcopy.exe' to main folder"
$null = (Copy-Item -Path ('{0}\azcopy.exe' -f $AZCopyEXEPath) -Destination ('{0}\' -f $WingetUpdatePath) -Force -Confirm:$false)
2023-09-15 14:38:54 +00:00
Write-ToLog -LogMsg 'Removing temporary AZCopy files'
$null = (Remove-Item -Path $AZCopyEXEPath -Recurse -Force -Confirm:$false)
$null = (Remove-Item -Path ('{0}\azcopyv10.zip' -f $WingetUpdatePath) -Force -Confirm:$false)
$AZCopyCurrentVersion = & "$WingetUpdatePath\azcopy.exe" -v
$AZCopyCurrentVersion = $AZCopyVersionRegex.Match($AZCopyCurrentVersion).Value
2023-09-15 14:38:54 +00:00
Write-ToLog -LogMsg ('AZCopy version {0} installed' -f $AZCopyCurrentVersion)
}
}