我正在编写一个 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”无法执行写入操作,因为以下范围) 被锁定