Saturday, July 29, 2023

PowerShell: azcopy Working Around the New Version Available Information Message

One of the most optimal ways to upload and download blobs to/from Azure storage containers is the azcopy utility (see azcopy). This utility can be used from the command-line or can be included in a PowerShell script such as the following:

[string] $azCopyPath = 'C:\bin\azcopy.exe'
[string] $blobSasUrl = 'put your own storage URL plus SAS token here'

& $azCopyPath list $blobSasUrl 

The above example is one of cases where azcopy is not used to upload or download files/folders/blobs. The "azcopy list" command will list the blobs for a given storage URL (see azcopy list).

The output from the above command is as follows:

INFO: azcopy.10_19_0.exe 10.19.0: A newer version 10.20.0 is available to download

INFO: a.txt;  Content Length: 1.20 KiB
INFO: b.txt;  Content Length: 1.20 KiB
INFO: c.txt;  Content Length: 1.20 KiB
INFO: d.txt;  Content Length: 1.20 KiB

The desired output is as follows (sans the upgrade available message):



The azcopy utility creates a message on the  information data stream that an upgrade is available. The output from azcopy list is also provided on the information data stream. There is no way to suppress the "newer version is available to download" message.

The following PowerShell code uses "Select-Object -Skip" (see Select-Object) to remove the upgrade informational message and returns only the data retrieved by azcopy list:

[string] $upgradeAvialableSuffix = 'is available to download'

function Get-AzCopyList {
    param(
        [Parameter(Mandatory=$true)]
        [string] $azCopyFilePath,
        [Parameter(Mandatory=$true)]
        [string] $blobUrlWithSasToken
        )

    [string[]] $results = & $azCopyPath list $blobSasUrl 

    # always return an array
    if (($null -eq $results) -or (0 -eq $results.Length)) {
        return ,[string[]]::new(0)
    }

    [int] $index = 0

    if ($results[0].EndsWith($upgradeAvialableSuffix)) {
        [bool] $endMarkerFound = $false

        foreach ($result in $results) {
            $index++
            if (0 -eq $result.Length) {
                $endMarkerFound = $true
                break
            }
        }

        if ($false -eq $endMarkerFound) {
            throw "Upgrade message not delineated from results $($result[0])"
        }
    }

    return $results | Select-Object -Skip $index 
}

[string] $azCopyPath = 'C:\bin\azcopy.exe'
[string] $blobSasUrl = 'put your own storage URL plus SAS token here'
[string[]] $results = Get-AzCopyList $azCopyPath $blobSasUrl

$results

The output from above code is:
INFO: a.txt;  Content Length: 1.20 KiB
INFO: b.txt;  Content Length: 1.20 KiB
INFO: c.txt;  Content Length: 1.20 KiB
INFO: d.txt;  Content Length: 1.20 KiB



No comments :

Post a Comment