【问题标题】:Start-Process hangs when used with -Credential与 -Credential 一起使用时,Start-Process 挂起
【发布时间】:2019-02-28 18:51:22
【问题描述】:

在我的持续交付管道期间,我想运行 Powershell 脚本,该脚本启动用于更新数据库的 .NET Core 应用程序。问题是用户使用的 TFS 代理没有权限连接到 MSSQL 服务器。这就是为什么我想以不同的用户身份运行这个过程。我现在正在做的是

$username = 'DOMAIN\username'
$password = 'password'

$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential -ArgumentList $username, $securePassword

Start-Process -FilePath 'dotnet' -ArgumentList 'MyApp.dll' -NoNewWindow -Wait -Credential $credential

运行此脚本会导致 Powershell 窗口冻结。我不能Ctrl+C 也不能Ctrl+Z。相同的命令,但没有 -Credential 部分工作正常。在我的本地机器上,即使使用-Credential,一切正常,所以这一定是某种配置问题。

我在寻找答案时偶然发现了this SO question,但this comment 对我没有多大帮助。

我尝试将执行策略设置为Bypass 和Unrestricted。我还尝试在本地组策略编辑器 > 计算机配置 > Windows 组件 > Windows PowerShell 中配置 Turn on Script Execution 以 允许所有脚本,但也没有任何运气。

【问题讨论】:

    标签: powershell start-process


    【解决方案1】:

    试试这个:

    Start-Process -FilePath 'dotnet' -ArgumentList 'MyApp.dll' -NoNewWindow -Credential $credential -PassThru| Wait-Process -Timeout 5 -ErrorAction Stop 
    

    而不是直接给-Wait。

    PS:你也可以放一些 try catch 来帮助你。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-20
      • 2013-03-14
      • 2017-01-29
      • 2019-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-06
      相关资源
      最近更新 更多