【问题标题】:PowerShell shell hangs when saving results to variable (Azure Pipelines List)将结果保存到变量时 PowerShell shell 挂起(Azure 管道列表)
【发布时间】:2020-02-19 18:09:27
【问题描述】:

我正在尝试将 Azure CLI (az pipelines list) 命令的结果保存到变量中,但 shell/脚本挂起。

如果我自己运行命令,它会起作用:

PS C:\> az pipelines list --project PROJECT_NAME --name PIPELINE_NAME --output json
This command is in preview. It may be changed/removed in a future release.
[
  {
    "authoredBy": {
      # ...
    },
    "createdDate": "2019-12-07T00:08:03.620000+00:00",
    "draftOf": null,
    "drafts": [],
    "id": 541,
    "latestBuild": null,
    "latestCompletedBuild": null,
    "metrics": null,
    "name": "PIPELINE_NAME",
    "path": "\\",
    "project": {
      "abbreviation": null,
      "defaultTeamImageUrl": null,
      "description": null,
      "id": "99a1b81a-ca3b-418a-86cf-0965eaba6dab",
      "lastUpdateTime": "2019-12-13T20:54:20.28Z",
      "name": "PROJECT_NAME",
      "revision": 462,
      "state": "wellFormed",
      "url": "https://dev.azure.com/ORGANIZATION_NAME/_apis/projects/99a1b81a-ca3b-418a-86cf-0965eaba6dab",
      "visibility": "private"
    },
    "quality": "definition",
    "queue": {
      "id": 501,
      "name": "Azure Pipelines",
      "pool": {
        "id": 65,
        "isHosted": true,
        "name": "Azure Pipelines"
      },
      "url": "https://dev.azure.com/ORGANIZATION_NAME/_apis/build/Queues/501"
    },
    "queueStatus": "enabled",
    "revision": 30,
    "type": "build",
    "uri": "vstfs:///Build/Definition/541",
    "url": "https://dev.azure.com/ORGANIZATION_NAME/99a1b81a-ca3b-418a-86cf-0965eaba6dab/_apis/build/Definitions/541?revision=30"
  }
]
PS C:\>

但是,如果我尝试将结果分配给变量,则 shell/脚本会挂起:

PS C:\> $pipelines = az pipelines list --project PROJECT_NAME --name PIPELINE_NAME --output json
This command is in preview. It may be changed/removed in a future release.

然后光标跳到字符 61 的位置并永远停留在那里。

这种行为的原因可能是什么?我觉得预览警告造成了一些麻烦,但我不知道如何抑制它。

非常感谢任何见解。

谢谢!

【问题讨论】:

  • 无法重现此问题。上面的命令对我有用。
  • 我也遇到了 az pipelines show 而不是列表的问题。我还可以重现光标跳来跳去。我的 azure-devops 扩展的版本是 0.17.0。
  • 在浪费了一个上午之后......如果标题被重命名以提及更多相关的关键字,例如:输出文本不可见

标签: json powershell variable-assignment azure-cli


【解决方案1】:

好的,这听起来很奇怪,但这只是一个渲染问题 - 应用程序根本没有挂起,它只是阻止控制台正确输出,包括命令完成后的提示。

在脚本的顶部添加以下内容:

$PSBackgroundColor = $Host.UI.RawUI.BackgroundColor
$PSForegroundColor = $Host.UI.RawUI.ForegroundColor

function Reset-Console {
    $Host.UI.RawUI.BackgroundColor = $PSBackgroundColor
    $Host.UI.RawUI.ForegroundColor = $PSForegroundColor
}

然后运行命令后:

Reset-Console

这为我解决了这个问题。

【讨论】:

  • 一个更短的类似的东西:[Console]::ResetColor()
【解决方案2】:

如上一个答案所述,这是由于 azure cli 的颜色输出(在本例中为警告文本)弄乱了终端。

由于 PR [Core] Knack adoption #12604,可以通过将环境变量 AZURE_CORE_NO_COLOR 设置为 True 来禁用 Azure cli 的输出着色(或者通过在 ~/.azure/config 中设置 [core] no_color=True 选项

我在 Azure CLI 2.14.2 版中成功使用了 i。

来自公关[Core] Knack adoption #12604的描述

[Core] PREVIEW:允许通过设置 AZURE_CORE_NO_COLOR 禁用颜色 环境变量为 True 或 [core] no_color=True config (#12601)

【讨论】:

    猜你喜欢
    • 2013-05-05
    • 1970-01-01
    • 2014-04-16
    • 1970-01-01
    • 2021-12-09
    • 2014-05-25
    • 1970-01-01
    • 2023-02-09
    • 1970-01-01
    相关资源
    最近更新 更多