【问题标题】:List all azure-blobs with metadata into a textfile (like "dir > listmyfolder. txt" in cmd )将所有带有元数据的 azure-blob 列出到文本文件中(如 cmd 中的“dir > listmyfolder.txt”)
【发布时间】:2021-01-23 23:28:24
【问题描述】:

我需要列出我们在 azure 存储帐户中拥有的所有 blob。 我们有大约 10'000 个容器和 7'000'000 个 blob。

我正在使用 PowerShell,但我是菜鸟。 有没有办法将此信息放入文本文件中? 我需要每个 blob 的名称和容器。如果我能得到尺寸和创建日期,那就太好了。

【问题讨论】:

    标签: powershell azure-blob-storage azure-storage-account


    【解决方案1】:

    这取决于您的 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
    

    【讨论】:

    • 我一直在挖掘这篇文章,直到你一遍又一遍地完成整个数组。最好只将 foreach 的输出捕获到变量中
    • @DougMaurer 你能澄清一下吗?因为这正是我正在做的事情。我将数组实例化一次,然后 foreach 创建一个自定义对象,设置所需属性的值,并将其添加到该数组中。
    • powershell.org/2013/09/… 每次你这样做时都会创建一个新数组,并且在大型数据集上它很慢。要么做 $var = foreach(){...} 要么使用 system.collections.generic.list
    • @DougMaurer 啊,现在我明白你的意思了。诚然,如果我要在时间上更有效,我会使用一个列表,然后附加到 foreach 中的列表。如果我不关心可读性,并且想要更短的代码,我会选择$var = foreach(){} 方法。这个答案也不是这样。我正在使用最简单、最容易理解的方式来做 OP 想要的事情,因为他们说他们是 PS 新手。这与我没有键入类属性的原因相同。将System.Collections.Generic.List[System.String]$list 之类的东西扔给 PS 新手可能会让人不知所措
    猜你喜欢
    • 2021-06-25
    • 2022-11-28
    • 2023-02-24
    • 1970-01-01
    • 1970-01-01
    • 2022-08-16
    • 2019-07-28
    • 2018-08-06
    • 2021-04-14
    相关资源
    最近更新 更多