简体   繁体   English

Azure 管道:使用凭证启动进程不起作用

[英]Azure Pipelines: Start-Process with credentials not working

I am trying to start an application from Azure Pipelines YAML using Powershell.我正在尝试使用 Powershell 从 Azure 管道 YAML 启动应用程序。

I want the app to start with a non-administrator account, while the Azure Pipelines deployment agent uses an administrator account.我希望应用程序以非管理员帐户启动,而 Azure Pipelines 部署代理使用管理员帐户。

To do this, I am using a Powershell task running in a deployment job that calls Start-Process with -Credentials:为此,我使用 Powershell 任务在部署作业中运行,该作业使用 -Credentials 调用 Start-Process:

- task: PowerShell@2
  displayName: 'Start application'
  inputs:
    targetType: 'inline'
    script: |   
        $user = "myOrdinaryUser"
        $PWord = ConvertTo-SecureString -String "myPassword" -AsPlainText -Force

        $credentials = New-Object -TypeName System.Management.Automation.PSCredential ($user, $PWord)

        $process = Start-Process -FilePath "C:\Windows\notepad.exe" -PassThru -Credential $credentials -WorkingDirectory 'C:\Windows' -RedirectStandardOutput myOutputLogFile -RedirectStandardError myErrorLogFile
        

         Write-Host "Process ExitCode: " $process.ExitCode
         Write-Host "Process id: " $process.Id
         Write-Host "Process name: " $process.Name
         Write-Host "HasExited: " $process.HasExited
         Write-Host "StartTime: " $process.StartTime
         Write-Host "ExitTime: " $process.ExitTime

But it does not work.但它不起作用。

Output: Output:

Process ExitCode:进程退出代码:

Process id: 11408进程号:11408

Process name:进程名称:

HasExited: True已退出:真

StartTime:开始时间:

ExitTime:退出时间:

Both the output log files get created, but contain nothing. output 日志文件都已创建,但不包含任何内容。

In Event Viewer, under Windows Logs -> System, there is the message在事件查看器中,在 Windows 日志 -> 系统下,有消息

Source: Application Popup资料来源:应用程序弹出窗口

notepad.exe - Application Error notepad.exe - 应用程序错误

The application was unable to start correctly (0xc0000142).应用程序无法正确启动 (0xc0000142)。 Click OK to close the application.单击“确定”关闭应用程序。

If I do the same without credentials, it works.如果我在没有凭据的情况下做同样的事情,它就可以工作。 However, the app closes when the job finishes.但是,应用程序会在作业完成时关闭。 Apparently, this is by design: https://developercommunity.visualstudio.com/t/starting-a-process-from-azure-deployment-agent-tas/1038946显然,这是设计使然:https://developercommunity.visualstudio.com/t/starting-a-process-from-azure-deployment-agent-tas/1038946

$process = Start-Process -FilePath "C:\Windows\notepad.exe" -PassThru

Output: Output:

Process ExitCode:进程退出代码:

Process id: 10024进程号:10024

Process name: notepad进程名称:记事本

HasExited: False已退出:假

StartTime: 19-04-2022 16:23:52开始时间:19-04-2022 16:23:52

ExitTime:退出时间:

Also, If I run the PS script in a Powershell prompt it works with credentials.此外,如果我在 Powershell 提示符下运行 PS 脚本,它会使用凭据。

How can I get more information about why the process does not start and how do I fix it?我如何才能获得有关进程未启动的原因以及如何解决的更多信息?

I solved this with the following workarounds:我通过以下解决方法解决了这个问题:

1. Created a Windows Task with an event log trigger that runs the app under the correct user account. 1. 创建了一个带有事件日志触发器的 Windows 任务,该任务在正确的用户帐户下运行应用程序。

Then, replaced Start-Process with this Powershell command:然后,用这个 Powershell 命令替换 Start-Process:

Write-EventLog -LogName "Application" -Source "MySource" -EventID 999 -Message "Start application"

Installed a build agent instead of a deployment/resource agent.安装了构建代理而不是部署/资源代理。 Configured to run in Interactive mode.配置为以交互模式运行。

Start-Process works using -Credential in this mode. Start-Process 在此模式下使用 -Credential 工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM