如何使用PowerShell在Azure中为我的订阅ID锁定存储帐户时获取孤立VHD列表

时间:2018-03-21 06:55:35

标签: powershell azure azure-storage-account

我正在编写一个PowerShell脚本,它会将我的孤儿vhds列表放入azure中,但如果我没有存储帐户的访问权限,那么我就会遇到问题。

除非锁定任何存储帐户,否则此方法无效。

登录Azure帐户

Login-AzureRmAccount

获取有关所有存储帐户的信息

$SA = Get-AzureRmStorageAccount

获取所有存储帐户中的VHD

$UMD = $SA | Get-AzureStorageContainer | Get-AzureStorageBlob | Where {$_.Name -like '*.vhd'}

过滤掉所有未锁定的VHD

$UMVHDS = $UMD | Where {$_.ICloudBlob.Properties.LeaseStatus -eq "Unlocked"}

获取所有托管磁盘

$MVHDS = Get-AzureRmDisk

过滤器管理磁盘,没有任何所有者,即孤立

$MVHD = $MVHDS | Where {$_.OwnerId -eq $null}

获取所有没有父对象的对象

$RmDiskInfo = foreach ($UMVHD in $UMVHDS)
{
    $StorageAccountName = if ($UMVHD.ICloudBlob.Parent.Uri.Host -match '([a-z0-9A-Z]*)(?=\.blob\.core\.windows\.net)') {$Matches[0]}
    $StorageAccount = $SA | Where { $_.StorageAccountName -eq $StorageAccountName }

    $Property = [ordered]@{
        AbsoluteUri = $UMVHD.ICloudBlob.Uri.AbsoluteUri;
        LeaseStatus = $UMVHD.ICloudBlob.Properties.LeaseStatus;
        LeaseState = $UMVHD.ICloudBlob.Properties.LeaseState;
        StorageType = $StorageAccount.Sku.Name;
        StorageAccountName = $StorageAccountName;
        ResourceGroupName = $StorageAccount.ResourceGroupName
    }
    New-Object -TypeName PSObject -Property $Property
}

将未管理的磁盘导出为CSV

$RmDiskInfo | Export-Csv -Path '.\UnusedUnmanagedVHDs.csv' -NoTypeInformation

将托管磁盘导出为CSV

$MVHD | Export-Csv -Path '.\UnusedManagedVHDs.csv' -NoTypeInformation

错误: 错误:Get-AzureStorageContainer:范围' / subscriptionsxxxxxx-xxxx-xxxx-bea7-31faae224077 / resourceGroups / Test-RG-BlobDeletion / provid ers / Microsoft.Storage / storageAccounts / str1test'因为以下范围被锁定,所以无法执行写操作

1 个答案:

答案 0 :(得分:0)

要阅读,请列出您必须使用链接到该存储帐户的密钥的容器内容,例如:

 $ctx = New-AzureStorageContext -StorageAccountName <name> -StorageAccountKey <key>
Get-AzureStorageContainer -Context $ctx

有关访问Blob和容器的更多信息,请访问:https://docs.microsoft.com/en-us/azure/storage/blobs/storage-manage-access-to-resources