【发布时间】:2021-01-23 23:28:24
【问题描述】:
我需要列出我们在 azure 存储帐户中拥有的所有 blob。 我们有大约 10'000 个容器和 7'000'000 个 blob。
我正在使用 PowerShell,但我是菜鸟。 有没有办法将此信息放入文本文件中? 我需要每个 blob 的名称和容器。如果我能得到尺寸和创建日期,那就太好了。
【问题讨论】:
标签: powershell azure-blob-storage azure-storage-account
我需要列出我们在 azure 存储帐户中拥有的所有 blob。 我们有大约 10'000 个容器和 7'000'000 个 blob。
我正在使用 PowerShell,但我是菜鸟。 有没有办法将此信息放入文本文件中? 我需要每个 blob 的名称和容器。如果我能得到尺寸和创建日期,那就太好了。
【问题讨论】:
标签: powershell azure-blob-storage azure-storage-account
这取决于您的 PowerShell $PSVersionTable.PSVersion 版本
如果你是5.1及以上,可以使用The Az module
然后我假设this cmdlet 和/或this one 带有通配符。
通常我会引导您完成确切的步骤,但我是一名 AWS 人员,我的 azure 经验非常有限。考虑到这一点,我认为它看起来像这样
你会得到你需要的一切(假设我是正确的)并将它存储在一个变量中
$allTheThings = Get-AzStorageContainer -Name * | Get-AzStorageBlob
然后,您可以通过抓取数组中的第一个对象并列出其所有属性来探索对象,并找出您需要从中获取哪些信息
# You can do this, which will just show the properties of the object
$allTheThings[0] | Get-Member
# Or this, which will show the values of each of the properties
$allTheThings[0] | Format-List *
确定要提取哪些信息后,您可以创建一个自定义类,然后使用 foreach 循环构建一个包含这些自定义对象的数组,并将其导出到 csv 文档。
class MySuperAwesomeClass
{
$PropertyIWant1
$PropertyIWant2
$PropertyIWant3
$PropertyIWant4
$PropertyIWant5
$PropertyIWant6
$PropertyIWant7
}
$allTheThings = Get-AzStorageContainer -Name * | Get-AzStorageBlob
$listofInfoIWant = @()
foreach($thing in $allTheThings)
{
$myCustomObj = New-Object -TypeName MySuperAwesomeClass
$myCustomObj.PropertyIWant1 = $thing.property1
$myCustomObj.PropertyIWant2 = $thing.property2
$myCustomObj.PropertyIWant3 = $thing.property3
$myCustomObj.PropertyIWant4 = $thing.property4
$myCustomObj.PropertyIWant5 = $thing.property5
$myCustomObj.PropertyIWant6 = $thing.property6
$myCustomObj.PropertyIWant7 = $thing.property7
$listofInfoIWant += $myCustomObj
}
$listofInfoIWant | Export-Csv -Path C:\temp\azStorageInfo.csv -NoTypeInformation
# this will auto-open the newly created csv doc, comment it out to not do that
ii C:\temp\azStorageInfo.csv
【讨论】:
$var = foreach(){} 方法。这个答案也不是这样。我正在使用最简单、最容易理解的方式来做 OP 想要的事情,因为他们说他们是 PS 新手。这与我没有键入类属性的原因相同。将System.Collections.Generic.List[System.String]$list 之类的东西扔给 PS 新手可能会让人不知所措