PowerShell runner cannot create new instance with Credentials specified

I'm trying to execute a PowerShell script in TeamCity with elevated credentials, but it doesn't seem to work.

This is a simple example:

$domain = $args[0]
$runAsUsername = $args[1]
$runAsPassword = $args[2]

$powershell = "%powershell_x86_Path%\Powershell.exe"

$cred = New-Object Management.Automation.PsCredential(("{0}\{1}" -f $domain, $runAsUsername), (ConvertTo-SecureString $runAsPassword -AsPlainText -Force))

Start-Process -FilePath $powershell -Credential $cred -ArgumentList "whoami | Out-File with.txt"
Start-Process -FilePath $powershell -ArgumentList "whoami | Out-File without.txt"

When running a PowerShell runner with 3 arguments specified I get 'without.txt' created in my TeamCity working directory, but no 'with.txt'.
When running in a PowerShell runner interactively on the Build Agent box with the 3 arguments specified, I get both files - it would seem this is a problem with the context of the PowerShell runner.

TeamCity doesn't seem to provide any feedback in these situations, it just doesn't work. I'm running TeamCity Enterprise 8.0.1.

Does anybody have any ideas?

Comment actions Permalink

I have the exact same problem. Did you manage to fix it?

Comment actions Permalink

I guess you've PowerShell >=3 version
try to use foreeach-parrallel or use commands as job (start-job) .


Please sign in to leave a comment.