第一篇文章。我的 Powershell 知识不太丰富。我试图列出“PublicAccess”属性设置为“开”的所有容器。我正在尝试使用MS提供的脚本。
$rgName = "<Resource Group name>"
$accountName = "<Storage Account Name>"
$storageAccount = Get-AzStorageAccount -ResourceGroupName $rgName -Name $accountName
$ctx = $storageAccount.Context
Get-AzStorageContainer -Context $ctx | Select Name, PublicAccess
但是我需要在大量存储帐户上执行此操作。过去我曾使用“foreach($item in $list)”将内容传递到一个小脚本中。但绝不适用于多个列表。有人可以帮忙吗?
最佳答案
根据评论中您的扩展要求,这应该可行。 (我没有对其进行测试,也没有处理任何与权限或其他相关的潜在错误)
# Create a collection for the items found.
$StorageAccounts = [System.Collections.Generic.List[System.Object]] @{}
# Loop through the available Azure contexts.
foreach ($Context in (Get-AzContext -ListAvailable)) {
# Set each subscription in turn, voiding the output.
[System.Void](Set-AzContext -Context $Context)
# Create an object with the container name, public access values and the name of the storage account/subscription.
$StorageAccountInfo = Get-AZStorageAccount | Get-AzStorageContainer | Select-Object Name, PublicAccess, @{l = "StorageAccountName"; e = { $_.Context.StorageAccountName } }, @{l = "Subscription"; e = { $Context.Subscription.Name } }
# If there is data found, add it to the collection.
if ($null -ne $StorageAccountInfo) {
$StorageAccounts.AddRange($StorageAccountInfo)
}
}
# Export the collected information to Csv.
$StorageAccounts | Export-Csv -Path .\myStorageAccounts.csv -NoClobber -Encoding utf8
关于azure - 存储帐户中的公共(public)容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73220913/