在使用psexec远程启动PowerShell脚本时,Jenkins作业中的输出被截断

我在Jenkins创build了一个工作,使用psexec在远程机器上启动一个小的powershell脚本。 在我的构build步骤中,我正在使用powershell插件并启动以下命令:

& "$env:SCRIPTROOT\test_psexec.ps1" -Username $env:USERNAME -Password $env:PASSWORD -Server $env:REMOTESERVER 

在test_psexec.ps1文件中,我有下面的代码:

 $filePath = "C:\windows\temp\test_script.ps1" $server = "server01" $cmd = "powershell -NoLogo -NonInteractive -ExecutionPolicy Bypass -File $filePath" Write-Host "Launching powershell on remote system using psexec..." Write-Host "Command: & $PSScriptRoot\tools\psexec -accepteula -nobanner -u $username -p **** -h \\$server $cmd" $output = & $PSScriptRoot\tools\psexec -accepteula -nobanner -u $username -p $password -h \\$server $cmd Write-Host "Script Output: " Write-Host $output 

test_script.ps1文件包含以下脚本:

 Write-Host "Verifying if server is pending reboot..." try { $regKey = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired" $regItems = Get-ItemProperty -Path $regKey -ErrorAction SilentlyContinue if($regItems) { Write-Host "Server has a reboot pending. Rebooting server..." } else { Write-Host "Server does not have a pending reboot." } } catch { Write-Host "Failed to verify pending reboot or unable to restart server." Write-Host $_.Message } 

作业成功执行,但由于某种原因,我在Jenkins的psexec中显示的输出只返回一行。 我用一堆文本在远程计算机上执行了另一个testing脚本,它似乎将输出截断为256个字符。 jenkins的工作输出看起来类似于下面的内容:

 Started by user Test Account Building in workspace D:\Jenkins\workspace\test_job [test_job] $ powershell.exe -NonInteractive -ExecutionPolicy ByPass "& 'C:\windows\TEMP\jenkins3570507542870234918.ps1'" Launching powershell on remote system using psexec... Command: D:\test_job\tools\psexec.exe -accepteula -nobanner -u testdomain\test_acct -p **** -h \\server01 "powershell" -NoLogo -NonInteractive -ExecutionPolicy Bypass -File C:\windows\temp\test_script.ps1 Connecting to server01... Starting PSEXESVC service on server01... Connecting with PsExec service on server01... Starting powershell on server01... powershell exited on server01 with error code 0. Script Output: Verifying if server is pending reboot... Execution successful. Finished: SUCCESS 

如果我在PowerShell的远程机器上login并启动脚本,则会得到相应的输出。 另外,我不直接在Jenkins中启动psexec,因为在这个与test_psexec.ps1脚本无关的部分还有其他逻辑。

有谁知道,如果我正在打某种缓冲区限制或知道我可以configuration,以避免这个问题的设置?

这不是为你工作的原因是因为你的第二个脚本是使用Write-HostWrite-Host的输出不能被redirect; 所以当您远程执行该脚本时,输出正在丢失。

在你的情况下,最好的select是Write-Out ,将对象写入输出stream。 在这种情况下,这应该可以正常工作,但要注意,在需要将其他结果返回到输出stream的函数中使用Write-Out时,会出现意外的结果。 (在Powershell中,从函数返回值相当于Write-Out 。)

另一种select是Write-Verbose ,它将结果写入详细stream。 要查看这些结果,您需要将-Verbose标志添加到您的脚本调用,或者您可以设置$VerbosePreference = $true为您的会话进行设置。

这里有一篇很好的文章,谈论Write-Host的缺陷: http : //www.jsnover.com/blog/2013/12/07/write-host-considered-harmful/