【问题标题】:Automatically pass string to powershell user input自动将字符串传递给 powershell 用户输入
【发布时间】:2021-08-26 12:24:07
【问题描述】:

在我的 powershell 脚本中,我调用了以下 azure 函数:

az repos import create --git-source-url https://incommunities@dev.azure.com/my-organisation/Templates/_git/$($Framework) --detect true --project $ProjectAlias --repository $ProjectAlias --requires-authorization

运行时,它会提示用户输入密码/PAT 令牌,例如:

Git Password / PAT:

有没有办法自动将密码/令牌传递给用户输入而无需手动输入?

我试图通过管道传递值,但这似乎不起作用,例如

my-pat-token | az repos import create --git-source-url https://incommunities@dev.azure.com/my-organisation/Templates/_git/$($Framework) --detect true --project $ProjectAlias --repository $ProjectAlias --requires-authorization

这两个 a) 可能吗,如果是的话 b) 我该怎么做?

【问题讨论】:

    标签: powershell azure-devops azure-cli


    【解决方案1】:

    您可以使用两种方法,两者都来自courtesy from this nice blog post which you'll probably want to read,因为它涉及大量 Azure Devops 任务。

    1. 使用环境变量

    这些命令将检查环境变量是否存在,并将使用它而不是提示。

    为此,请将名为 AZURE_DEVOPS_EXT_PAT 的环境变量设置为 PAT 的值。 (有关这些令牌如何工作的更多信息here from the Microsoft Docs

    # set environment variable for current process
    $env:AZURE_DEVOPS_EXT_PAT = 'xxxxxxxxxx'
    

    当您将事情自动化时,只需在运行 Azure 命令之前设置此变量即可。

    1. 将值输入

    我不是这种方法的忠实拥护者,但是您可以“回显” PAT 值并将其通过管道传输到命令中,这可能会起作用。恕我直言,这更脆弱,frunky,我不建议这样做。

    $pat | az devops login
    

    【讨论】:

    • 完美!该文章包含用于 repos 导入的特定配置密钥,因为它不使用基本登录凭据。 AZURE_DEVOPS_EXT_GIT_SOURCE_PASSWORD_OR_PAT
    猜你喜欢
    • 2020-03-15
    • 2014-01-01
    • 1970-01-01
    • 2014-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-21
    • 1970-01-01
    相关资源
    最近更新 更多