0

In our company we use TFS 2017 (update 1) for building and releasing our products. The release part is made up of several steps which include the execution of some Powershell scripts.

This is how I configure the PS step.

enter image description here

What I noticed is that the output of the powershell scripts is not written realtime while it is executing, but all together in the end of the PS task. This is very annoying in case of long running scripts as we are not able to see the live progress of the task, but we have to wait the task to finish to see the results.

I wrote some simple PS scripts to debug this problem but neither using write-host (this does not write nothing at all, even in the end of the task) nor using write-output nor with write-verbose -verbose allows me to write realtime output. This is one example script I tried, without success.

Write-Output "Begin a lengthy process..."
$i = 0
while ($i -le 100)
{
  Start-Sleep 1
  Write-Output "Inner code executed"
  $i += 10
}
Write-Output "Completed."

Did you ever found yourself in this situation?

Regards

gvdm
  • 3,006
  • 5
  • 35
  • 73
  • Try [this](https://blogs.msdn.microsoft.com/premier_developer/2016/04/13/tips-for-writing-powershell-scripts-to-use-in-build-and-release-tasks/), especially see "Displaying Warnings and Errors in the build output". – Christian.K Jun 06 '18 at 10:26
  • HI @Christian.K. I tried this link but it uses `write-host` (which does not output anything). The warning/error logs work, but the part of the progress bar does not work (I'm not able to set the progress and output in realtime). – gvdm Jun 06 '18 at 10:34
  • Hmm... I use `Write-Host` and it works (`Write-Output` won't work). – Christian.K Jun 06 '18 at 10:38
  • Which version of TFS are you using? – gvdm Jun 06 '18 at 10:49
  • TFS 2017 Update 3 (15.117.27024.0) – Christian.K Jun 06 '18 at 12:36

1 Answers1

3

I can reproduce this issue, based on my test realtime output is not supported for the PowerShell on Target Machines task.

Write-output or write-verbose -verbose just can output to console but it's not real-timed, the output only displays once the powershell script completely executed.

To display the real-time output you can use the Utility:PowerShell task instead of the Deploy:PowerShell on Target Machines task.

So, as a workaround you can deploy an agent on the target machine which you want to run the powershell script, then trigger the release using that agent running powershell script with Utility:PowerShell task.


UPDATE:

Well, find another workaround with Utility:PowerShell task:

1.Set up WinRM for target computers, refer to WinRM configuration

2.Copy the target PS script to the target machine (D:\TestShare\PStest.ps1 in below sample)

3.Create a PowerShell script to call the Powershell.exe to run the target powershell script on target machine, see below sample:

Param(
  [string]$computerName = "ICTFS2015.test.com",
)
$Username = "domain\usename"
$Password = ConvertTo-SecureString "Possword" -AsPlainText -Force

$cred = New-Object System.Management.Automation.PSCredential($Username,$password)

Invoke-Command -ComputerName $computerName  -Credential $cred -ScriptBlock {Invoke-Expression -Command:"powershell.exe /c 'D:\TestShare\PStest.ps1'"}

4.Add a Utility:PowerShell task to run above PowerShell script. (You can check in or run Inline Script).

enter image description here

Andy Li-MSFT
  • 28,712
  • 2
  • 33
  • 55
  • Hi @Andy. This sounds good; the problem is that, for what I've seen, the `Utility:PowerShell` component allows to execute only scripts published as linked artifacts. In my situation I need to execute a script which is located on the target machine (so I have to point to a "filesystem" script, not a "repo" script) – gvdm Jun 07 '18 at 08:24
  • @gvdm Yeah, I see. Just as I said above, unfortunately real-time output for `Deploy:PowerShell on Target Machines` task is not supported for now. The workaround I mentioned in above answer should work for you, you can check in the scripts into TFS, the agent will download the PowerShell scripts from TFS repo/linked artifacts to the target machine (agent machine) first, then run it on that machine. – Andy Li-MSFT Jun 14 '18 at 02:26
  • 1
    @gvdm Well, find another workaround with `Utility:PowerShell` task, see the updated answer. – Andy Li-MSFT Jun 14 '18 at 10:08
  • @gvdm Have you resolved the issue by the updated answer? any update? – Andy Li-MSFT Jun 19 '18 at 09:51
  • I've had no time to test it but it is a pending test I have to do :) Anyway thanks for your answer, I will update you as soon as I test it – gvdm Jun 19 '18 at 12:23
  • @gvdm Have you tried that? Any update? BTW, if it works for you, you could accept it as an answer. – Andy Li-MSFT Jul 02 '18 at 04:06
  • Now I found some spare time to test this solution and it works as expected. Thank you Andy – gvdm Jul 19 '18 at 13:38
  • Update: I enhanced the problem with some parameter-passing to the remote script. This is the solution, in case someone will need it: https://stackoverflow.com/a/51944787/2135719 – gvdm Aug 21 '18 at 08:40