GitLab CI/CD环境运行PowerShell命令无输出 如何强制输出结果
问题根因
你当前的CI配置写法存在逻辑错误:GitLab CI的script字段下每一行都是独立的系统shell(默认是bash)命令,你单独写pwsh这一行只是启动了一个交互式PowerShell会话,非交互式CI环境下这个会话会一直等待输入不会继续处理后续指令,下一行的$PSVersionTable是交给bash执行的,bash本身不识别PowerShell变量,自然不会有预期输出。
解决方法
1. 修正CI命令写法,把PowerShell指令传入pwsh进程执行
不要单独启动pwsh交互式会话,使用-Command参数直接把要执行的PowerShell命令作为参数传入,单条命令写法如下:
lint: image: mcr.microsoft.com/powershell stage: lint script: - apt-get install -y curl unzip - curl https://azurequickstartsservice.blob.core.windows.net/ttk/latest/arm-template-toolkit.zip --output arm-template-toolkit.zip - unzip arm-template-toolkit.zip - cd arm-ttk # 直接通过-Command参数执行PowerShell命令 - pwsh -Command "$PSVersionTable"
如果需要执行多条PowerShell命令,用分号分隔即可:
pwsh -Command "$PSVersionTable; Get-ChildItem; # 其他PowerShell命令"
2. 强制输出PowerShell执行结果的指令
如果部分命令执行后默认没有输出,可以用以下指令强制把结果打印到标准输出:
- 用
Out-Host强制输出到控制台:$PSVersionTable | Out-Host - 用
Write-Output显式打印返回值:$result = 你的命令 Write-Output $result - 复杂对象输出被截断时,搭配格式化指令输出完整内容:
$PSVersionTable | Format-List * -Force | Out-String -Width 300 | Out-Host
内容的提问来源于stack exchange,提问作者JS noob
相关产品推荐
相关产品推荐

