-1

I saw this construct on a question about bash

git -C 'repo-path' pull || git clone https://server/repo-name 'repo-path'

So, if the git pull fails, then do the git clone, which seems quite a clean approach.

What would be an equivalent syntax for this in PowerShell?

YorSubs
  • 3,194
  • 7
  • 37
  • 60

1 Answers1

3

I believe that syntax would work as expected on PowerShell core, since it now supports || and &&.

If you need this to work on pre-PowerShell core systems, you could use $lastExitCode to determine if the previous step worked or not.

git -C 'repo-path' pull
# If the previous line worked, lastExitCode will be zero
if ($lastexitCode) {  
   git clone https://server/repo-name 'repo-path'
}
Start-Automating
  • 8,067
  • 2
  • 28
  • 47
  • For my needs, I need something in PS 5.1, is there a way to get similar syntax on older PowerShell? (it is also very interesting that this works unchanged on PS 7.x though!). – YorSubs Oct 19 '22 at 20:38
  • 1
    Updated the answer to include how you'd do things on 5.1 – Start-Automating Oct 19 '22 at 23:46
  • Sadly, I see it's a bit more verbose in 5.1. I thought there might be an equivalently simple one-liner (sure, I can concatenate all of this onto one line with `;`, but not quite as clean as the bash / PS 7.x way. I'll use your method in 5.1 though, many thanks. – YorSubs Oct 20 '22 at 04:16