powershell - 有人可以帮我制作这个 PowerShell Recursive
问题描述
我似乎无法让这个 PowerShell 递归遍历所有目录并进行我需要的更改,以确保当我的 terraform 脚本作为 Azure devops 中的构建帐户应用时,它们可以访问我的 git repos 中的模块。
$InputFiles = Get-Item "$(build.artifactstagingdirectory)/Terraform/*.tf"
$OldString = "git::https://dev.azure.com"
$NewString = "git::https://$(System.AccessToken)@dev.azure.com"
Write-Host $NewString
$InputFiles | ForEach {
(Get-Content -Path $_.FullName).Replace($OldString, $NewString) | Set-Content -Path $_.FullName
}
此代码适用于顶级目录,但不处理任何子目录。
我认为某种通配符会起作用,即
$InputFiles = Get-Item "$(build.artifactstagingdirectory)/Terraform/**/*.tf"
但不,它没有。我在 PowerShell 上不是那么强,而且总是觉得它有点违反直觉,所以对于习惯它的人来说,这可能是一个简单的问题。
解决方案
检查此脚本是否适合您:(您可以根据需要更改文件夹路径和字符串)
$FolderPath = "$(build.artifactstagingdirectory)/Terraform/"
$OldString = "git::https://dev.azure.com"
$NewString = "git::https://$(System.AccessToken)@dev.azure.com"
foreach($currentFile in Get-ChildItem -Path ($FolderPath) -Include *.tf -Recurse)
{
If (Get-Content $currentFile.FullName | Select-String -Pattern $OldString)
{
(Get-Content -path $currentFile.FullName -Raw) -replace $OldString, $NewString | Set-Content $currentFile.FullName
}
}
Write-Information "Done"
推荐阅读
- python-3.x - 查找满足两列值条件的数组行
- html - 使用包构建 HTML 弹出窗口
- python - 使用 pip 安装 Sentinet 时出现 Unicode 解码错误
- c# - 将 SQLite .db3 文件复制到 OneDrive 应用文件夹 xamarin 表单
- javascript - Asset.fromModule 不是函数。(在 'Asset.fromModule(image)' 中,'Asset.fromModule' 未定义)
- android - 只能在所有者的初始化阶段过渡期间创建重启器
- mongodb - 发生重复键错误时,谁能帮我解释一下 mongo 插入过程?
- javascript - Angular 调用 promise 会导致 CSS/JS 发生故障
- c++ - init-declarators 的完整表达式的定义
- javascript - 使用来自本地主机的 AWS Lambda REST URL