且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 TFS 发布执行期间写入实时 powershell 输出

更新时间:2023-11-03 09:21:04

我可以重现这个问题,基于我的测试实时输出不支持 PowerShell on Target Machines代码>任务.

Write-outputwrite-verbose -verbose 只能输出到控制台,但不是实时的,只有在 powershell 脚本完全执行后才会显示输出.

要显示实时输出,您可以使用 Utility:PowerShell 任务而不是 Deploy:PowerShell on Target Machines 任务.

因此,作为一种解决方法,您可以在要运行 powershell 脚本的目标计算机上部署一个代理,然后使用该代理通过 Utility:PowerShell 任务运行 powershell 脚本来触发发布.

更新:

好吧,使用 Utility:PowerShell 任务找到另一个解决方法:

1.为目标计算机设置WinRM,参考

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.

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

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).