2

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!

12
  • 1
    May be this helps: stackoverflow.com/a/18537263/8534630 Commented Aug 15, 2018 at 14:32
  • 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 Commented Aug 15, 2018 at 14:35
  • 1
    @kiasta Does your $path variable have embedded quotes "? Commented Aug 15, 2018 at 14:53
  • 1
    Is that an environment variable? You should be able to access it: $Env:DefaultWorkingDirectory Commented Aug 15, 2018 at 14:57
  • 1
    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. Commented Aug 15, 2018 at 15:14

1 Answer 1

4

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)"
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.