我有一个 Azure DevOps 任务,需要在 VM 上执行 Powershell 脚本。内联脚本生成凭据,用作正在启动的 Powershell 进程的参数。
- task: AzureCLI@2
inputs:
azureSubscription: 'MySubscription'
scriptType: 'ps'
scriptLocation: 'inlineScript'
inlineScript: |
az vm run-command invoke --command-id RunPowerShellScript --name $(MyVmName) -g My-RG --scripts `
"`$pw = ConvertTo-SecureString `"$(PasswordSecret)`" -AsPlainText -Force" `
"`$cred = New-Object System.Management.Automation.PSCredential 'Vm1\localadmin',`$pw" `
"Start-Process PowerShell -Cred `$cred -ArgumentList '-noexit','-File','C:\Users\localadmin\Documents\deploy.ps1'"
但是,似乎启动进程本身被拒绝访问:
2021-04-21T22:15:29.6078202Z "message": "Start-Process : This command cannot be run due to the error: Access is denied.\nAt C:\\Packages\\Plugins\\Microsoft.CPlat.Core.RunCommandWindows\\1.1.8\\Downloads\\script11.ps1:3 char:1\n+ Start-Process PowerShell -Cred $cred -ArgumentList '-noexit','-File', ...\n+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n + CategoryInfo : InvalidOperation: (:) [Start-Process], InvalidOperationException\n + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand"
可以在 vm 上找到下面的内联脚本,并使用用于生成 $cred 的相同凭据成功执行。但是,我猜因为 DevOps 任务是在一个不同的、权限较低的帐户下运行的,所以它无权访问 Start-Process cmdlet。
有没有办法解决这个问题?
(注意:由于一些超出本文范围的原因,我无法在目标机器上使用 Powershell 任务,但实际上我已经尝试过了。)