Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a powershell command which runs in TeamCity. When I try to run a batch file remotely using psexec from this .ps file, I see once the remote execution started, nothing happens. I tried several ways discussed in multiple forums but of no use.

Main.ps:

Invoke-Command -ScriptBlock {C:\PSInstall.bat}

PSInstall.bat:

C:\Tools\psexec.exe -i -d "\\server2" -u "domain\admin" -p "abcd" -f -w cmd "C:\Install.bat"

My Build log:

[11:32:02]C:\BuildAgent\work\603cfc01a3fe22bb\Tools>C:\Tools\psexec.exe -i -d "\\server2" -u "domain\admin" -p "abcd" -f -w cmd "C:\Install.bat" 
[11:32:02]
[11:32:02]PsExec v1.98 - Execute processes remotely
[11:32:02]Copyright (C) 2001-2010 Mark Russinovich
[11:32:02]PsExec executes a program on a remote system, where remotely executed console
[11:32:02]Sysinternals - www.sysinternals.com
[11:32:02]applications execute interactively.

I am stuck at this point with no clue what's happening, any help is highly appreciated. I have set EULA already on the remote machine.

share|improve this question
    
If you run the commands directly from powershell (forgetting TeamCity for now), what happens? Does it work ok? – Chris Feb 11 '13 at 23:47
    
I have the same problem, and it works directly from Powershell. Must be some permissions issue? – stuartdotnet Jan 19 '15 at 23:37

The PsExec command/arguments you have given in your example are malformed, instead try:

C:\Tools\PsExec.exe \\server2 -u "domain\admin" -p "abcd" "C:\Install.bat" -i -d -f -w

Also, putting it all together with an example adapted from something I've written earlier. PSExecRetry.log will contain the output of PsExec (including errors), although won't capture the StdOut/StdErr output of the subsequent command as-is.

PSExecRetry.ps1 is the PowerShell script with some basic retry logic:

#PSExecRetry.ps1

$LogFile = "PSExecRetry.log"
$defaultSleepSecs = 3
$RetryCount = 3
$StopLoop = $false
$retries = 1

try {
    # open the log file
    Start-Transcript -path $LogFile -append
    do {
        try
        {
            $Command = "C:\PSInstall.bat"
            Write-Host "Executing command" $Command ".`r"
            Invoke-Expression -Command $Command

            if ($LastExitcode -ne 0)
            {
                throw "Retry {0} of {1}" -f $retries, $RetryCount
            }
            else
            {
                $StopLoop = $true
            }
        }
        catch
        {
            if ($retries -gt $RetryCount)
            {
                Write-Host("Exception.Message={0}; InvocationInfo.ScriptName={1}" -f $_.Exception.Message, $_.InvocationInfo.ScriptName)
                Write-Host("Giving up after {0} retries.`r" -f $RetryCount)
                $StopLoop = $true
            }
            else
            {
                Write-Host("Exception.Message={0}; InvocationInfo.ScriptName={1}" -f $_.Exception.Message, $_.InvocationInfo.ScriptName)
                Write-Host("Exception, retrying in {0} seconds.`r" -f $defaultSleepSecs)
                Start-Sleep -Seconds $defaultSleepSecs
                $retries = $retries + 1        
            }
        }
    } While ($StopLoop -eq $false)
}
catch
{
    Write-Host("Exception.Message={0}; InvocationInfo.ScriptName={1}" -f $_.Exception.Message, $_.InvocationInfo.ScriptName)
}
finally
{
    Stop-Transcript
}

PSInstall.cmd is modified as follows:

#PSInstall.cmd

C:\PsExec.exe \\server2 -u "domain\admin" -p "abcd" "C:\Install.bat" -i -d -f -w

Install.bat stub:

#Install.bat

echo Hello world!
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.