powershell - Powershell查找和替换失败,因为文件被另一个进程使用
问题描述
在 for 循环中,我在文件中搜索各种模式并用预定义的标记替换它们:
((Get-Content -path $file -Raw) -replace $pattern, $tokenValue) | Set-Content -Path $file
这有时有效,但有时它会因错误而失败:
The process cannot access the file 'my-file' because it is being used by another process.
我看到解决方法是使用管道 | (在文件中查找和替换失败)但我已经这样做了。这个问题的另一个原因是什么?
这是完整的片段:
foreach ($variable in $variables) {
$token = $variable.Value.replace("__", "")
if ([bool]($group.variables.PSobject.Properties.name -eq $token)){
Write-Host -ForegroundColor DarkGreen "Match found ! token $token"
$tokenValue = $group.variables | Select-Object -ExpandProperty $token | Select-Object -ExpandProperty Value
Write-Host -ForegroundColor DarkGreen "replacing " $variable.Value " with $tokenValue"
((Get-Content -path $file -Raw) -replace $pattern, $tokenValue) | Set-Content -Path $file
}
}
解决方案
推荐阅读
- d3.js - 如何使 svg 符号投影
- kubernetes - Google FileStore 是否适用于 GKE 上的 Windows 容器?
- amazon-redshift - 首次运行查询的 Redshift 编译时间
- sql - 使用新列将主键从简单更改为复合
- sql-server - 通过 .NET 应用程序中的 SqlCommand 的临时表
- c# - 如何删除日期字段中的时间格式(Json 数据)
- python - 熊猫数据框多列条形图
- javascript - 当 API 调用后的 setState 时反应不重新渲染
- python - vscode:如何自动缩进jupyter notebook ipynb文件?
- .net-core - 如何让 Rider 再次显示 API 文档?