如何通过Powershell搜索文件夹中的.token文件并替换字符前后的“__”?

How to search .token files in a folder and replace "__" in front and end of character via Powershell?

我想搜索具有以下模式字符串的 .token 文件的文件 __[characters]__ 并通过 PowerShell 执行以下操作:

例如:

__STAGE__
to
#{STAGE}

我正在将 RM 令牌文件迁移到 Octopus Deploy,需要通过脚本进行清理步骤。

以最基本的方式并假设将令牌文件装入内存没有问题:

$TokenFiles = Get-ChildItem *.token

foreach ($file in $TokenFiles) {
    $NewContent = Get-Content $file.FullName -Raw
    $NewContent = $NewContent -replace '__(.*?)__', '#{}'
    Set-Content $file.FullName -Value $NewContent
}