从cmd调用时,字符串替换不起作用

时间:2016-02-29 16:01:04

标签: powershell cmd

我有以下PowerShell-Command,直接从powershell调用时效果很好:

Get-ChildItem -r -include 'AssemblyInfo.cs' `
| foreach-object { $a = $_.fullname; ( get-content $a ) `
| foreach-object { $_ -replace '((\[assembly:\sAssemblyVersion\(")(\d\.\d\.\d\.\d)("\)\]))', '$1[assembly:AssemblyInformationalVersion("$3-alpha")]' } ` 
| Out-File $_.fullname }

但是,当从cmd调用时(我们的Buildservers-Process在cmd中运行)它不会引发错误但它也不会按预期替换字符串(它可以从任何cmd窗口重现):

PowerShell -Command "(Get-ChildItem -r -include  'AssemblyInfo.cs') | foreach-object { $a = $_.fullname; ( get-content $a ) | foreach-object { $_ -replace '((\[assembly:\sAssemblyVersion\(")(\d\.\d\.\d\.\d)("\)\]))', '$1[assembly:AssemblyInformationalVersion("$3-alpha")]' } | Out-File $_.fullname }"

我尝试使用and&`来转义引号和命令,但它没有帮助。

我错过了什么?

1 个答案:

答案 0 :(得分:2)

命令字符串中有双引号:

... -replace '((\[assembly:\sAssemblyVersion\(")(\d\.\d\.\d\.\d)("\)\]))', ...
                                              ^                  ^

必须为CMD转义这些内容:

... -replace '((\[assembly:\sAssemblyVersion\(\")(\d\.\d\.\d\.\d)(\"\)\]))', ...
                                              ^^                  ^^