使用Powershell将Azure块Blob快照还原到另一个容器时出现问题

时间:2019-06-03 21:00:59

标签: azure powershell blob storage restore

我正在尝试为azure blob存储容器中的所有数据恢复昨天的快照。

$StorageAccountName = "SOURCE_STORAGE_ACCOUNT"
$StorageAccountKey = "STORAGE_ACCOUNT_KEY"
$ContainerName = "SOURCE_CONTAINER"
$Ctx = New-AzureStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey 
$ListBlob = Get-AzureStorageBlob –Context $Ctx -Container $ContainerName | Where-Object {$_.ICloudBlob.IsSnapshot -and $_.SnapshotTime -ne $null -and (($_.SnapshotTime.localdatetime).ToString('yyyyMMdd') -eq $ySnap)}
$DestContainerName = "DESTINATION_CONTAINER"

for ($i=0; $i -lt $ListBlob.length; $i++) {
    $snapshot=$ListBlob[$i]
    [Microsoft.WindowsAzure.Storage.Blob.CloudBlob] $snapshot.ICloudBlob
    Start-AzureStorageBlobCopy –Context $Ctx -ICloudBlob $snapshot -DestContainer $DestContainerName
}

一切正常,直到:

[Microsoft.WindowsAzure.Storage.Blob.CloudBlob] $snapshot.ICloudBlob

我收到的错误是:

Cannot convert the "Microsoft.Azure.Storage.Blob.CloudBlockBlob" value of type "Microsoft.Azure.Storage.Blob.CloudBlockBlob" to type "Microsoft.WindowsAzure.Storage.Blob.CloudBlob".
At line:1 char:1
+ [Microsoft.WindowsAzure.Storage.Blob.CloudBlob] $snapshot.ICloudBlob
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidArgument: (:) [], RuntimeException
+ FullyQualifiedErrorId : ConvertToFinalInvalidCastException

我如何将$ snapshot从CloudBlockBlob投射到CloudBlob?还是我走错路了?

谢谢。

1 个答案:

答案 0 :(得分:0)

我很傻。解决方案是使用.ICloudBlob属性,而不是尝试将其强制转换为CloudBlob。

即。

Start-AzureStorageBlobCopy –Context $Ctx -ICloudBlob $snapshot.ICloudBlob -DestContainer $DestContainerName