使用Get-ChildItem批量检查所有.policy文件夹下的文件是否合规
你可以通过先递归检索所有.policy文件夹、再逐个校验的方式实现需求,参考实现代码如下:
# 定义允许的文件名称列表 $allowedFiles = @('azureDeploy.parameters.json', 'azureDeploy.json') # 递归检索所有名称为 .policy 的文件夹,替换路径为你需要扫描的根目录 $policyFolders = Get-ChildItem -Path "替换为你的扫描根目录路径" -Directory -Recurse -Filter ".policy" # 逐个校验每个 .policy 文件夹内的文件 foreach ($folder in $policyFolders) { Write-Host "校验路径:$($folder.FullName)" $currentFiles = (Get-ChildItem -Path $folder.FullName -File -Recurse).Name $currentFiles | Should -BeIn $allowedFiles }
补充说明:
- 如果你不需要校验
.policy文件夹内子目录的文件,可以删除第二个Get-ChildItem后的-Recurse参数 - 如果需要明确输出非法文件名称方便排查,可以在foreach循环内追加判断逻辑:
$invalidFiles = $currentFiles | Where-Object { $_ -notin $allowedFiles } if ($invalidFiles) { Write-Host "发现非法文件:$($invalidFiles -join '、')" -ForegroundColor Red }
内容的提问来源于stack exchange,提问作者Nadia Hansen
相关产品推荐
相关产品推荐

