在自定义任务问题中具有凭据的开始作业 [英] Start-Job with credential in custom task problems

查看:58
本文介绍了在自定义任务问题中具有凭据的开始作业的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Powershell开发自定义任务,该任务需要使用Start-Job -Cred来切换到其他用户.代理以用户A的身份运行,我需要切换到用户B.以用户A的身份登录运行代理的服务器,然后运行脚本可以正常工作-Start-Job切换凭据并以用户B的身份运行脚本块.

使用与用户A运行代理的相同(本地)代理服务器在云中从VSTS运行完全相同的操作失败,并显示错误消息:

"The background process reported an error with the following message: ."

我已经进行了更多的调试,并且在任何地方都没有其他错误消息.它似乎与Start-Job-Cred参数有关,因为它与脚本块运行没有什么区别,而且如果我删除了-Cred参数,也很好.

  • 用户A在运行代理的服务器上的管理员"组中
  • 代理以用户A身份运行

有什么想法吗?

解决方案

例如,使用Invoke-Command进行尝试(输出当前用户名):

$mypwd = ConvertTo-SecureString -String "[password, could use variable]" -Force -AsPlainText
$Cred = New-Object System.Management.Automation.PSCredential('[user name]',$mypwd)
$scriptToExecute = 
{
$VerbosePreference='Continue'
Write-Output "$env:UserName"
# Write-Verbose "Verbose" 4>&1
}
$b = Invoke-Command -ComputerName localhost -ScriptBlock $scriptToExecute -Credential $Cred
Write-Output "Content of variable B"
Write-Host $b

I am trying to develop a custom task using Powershell which needs to use Start-Job -Cred to switch to another user in places. Agent is running as user A and I need to switch to user B. Logging in to the server running the agent as user A and then running the script works fine - the Start-Job switches credentials and runs a scriptblock as user B.

Running exactly the same thing from VSTS in the cloud using the same (on-prem) agent server running the agent as user A fails with the uninformative error:

"The background process reported an error with the following message: ."

I have done more debugging and there is no other error message anywhere. It seems to be related to the -Cred parameter of Start-Job as it makes no difference what is in the script block run and if I remove the -Cred parameter, it's also fine.

  • User A is in the Adminstrators group on the server running the agent
  • Agent runs as user A

Any ideas?

解决方案

Try it with Invoke-Command, for example (output current user name):

$mypwd = ConvertTo-SecureString -String "[password, could use variable]" -Force -AsPlainText
$Cred = New-Object System.Management.Automation.PSCredential('[user name]',$mypwd)
$scriptToExecute = 
{
$VerbosePreference='Continue'
Write-Output "$env:UserName"
# Write-Verbose "Verbose" 4>&1
}
$b = Invoke-Command -ComputerName localhost -ScriptBlock $scriptToExecute -Credential $Cred
Write-Output "Content of variable B"
Write-Host $b

这篇关于在自定义任务问题中具有凭据的开始作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆