2
votes

I've spent quite a bit of time banging my head on this one. A little StackOverflow help please, good folks!

Scenario: We are trying to run a custom .bat file located on the CI server via the TeamCity Powershell step.

  • When powershell script is run on local box manually, it kicks off .bat file correctly.
  • When powershell script is run through TeamCity, it successfully 'sees' the .bat file (validated by receiving a 'cannot find file' response when I rename the .bat file it is expecting)
  • HOWEVER, we have not seen any indication that the .bat file was actually kicked off.

What we've tried:

  • We've added the 'RedirectStandardOutput' and 'RedirectStandardError' for attempt to diagnose, but although the log file is created, it is returned blank.
  • We've granted filepath permissions and tried two different credentials including the credential of the TC build agent
  • Added "-wait" at one point to see if we needed to 'tell' PS to wait on the .bat file.

Two questions...

  • What is preventing us from running this .bat file?
  • How do we diagnose issues like this? ATM it is a 'blackbox' to us.

TeamCity Powershell settings:

  • Powershell Run Mode: Version 1.0; Bitness x64 (tried x86 as well)
  • Working Directory: Tried as blank, and specific filepath of .bat file (so, 'D:\folder\folder2\')
  • Script: Source Code
  • Script Execution: Execute .ps1 from external file (tried with 'Put script into PowerShell stdin with "-Command -" argument' as well)
  • Add -NoProfile argument (tried both)

Powershell script:

#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."

UPDATE 1: If we remove the '-Credential ($Cred)' portion we are able to kick off the testbat.bat file from TeamCity, as expected. The problem must lie with that "-Credential ($Cred)" argument, somehow. Any thoughts?

UPDATE 2: If we set the '-Credential ($Cred)' portion to the credential of the build agent user we are able to kick off the test.bat file from TeamCity. The problem only occurs when we set the credential to a user other than the one running the build agent. This seems to indicate that credential syntax is fine.

UPDATE 3: Tried running with PowerShell executionpolicy set to 'RemoteSigned' and 'Unrestricted'. Problem persists.

UPDATE 4: Gave the BuildAgent user, and the user of whom we want to run this as, full permissions to powershell via 'Set-PSSessionConfiguration'. Problem persists.

2

2 Answers

0
votes
$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 }'

note:
first i get credential "user" ur user then convert your pass to plain text
then start-process with your credential set

0
votes

If agent is running as service under Local System account, then it's not possible to run PowerShell under specified account. The workarounds are:

  1. Run agent via command line.
  2. Try to run agent service under another account (not Local System) with administrator rights, probably it will help.
  3. Try RunAs plugin. It provides an ability to run builds under the specified user account.