Powershell script -Replace doesn't work
I am using Powershell step to change the parameter and file location, i just do very simple scripting to remove "\" and change it to "_" but PowerShell in TeamCity doesn't replace this characters
I just want this output "start_end_egypt"
I was trying many ways with no success
this :
$str = 'start\end\egypt'
$str = $str -replace '\\' , '_'
echo $str
and this :
$str = 'start\end\egypt'
$str = [regex]::Replace($str,"\\","_")
echo $str
and this :
$str = '\start\end\egypt'
$str = $str.Replace("\" , "_")
echo $str
Please sign in to leave a comment.
Hi,
could you provide us with some extra details? Powershell version and OS, and whether you are calling the power shell via a script file or writing the script directly in TeamCity's UI?
Also, in the first and second cases, it looks like you are only escaping the "\" on one of the strings, not the others. Maybe you could check that?
If you could also add teamcity's version, it would be helpful
Thank you for support, I have found the solution, the problem was in TeamCity variable %TeamCity.build.branch% it gives me this path: start/end/egypt and my replace method trying to replace "\" not "/", so it change nothing and when Team City PowerShell step looking for the folder on the server with a name D:\packages\artifact.windows.3.2.5.%teamcity.build.branch% it was reflect D:\packages\artifact.windows.3.2.5.start\end\egypt and there is no folder with this name , because the folder name is D:\packages\artifact.windows.3.2.5.start_end_egypt
I added :
$str ="%teamcity.build.branch%"
if ($str -eq "start/end/egypt") {
$str ="start_end_egypt"
}
$path = 'D:\packages\artifact.windows.3.2.5.'+$str
and the result was: D:\packages\artifact.windows.3.2.5.start_end_egypt
problem solved and thank you for your support