VS / TFS 2017 This post was updated to reflect new information
There are 2 PowerShell scripts:
p1.ps1
p2.ps1
p1 is executed as a PowerShell task in a TFS build process. p2 is used twice within p1.
Use 1: p2 is called from p1, meaning p2 is executed inline
Use 2: p2 is launched from p1, meaning p1 does a fire and forget execution of p2, i.e. as a background task.
Within the TFS build process, use 1 works exactly as expected, use 2 does not.
Near the end of p1 are the following 2 different sets of code that have been attempted for the fire and forget execution of p2:
$powershellArguments = "-file C:\conf\p2.ps1" , "refresh" , "C:\agent\_work\4\a\for-deploy\website\"
Start-Process powershell.exe -Argument $powershellArguments
And the 2nd methodology:
Start-Job -FilePath $baseFolder"p2.ps1" -ArgumentList "refresh", $sourceRoot
If I manually execute p1 in a Command Prompt PowerShell Window, the fire and forget p2 execution happens as expected with either sets of code above.
But when I run the build and p1 is executed as a task within the build, p2 does not fire and forget. Again, to be clear, within the TFS build, the use 1 inline process does execute properly.
At the head of the script I embedded some code to write a small text file to confirm the script is at least being started and the code also writes out the arguments received to ensure the argument syntax has been used correctly. The arguments are passed to p2 properly when p1 is executed outside of the build environment. But when p1 is executed as a PowerShell Script within the build, the small text file does not reflect that p2 even started in the fire and forget mode.
It turns out that p1 and p2 are the same script, they just use switches to execute slightly differently. But I copied p1 and named it p2 just to separate the 2 scripts and the result is the same. I can't get p2 to start using the Start-Process cmdlet or the Start-Job cmdlet when p1 is executing within the build.
I think I've been pretty diligent to narrow this down and it seems there is something about launching a separate script from within a script inside the build process.
Why might this be and is there a way around this?
I did wonder if it relates to the basic script policies such as in a batch file that attempts to run a PowerShell script for which we include -executionpolicy remotesigned. Is that possible?
Start-Sleepin the end of p1 (with how much time take p2 run) and check the p2 results.