在Azure PowerShell Runbook中更改Azure Blob存储层

时间:2019-09-13 19:09:57

标签: azure powershell azure-storage-blobs azure-runbook

我发现了几篇使用ICloudBlob.SetStandardBlobTier("Archive")来更改CloudBlockBlob的层的文章。因此,我整理了以下要在Azure PowerShell Runbook中运行的脚本。

Import-Module Azure

#Define storage account information
$StorageAccount = "xxxxx"
$StorageAccountKey = "xxxxx"
$containername = "xxxxx"

#Create a storage context
$context = New-AzureStorageContext -StorageAccountName $StorageAccount -StorageAccountKey $StorageAccountKey

# Get the blobs
$blobs = Get-AzureStorageBlob -Container $containername -Context $context
$blob = $blobs[0]

$blob.SetStandardBlobTier("Archive")

但这会产生以下错误消息

Method invocation failed because [Microsoft.WindowsAzure.Commands.Storage.Model.ResourceModel.AzureStorageBlob] does 
not contain a method named 'SetStandardBlobTier'.
At line:15 char:1
+ $blob.SetStandardBlobTier("Archive")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

当我调用Write-Output $blobs[0].ICloudBlob | Get-Member时,似乎我的对象确实没有这种方法:

[...]
RenewLease                    Method     void RenewLease(Microsoft.WindowsAzure.Storage.AccessCondition accessConditi...
RenewLeaseAsync               Method     System.Threading.Tasks.Task RenewLeaseAsync(Microsoft.WindowsAzure.Storage.A...
SetMetadata                   Method     void SetMetadata(Microsoft.WindowsAzure.Storage.AccessCondition accessCondit...
SetMetadataAsync              Method     System.Threading.Tasks.Task SetMetadataAsync(), System.Threading.Tasks.Task ...
SetProperties                 Method     void SetProperties(Microsoft.WindowsAzure.Storage.AccessCondition accessCond...
SetPropertiesAsync            Method     System.Threading.Tasks.Task SetPropertiesAsync(), System.Threading.Tasks.Tas...
Snapshot                      Method     Microsoft.WindowsAzure.Storage.Blob.CloudBlob Snapshot(System.Collections.Ge...
SnapshotAsync                 Method     System.Threading.Tasks.Task[Microsoft.WindowsAzure.Storage.Blob.CloudBlob] S...
StartCopy                     Method     string StartCopy(Microsoft.WindowsAzure.Storage.File.CloudFile source, Micro...
StartCopyAsync                Method     System.Threading.Tasks.Task[string] StartCopyAsync(Microsoft.WindowsAzure.St...
StartCopyFromBlob             Method     string StartCopyFromBlob(Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob ...
StartCopyFromBlobAsync        Method     System.Threading.Tasks.Task[string] StartCopyFromBlobAsync(Microsoft.Windows...
ToString                      Method     string ToString()                                                              
UploadFromByteArray           Method     void UploadFromByteArray(byte[] buffer, int index, int count, Microsoft.Wind...
[...]

我什至不知道如何从对象中读取当前层。我浏览了属性和元数据,但没有运气。

1 个答案:

答案 0 :(得分:1)

我可以重现您的问题,因为您使用的命令较旧,请按照以下步骤解决问题。

在运行手册所在的门户中导航至automation account-> Modules->检查是否有模块Az.Accounts 1.6.2Az.Storage 1.6.0,如果没有,请在Browse Gallery->搜索它们并Import(请注意,Az.Storage 1.6.0Az.Storage的依赖项,因此您需要首先导入Az.Accounts 1.6.2。 如果您已经拥有旧版本,只需单击它们即可删除并导入最新版本,如上。

然后在您的Runbook中,使用以下命令,它对我而言很好用。

#Define storage account information
$StorageAccount = "xxxxx"
$StorageAccountKey = "xxxxx"
$containername = "xxxxx"

#Create a storage context
$context = New-AzStorageContext -StorageAccountName $StorageAccount -StorageAccountKey $StorageAccountKey

# Get the blobs
$blobs = Get-AzStorageBlob -Container $containername -Context $context
$blob = $blobs[0]

$blob.ICloudBlob.SetStandardBlobTier("Archive")