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!

Arun
  • 300
  • 2
  • 13
dh6984
  • 108
  • 12
  • 1
    May be this helps: https://stackoverflow.com/a/18537263/8534630 – Arun Aug 15 '18 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` – Maximilian Burszley Aug 15 '18 at 14:35
  • @ArunG I looked at that before and that's where I got the idea to replace. I'm not sure how to implement the ampersand, however. – dh6984 Aug 15 '18 at 14:50
  • @TheIncorrigible1 I tried without the quotation marks like so: `$conv = Get-Content -Raw -Path $path | ConvertFrom-Json;` but I get the same error: `Cannot find path 'D:\TFS' because it does not exist.` Also there is a space in the actual path now, there wasn't before. – dh6984 Aug 15 '18 at 14:52
  • 1
    @kiasta Does your `$path` variable have embedded quotes `"`? – Maximilian Burszley Aug 15 '18 at 14:53
  • @TheIncorrigible1 It's an argument being passed in the powershell task in tfs: `-path $(System.DefaultWorkingDirectory)` there are no quotation marks in the path being passed from me, not sure about the variable, however. – dh6984 Aug 15 '18 at 14:55
  • 1
    Is that an environment variable? You should be able to access it: `$Env:DefaultWorkingDirectory` – Maximilian Burszley Aug 15 '18 at 14:57
  • @TheIncorrigible1 That was the solution, so apparently what was happening was that the log was showing the right path but when I Write-Output $path it was for some reason truncated to D:\TFS. I used the $env:SYSTEM_DEFAULTWORKINGDIRECTORY and that worked like a charm. Thanks a lot! – dh6984 Aug 15 '18 at 15:13
  • 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. – user4003407 Aug 15 '18 at 15:14
  • @PetSerAl From the TFS log itself, it was showing the right path for the argument and I never Write-Output the $path variable because I assumed it was passing the right path: `2018-08-15T14:59:16.3135448Z ##[debug]Write-Output $path' 2018-08-15T14:59:16.3165515Z ##[debug]Env:INPUT_SCRIPTARGUMENTS: '-path D:\TFS Agent\_work\r3\a' 2018-08-15T14:59:16.3465484Z ##[command]& 'D:\TFS Agent\_work\_temp\tmp14B7.ps1' -path D:\TFS Agent\_work\r3\a 2018-08-15T14:59:16.3605498Z D:\TFS` But I wrote output and it was truncated. – dh6984 Aug 15 '18 at 15:18
  • `-path D:\TFS Agent\_work\r3\a` Why do you think this will pass `D:\TFS Agent\_work\r3\a` to `-path` parameter, but not `D:\TFS` will be passed to `-path` parameter and `Agent\_work\r3\a` will be passed as positional argument? Space is argument separator after all. Also if you think that `Write-Output $path` truncate displayed output, then why not print something else, like `$path.Length`? – user4003407 Aug 15 '18 at 15:26
  • Your replace is doing nothing - the first quotes `" "` are a space, and you are replacing it with the second `"\` "` which is a PowerShell escape sequence, and only special characters have any meaning with an escape backtick - `"\`n"` for example as a newline. Other characters, like space, are not changed by a backtick and end up the same. So you are replacing a space with a space, which changes nothing. – TessellatingHeckler Aug 15 '18 at 17:50

1 Answers1

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)"
Marina Liu
  • 36,876
  • 5
  • 61
  • 74