2

我花了相当多的时间来敲打这个。好人,请提供一点 StackOverflow 帮助!

场景:我们正在尝试通过 TeamCity Powershell 步骤运行位于 CI 服务器上的自定义 .bat 文件。

  • 当 powershell 脚本在本地机器上手动运行时,它会正确启动 .bat 文件。
  • 当 powershell 脚本通过 TeamCity 运行时,它成功地“看到”了 .bat 文件(通过在我重命名预期的 .bat 文件时接收到“找不到文件”响应来验证)
  • 但是,我们没有看到任何迹象表明 .bat 文件实际上已启动。

我们尝试过的:

  • 我们添加了“RedirectStandardOutput”和“RedirectStandardError”来尝试诊断,但是虽然创建了日志文件,但它返回为空白。
  • 我们已授予文件路径权限并尝试了两种不同的凭据,包括 TC 构建代理的凭据
  • 在某一时刻添加了“-wait”以查看我们是否需要“告诉”PS 等待 .bat 文件。

两个问题...

  • 是什么阻止我们运行这个 .bat 文件?
  • 我们如何诊断这样的问题?ATM 对我们来说是一个“黑匣子”。

TeamCity Powershell 设置:

  • Powershell 运行模式:1.0 版;位数 x64(也尝试过 x86)
  • 工作目录:尝试作为 .bat 文件的空白和特定文件路径(因此,'D:\folder\folder2\')
  • 脚本:源代码
  • 脚本执行:从外部文件执行 .ps1(也尝试使用“使用“-Command -”参数将脚本放入 PowerShell 标准输入”)
  • 添加 -NoProfile 参数(都尝试过)

Powershell脚本:

#Predefine necessary information
$Username = "DOMAIN\username"
$Password = "password"
$ComputerName = "CI Build Server Name"

#Create credential object
$SecurePassWord = ConvertTo-SecureString -AsPlainText $Password -Force
$Cred = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $Username, $SecurePassWord

#Start batch file
Start-Process "testbat.bat" -WorkingDirectory D:\file\path\ -Credential ($Cred)-NoNewWindow -RedirectStandardError stderr.txt -RedirectStandardOutput stdout.txt

Write-Host "Executed powershell."

更新 1:如果我们删除“-Credential ($Cred)”部分,我们可以按预期从 TeamCity 启动 testbat.bat 文件。不知何故,问题一定出在“-Credential ($Cred)”参数上。有什么想法吗?

更新 2:如果我们将“-Credential ($Cred)”部分设置为构建代理用户的凭据,我们就可以从 TeamCity 启动 test.bat 文件。仅当我们将凭据设置为运行构建代理的用户以外的用户时,才会出现此问题。这似乎表明凭证语法很好。

更新 3:尝试在 PowerShell executionpolicy 设置为“RemoteSigned”和“Unrestricted”的情况下运行。问题仍然存在。

更新 4:通过“Set-PSSessionConfiguration”为 BuildAgent 用户和我们想要运行它的用户授予对 powershell 的完全权限。问题仍然存在。

4

2 回答 2

0
$credential = New-Object System.Management.Automation.PsCredential(".\user", (ConvertTo-SecureString "pass" -AsPlainText -Force))
     Start-Process powershell -Credential $credential -ArgumentList '-noprofile -command &{Start-Process  D:\file\path\test.bat -NoNewWindow -RedirectStandardError stderr.txt -RedirectStandardOutput stdout.txt }'

注意:
首先我得到凭证“用户”你的用户,然后将您的通行证转换为纯文本
,然后使用您的凭证集开始处理

于 2015-03-25T23:47:23.607 回答
0

如果代理在本地系统帐户下作为服务运行,则无法在指定帐户下运行 PowerShell。解决方法是:

  1. 通过命令行运行代理。
  2. 尝试在具有管理员权限的另一个帐户(不是本地系统)下运行代理服务,可能会有所帮助。
  3. 试试RunAs 插件。它提供了在指定用户帐户下运行构建的能力。
于 2016-03-22T11:51:54.143 回答