gvdm
gvdm

Reputation: 3166

Write realtime powershell output during TFS release execution

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

Upvotes: 0

Views: 2325

Answers (1)

Andy Li-MSFT
Andy Li-MSFT

Reputation: 30412

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

Upvotes: 3

Related Questions