2
votes

The boss changed TFS servers and added a space in the path: D:\TFS Agent\xxx and it's causing my powershell script to fail upon release.

We have build/release automatic integration with TFS and I have an inline powershell task to read a file and convert it to JSON then execute some SQL. I had this working until this morning.

The problem is that the agent path is a system variable in TFS: $(System.DefaultWorkingDirectory) and I'm not sure how to handle the space in the path.

I've tried this:

# Begin inline script
Param(
    [string]$path,
    [string]$db,
    [string]$schema,
    [string]$envName
)
# Create module
$formattedPath = $path -replace " ", "` ";
$conv = Get-Content -Raw -Path "$formattedPath" | ConvertFrom-Json;

But all I get is D:\TFS. The path looks like this before the replace:

D:\TFS Agent\_work\r3\a\xxx

I can't for the life of me figure out how to replace the space with a tick mark or how to ignore spaces. I'm very new to powershell so this may just be some simple thing, but my google-fu is not strong today. Any help would be appreciated. Thanks!

1
why is a space in the path breaking things..? Are you trying to replace the space because there isn't a space in the actual path? You should be able to run this as it is: Get-Content -Path $path -Raw - Maximilian Burszley
@kiasta Does your $path variable have embedded quotes "? - Maximilian Burszley
Is that an environment variable? You should be able to access it: $Env:DefaultWorkingDirectory - Maximilian Burszley
The path looks like this before the replace: D:\TFS Agent\_work\r3\a\xxx What evidence of that do you have? Your -replace operation is effectively no-op. It replace space with space. If you only get D:\TFS in the result, then that mean you have only that from start. - user4003407

1 Answers

4
votes

It seems you are pass $(System.DefaultWorkingDirectory) with the variable path as below:

-path $(System.DefaultWorkingDirectory)

While, if $(System.DefaultWorkingDirectory) contains spaces (such as D:\TFS Agent\_work\r3\a), it will show divided the values into different lines by spaces (such as D:\TFS Agent\_work\r3\a will show in two lines with value D:\TFS and Agent\_work\r3\a). So the variable $path with the value in the first line (like D:\TFS).

To solve the problem, you just need to add double quotes for $(System.DefaultWorkingDirectory). So just change the argument in PowerShell task as:

-path "$(System.DefaultWorkingDirectory)"