修改PowerShell脚本实现Azure存储Blob按文件夹统计并动态生成匹配列
修改PowerShell脚本实现Azure存储Blob按文件夹统计并动态生成匹配列
看起来你已经搞定了基础的文件夹Blob统计和匹配检查,现在的核心需求是把动态的搜索字符串直接作为CSV的列名,而不是把所有结果塞到一个MatchStatus列里对吧?我帮你调整下脚本,刚好能满足你动态列的需求。
修改后的完整脚本
function Get-FolderBlobCount { param ( [string]$StorageAccountName, [string]$StorageAccountKey, [string]$ContainerName, [string]$SubPath = "", [string[]]$SearchStrings, [string]$OutputFilePath ) # Authenticate to Azure Storage $context = New-AzStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey # Get the list of blobs in the specified container and folder $blobs = Get-AzStorageBlob -Context $context -Container $ContainerName -Prefix $SubPath # Group blobs by folder $blobsByFolder = $blobs | Group-Object { $_.Name -replace "/[^/]+$" } # Initialize a result array to store folder information $result = @() # Iterate through each folder foreach ($folder in $blobsByFolder) { # Initialize blob count $blobCount = $folder.Group.Count # 初始化每个搜索字符串的匹配状态为False $matchStatuses = @{} foreach ($str in $SearchStrings) { $matchStatuses[$str] = $false } # 遍历当前文件夹下的所有Blob,检查每个搜索字符串的匹配情况 foreach ($blob in $folder.Group) { foreach ($str in $SearchStrings) { if (-not $matchStatuses[$str] -and $blob.Name -like $str) { # 只要找到一个匹配就设为True,不需要重复检查 $matchStatuses[$str] = $true } } } # 动态构建结果对象:先添加基础属性 $folderResult = [PSCustomObject]@{ FolderName = $folder.Name BlobCount = $blobCount } # 遍历搜索字符串,动态添加对应的属性,属性名就是搜索串本身 foreach ($str in $SearchStrings) { $folderResult | Add-Member -MemberType NoteProperty -Name $str -Value $matchStatuses[$str] } # 将结果加入数组 $result += $folderResult # 调试输出 Write-Host "Folder: $($folder.Name), Blob Count: $blobCount, Matches: $($matchStatuses.Values -join ', ')" } # 导出到CSV,动态列会自动被识别 $result | Export-Csv -Path $OutputFilePath -NoTypeInformation -UseQuotes AsNeeded Write-Host "Results written to $OutputFilePath" } # 配置你的Azure存储信息 $storageAccountName = "X" $storageAccountKey = "Y" $containerName = "Z" $subPath = "A" # 留空表示根路径 $searchStrings = @( "*X_*Y_*Z001*.jpg", "*X_*Y_*Z002*.jpg", "*X_*Y_*Z003*.jpg", "*X_*Y_*Z004*.jpg", "*X_*Y_*Z005*.jpg", "*X_*Y_*Z006*.jpg" ) # 动态的搜索字符串,数量和名称可任意调整 $outputFilePath = "C:\OutputFileTT.csv" # 调用函数 Get-FolderBlobCount -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey -ContainerName $containerName -SubPath $subPath -SearchStrings $searchStrings -OutputFilePath $outputFilePath
关键修改说明
动态属性构建:
不再使用固定的MatchStatus属性,而是先创建包含FolderName和BlobCount的基础对象,然后通过Add-Member动态添加每个搜索字符串对应的属性,属性名完全等于你定义的搜索串,完美匹配你要的列名需求。匹配状态的高效维护:
用哈希表$matchStatuses来维护每个搜索字符串的匹配状态,遍历Blob时只要找到一次匹配就标记为True,避免重复检查,提升效率。CSV导出适配:
PowerShell的Export-Csv会自动识别动态添加的属性,将它们作为CSV的列,列的顺序和你在$SearchStrings中定义的顺序完全一致,不管你传多少个搜索字符串都能自动适配。
最终输出效果
导出的CSV会和你期望的格式完全一致,示例如下:
FolderName,BlobCount,"*X_*Y_*Z001*.jpg","*X_*Y_*Z002*.jpg","*X_*Y_*Z003*.jpg","*X_*Y_*Z004*.jpg","*X_*Y_*Z005*.jpg","*X_*Y_*Z006*.jpg" 4010009104797,366,False,True,True,True,True,True 4010009106951,366,False,True,True,True,True,True 4010009107064,366,False,True,True,True,True,True
备注:内容来源于stack exchange,提问作者Julian
相关产品推荐
相关产品推荐

