需要PowerShell 2.0替换string.replace方法

时间:2016-10-12 09:08:14

标签: powershell

我使用以下PowerShell命令作为后期构建过程的一部分,该过程适用于最新版本的PowerShell:

powershell.exe –command "(Get-Content VSTEST.BAT).replace('__DLL_NAME__', 'SKCltMgrTestu.dll') | Set-Content 'C:\Builds\29\nSKEP\Dev_Continue\bin\VSTEST_SKCltMgrTest.BAT'

但是我意识到我需要一个只装有PowerShell 2.0的产品的等效产品。 在此计算机上执行命令时,出现以下错误:

C:\Builds\29\nSKEP\Dev_Continue\src\Tests\VSTest\SKCltMgrTest>powershell.exe –command "(Get-Content VSTEST.BAT).replace('__DLL_NAME__', 'SKCltMgrTestu.dll') | Set-Content 'C:\Builds\29\nSKEP\Dev_Conti
nue\bin\VSTEST_SKCltMgrTest.BAT'
Method invocation failed because [System.Object[]] doesn't contain a method named 'replace'.
At line:1 char:33
+ (Get-Content VSTEST.BAT).replace <<<< ('__DLL_NAME__', 'SKCltMgrTestu.dll') | Set-Content 'C:\Builds\29\nSKEP\Dev_Continue\bin\VSTEST_SKCltMgrTest.BAT'
    + CategoryInfo          : InvalidOperation: (replace:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

我认为所需的方法仅在PowerShell的更高版本中引入。

是否存在使用与PowerShell 2.0兼容的单行替换文本文件中单个字符串出现的等效方法?

1 个答案:

答案 0 :(得分:3)

尝试使用 -replace代替 replace方法:

powershell.exe –command "(Get-Content VSTEST.BAT) -replace '__DLL_NAME__', 'SKCltMgrTestu.dll' | Set-Content 'C:\Builds\29\nSKEP\Dev_Continue\bin\VSTEST_SKCltMgrTest.BAT'
相关问题