术语 'AzCopy' 未被识别为 cmdlet、函数、脚本文件的名称

The term 'AzCopy' is not recognized as a name of a cmdlet, function, script file

我需要将 tables 从 table 存储复制到不同的存储帐户。尝试执行 AzCopy 时出现以下异常:

The term 'AzCopy' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

我已从门户连接到终端,并出现 powershell 提示:

问题似乎出在这一行:

    AzCopy /Source:$SrcTableUrl `
                      /Dest:$DstBlobUrl/$TableName `
                      /SourceKey:$SrcAccessKey `
                      /Destkey:$DstAccessKey

我们如何在 Azure 门户的终端中 运行 AzCopy 命令?

这是我尝试执行的完整代码 powershell 脚本:

# This simple PowerShell script will copy one or more Azure storage table from one location into another azure storage table
#
# Dependencies :
#   https://docs.microsoft.com/en-us/azure/storage/common/storage-use-azcopy
#   https://docs.microsoft.com/en-us/powershell/azure/overview?view=azps-1.6.0
#
# Usage :
#        Copy-AzureStorageTable -SrcStorageName "" -SrcAccessKey "" -DstStorageName "" -DstAccessKey "" -IncludeTable All  
#        Copy-AzureStorageTable -SrcStorageName "" -SrcAccessKey "" -DstStorageName "" -DstAccessKey "" -IncludeTable Table1,Table2,Table3  

function Copy-AzureStorageTable
{
    param
    (
        [parameter(Mandatory=$true)]
        [String]
        $SrcStorageName,

        [parameter(Mandatory=$true)]
        [String]
        $SrcAccessKey,

        [parameter(Mandatory=$true)]
        [String]
        $DstStorageName,

        [parameter(Mandatory=$true)]
        [String]
        $DstAccessKey,

        [parameter(Mandatory=$true)]
        [String[]]
        $IncludeTable
    )

    # Check if logged in
    Azure-Login

    # Source Account Storage Parameters
    $SrcContext = New-AzureStorageContext -StorageAccountName $SrcStorageName -StorageAccountKey $SrcAccessKey
    $SrcBaseUrl = "https://" + $SrcStorageName + ".table.core.windows.net/"

    # Destination Account Storage Parameters
    $DstContext = New-AzureStorageContext -StorageAccountName $DstStorageName -StorageAccountKey $DstAccessKey
    $DstTempContainer = "temptable"
    $DstBlobUrl = "https://" + $DstStorageName + ".blob.core.windows.net/$DstTempContainer"
    $DstTableUrl = "https://" + $DstStorageName + ".table.core.windows.net"

    # Create container in destination blob
    Write-Host "$DstTempContainer is not existing in $DstStorageName..."
    Write-Host "Creating container $DstTempContainer in $DstStorageName..."
    New-AzureStorageContainer -Name $DstTempContainer -Permission Off -Context $DstContext

    # Get all tables from source
    $SrcTables = Get-AzureStorageTable -Name "*" -Context $SrcContext
    foreach($table in $SrcTables)
    {
        $TableName = $table.Name                
        Write-Host "Table $TableName"

        # Validate if copy all table from source
        # Validate if table name is included in our list
        if(!$IncludeTable.Contains("All") -and !$IncludeTable.Contains($TableName))
        {
           Write-Host "Skipping table $TableName"
           return
        }
                
        Write-Host "Migrating Table $TableName"      
        $SrcTableUrl = $SrcBaseUrl + $TableName

        # Copy Table from source to blob destination. As far as I know there is way no way to copy table to table directly.
        # Alternatively, we will copy the table temporaryly into destination blob.
        # Take note to put the actual path of AzCopy.exe
        Write-Host "Start exporting table $TableName..."
        Write-Host "From    : $SrcTableUrl"
        Write-Host "To      : $DstBlobUrl/$TableName"

        AzCopy /Source:$SrcTableUrl `
                          /Dest:$DstBlobUrl/$TableName `
                          /SourceKey:$SrcAccessKey `
                          /Destkey:$DstAccessKey
   
        # Get the newly created blob
        Write-Host "Get all blobs in $DstTempContainer..."
        $CurrentBlob = Get-AzureStorageBlob -Container $DstTempContainer -Prefix $TableName -Context $DstContext

        # Loop and check manifest, then import blob to table
        foreach($blob in $CurrentBlob)
        {
            if(!$blob.Name.contains('.manifest'))
            {
                return
            }

            $manifest = $($blob.Name).split('/')[1]

            Write-Host "Start importing $TableName..."
            Write-Host "Source blob url : $DstBlobUrl/$TableName"
            Write-Host "Dest table url  : $DstTableUrl/$TableName"           
            Write-Host "Manifest name   : $manifest"
                
            # Import blob to table. Insert entity if missing and update entity if exists
            AzCopy /Source:$DstBlobUrl/$TableName `
                              /Dest:$DstTableUrl/$TableName `
                              /SourceKey:$DstAccessKey `
                              /DestKey:$DstAccessKey `
                              /Manifest:$manifest `
                              /EntityOperation:"InsertOrReplace"            
        }
    }

    # Delete temp table storage after export and import process
    Write-Host "Removing $DstTempContainer from destination blob storage..."
    Remove-AzureStorageContainer -Name $DstTempContainer -Context $DstContext -Force
}

# Login
function Azure-Login
{
    $needLogin = $true

    Try 
    {
        $content = Get-AzureRmContext

        if ($content) 
        {
            $needLogin = ([string]::IsNullOrEmpty($content.Account))
        } 
    } 
    Catch 
    {
        if ($_ -like "*Login-AzureRmAccount to login*") 
        {
            $needLogin = $true
        } 
        else 
        {
            throw
        }
    }

    if ($needLogin)
    {
        Login-AzureRmAccount
    }
}

Azure 门户云 shell 显示自 2021 年 11 月 10 日起使用 AzCopy 版本 10.6.1。在 7.3 版本之后删除了表之间复制的能力。

您需要从可以下载旧版本 AzCopy 的机器上运行脚本。

我的解决方案只是运行一个命令:

Install-Module -Name Az -Scope CurrentUser -Repository PSGallery -Force

在 powershell 中

之前:

azcopy 现在有效 脚本现在有效:

希望对您有所帮助