替换文件路径的一部分并在Sonarqube中获取结果

时间:2015-10-19 06:29:43

标签: powershell jenkins powershell-v2.0 powershell-v3.0 sonarqube5.1

我正在尝试在jenkins中编写一个Powershell脚本来读取文件并替换该文件中的部分文件路径。当我对路径进行硬编码时,下面的脚本就可以解决了:

$file = "$env:WORKSPACE\UIArtifacts\unit-tests-lcov.info" 
$text = (Get-Content -Path $file -ReadCount 0) -join "`n" 
$text -replace "c:\\Sysapps\\Hudson\\.jenkins\\jobs\\Encirca - UI CI\\workspace\\trunk\\app", $env:WORKSPACE | Set-Content -Path $file 

但是,当我动态设置文件路径时,我没有得到结果。下面是我正在尝试运行的脚本:

$file = "$env:WORKSPACE\UIArtifacts\unit-tests-lcov.info" 
$text = (Get-Content -Path $file -ReadCount 0) -join "`n" 
$text -replace ".*\\workspace\\trunk\\app", $env:WORKSPACE | Set-Content -Path $file 

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

$file = "$env:WORKSPACE\UIArtifacts\unit-tests-lcov.info" 
$text = (Get-Content -Path $file -ReadCount 0) -join "`n"
$replace = ".*\\trunk\\app"
$text -replace $replace $env:WORKSPACE | Set-Content -Path $file

PS C:\Windows\system32> $file = "C:\temp\test.txt"

PS C:\Windows\system32> $text = (Get-Content -Path $file -ReadCount 0) -join "`n" 

PS C:\Windows\system32> $text
c:\Sysapps\Hudson\.jenkins\jobs\Encirca - UI CI\workspace\trunk\app\TestFolder1\TestFolder2

PS C:\Windows\system32> $string = ".*\\trunk\\app"

PS C:\Windows\system32> $text -replace $string, "Set\Custom\Path\Here" | Set-Content -Path $file

PS C:\Windows\system32> Get-Content $file
Set\Custom\Path\Here\TestFolder1\TestFolder2