How to execute bat file remotely using Psexec from team city inside the powershell command

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!