我有一个在 TeamCity 中运行的 powershell 命令。当我尝试使用 psexec 从此 .ps 文件远程运行批处理文件时,我发现一旦远程执行开始,什么也没有发生。我尝试了多个论坛中讨论的几种方法,但毫无用处。
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"
我的构建日志:
[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。
答案1
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!