2023-09-15 14:33:51 +00:00
|
|
|
# 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
|
2023-09-15 14:33:51 +00:00
|
|
|
{
|
|
|
|
[CmdletBinding()]
|
|
|
|
param
|
|
|
|
(
|
2023-09-15 14:38:54 +00:00
|
|
|
[string]
|
|
|
|
$WingetUpdatePath
|
2023-09-15 14:33:51 +00:00
|
|
|
)
|
2023-09-15 14:38:54 +00:00
|
|
|
|
2023-09-15 14:33:51 +00:00
|
|
|
$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)
|
2023-09-15 14:33:51 +00:00
|
|
|
{
|
|
|
|
$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)
|
2023-09-15 14:33:51 +00:00
|
|
|
{
|
|
|
|
$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:33:51 +00:00
|
|
|
}
|
2023-09-15 14:38:54 +00:00
|
|
|
else
|
2023-09-15 14:33:51 +00:00
|
|
|
{
|
2023-09-15 14:38:54 +00:00
|
|
|
Write-ToLog -LogMsg 'AZCopy not already installed'
|
2023-09-15 14:33:51 +00:00
|
|
|
$AZCopyCurrentVersion = '0.0.0'
|
|
|
|
}
|
2023-09-15 14:38:54 +00:00
|
|
|
|
|
|
|
if (([version]$AZCopyCurrentVersion) -lt ([version]$AZCopyLatestVersion))
|
2023-09-15 14:33:51 +00:00
|
|
|
{
|
2023-09-15 14:38:54 +00:00
|
|
|
Write-ToLog -LogMsg ('Installing version {0} of AZCopy' -f $AZCopyLatestVersion)
|
2023-09-15 14:33:51 +00:00
|
|
|
$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'
|
2023-09-15 14:33:51 +00:00
|
|
|
$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])
|
2023-09-15 14:33:51 +00:00
|
|
|
{
|
|
|
|
$AZCopyEXEPath = $AZCopyPathSearch[$AZCopyPathSearch.Length - 1]
|
|
|
|
}
|
2023-09-15 14:38:54 +00:00
|
|
|
else
|
2023-09-15 14:33:51 +00:00
|
|
|
{
|
|
|
|
$AZCopyEXEPath = $AZCopyPathSearch
|
|
|
|
}
|
2023-09-15 14:38:54 +00:00
|
|
|
|
|
|
|
Write-ToLog -LogMsg "Copying 'azcopy.exe' to main folder"
|
2023-09-15 14:33:51 +00:00
|
|
|
$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'
|
2023-09-15 14:33:51 +00:00
|
|
|
$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)
|
2023-09-15 14:33:51 +00:00
|
|
|
}
|
|
|
|
}
|