如何使用powershell命令中的团队城市中的Psexec远程执行bat文件

我有一个在TeamCity中运行的powershell命令。 当我尝试从这个.ps文件使用psexec远程运行一个batch file时,我看到一旦远程执行开始,什么都不会发生。 我尝试了在多个论坛讨论的几种方式,但没有用。

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" 

我的build立日志:

 [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. 

我被困在这一点,不知道发生了什么事,任何帮助,高度赞赏。 我已经在远程机器上设置了EULA。

您在示例中给出的PsExec命令/参数格式不正确,请尝试:

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

另外,把它们放在一起,用我之前写的东西改编。 PSExecRetry.log将包含PsExec的输出(包括错误),但不会捕获后续命令的StdOut/StdErr输出。

PSExecRetry.ps1是带有一些基本重试逻辑的PowerShell脚本:

 #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修改如下:

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

Install.bat存根:

 #Install.bat echo Hello world!