在 TFS 发布执行期间写入实时 powershell 输出 [英] Write realtime powershell output during TFS release execution

查看:55
本文介绍了在 TFS 发布执行期间写入实时 powershell 输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们公司,我们使用 TFS 2017(更新 1)来构建和发布我们的产品.发布部分由几个步骤组成,其中包括执行一些 Powershell 脚本.

这就是我配置 PS 步骤的方式.

我注意到,powershell 脚本的输出不是在执行时实时写入,而是在 PS 任务结束时一起写入.这对于长时间运行的脚本来说非常烦人,因为我们无法看到任务的实时进度,但我们必须等待任务完成才能看到结果.

我写了一些简单的 PS 脚本来调试这个问题,但既不使用 write-host(这根本不写任何东西,即使在任务结束时)也不使用 write-outputwrite-verbose -verbose 都允许我编写实时输出.这是我尝试过的一个示例脚本,但没有成功.

Write-Output 开始一个漫长的过程……"$i = 0而 ($i -le 100){开始睡眠 1写输出内部代码执行"$i += 10}写输出已完成".

你有没有遇到过这种情况?

问候

解决方案

我可以重现这个问题,基于我的测试实时输出不支持 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).

这篇关于在 TFS 发布执行期间写入实时 powershell 输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆