gpt4 book ai didi

powershell - 如何使用用户凭据在 Powershell 中运行 Start-Process?

转载 作者:行者123 更新时间:2023-12-02 22:14:46 29 4
gpt4 key购买 nike

我有一个运行脚本的 Windows 服务 (Jenkins),该脚本需要以特定用户身份运行命令。

我试图这样做,但它不起作用:

$secpasswd = ConvertTo-SecureString "myPassword" -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential("DOMAIN\myUsername", $secpasswd)

$Arguments = @()
$Arguments += "-Command"
$Arguments += "pwd"
$Arguments += ">"
$Arguments += "output.txt"
Start-Process powershell.exe -ArgumentList $Arguments -Credential $mycreds -NoNewWindow -WorkingDirectory $workingDir
Start-Sleep 2
Get-Content "$workingDir\output.txt"

我得到这个输出:
Start-Process : This command cannot be executed due to the error: Access is denied.
At C:\Windows\TEMP\hudson2382859596554223918.ps1:32 char:14
+ Start-Process <<<< powershell.exe -ArgumentList $Arguments -Credential $mycreds -NoNewWindow -WorkingDirectory $workingDir
+ CategoryInfo : InvalidOperation: (:) [Start-Process], InvalidOperationException
+ FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand

现在,如果我删除 -Credential $mycreds它工作正常。之所以有那个 Start-Sleep最后是我删除了 -Wait阅读后 this question on SO .

我在这里错过了什么吗?

最佳答案

$username = "username"

$password = "password"


$credentials = New-Object System.Management.Automation.PSCredential -ArgumentList @($username,(ConvertTo-SecureString -String $password -AsPlainText -Force))


Start-Process dnscrypt-proxy.exe -WorkingDirectory path_here -Credential ($credentials)

--来自powershell论坛;几天前,我搜索了同样的解决方案,这奏效了。希望对你有帮助。

来源: http://powershell.com/cs/forums/t/9502.aspx

关于powershell - 如何使用用户凭据在 Powershell 中运行 Start-Process?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21758651/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com