Blob 属性 - 除了通过 ForEach 循环之外,是否有更好的方法从该集合中进行选择?

Blob property - Is there a better way of picking from this collection other than through a ForEach loop?

我有以下代码获取 blob 的属性集合,然后使用 foreach 循环定位选定的 属性 值。有没有不涉及循环遍历 PowerShell 7

中的集合的更好的方法
$Blobs = Get-AzStorageBlob -Container $containerName -Context $ctx  
ForEach ($Blob in $Blobs){
    if($Blob.Name.IndexOf($blobName) -ge 0)
    {          
        if (Get-Member -InputObject $Blob.ICloudBlob.Properties -Name $blobPropertyName -MemberType Property) {
            $retValue = $Blob.ICloudBlob.Properties.$blobPropertyName
            break;
        }            
    } else{
        Write-Host "Blob not found!"
    }
}

未测试:

(Get-AzStorageBlob -Blob "*$blobName*" -Container $containerName -Context $ctx).
  ICloudBlob.Properties.$blobPropertyName
  • Get-AzStorageBlob's -Blob parameter accepts wildcard expression,因此您无需手动遍历所有 blob 即可找到感兴趣的 blob。

  • 请注意,如果通配符 "*$blobName*" 匹配 多个 blob,该命令也将起作用,因为从 v3 开始,PowerShell 具有称为 member-access enumeration,它能够访问 集合 上的成员(属性 或方法)并将其隐式应用于 它的每个元素 结果收集在一个数组中

  • 只要Set-StrictMode关闭(默认)或者最高设置为-Version 1,上面会简单return $null 如果不存在匹配的 blob 或者如果匹配的 blob 没有目标 属性.