我有一个在 TeamCity 中运行的 powershell 命令。当我尝试使用此 .ps 文件中的 psexec 远程运行批处理文件时,我看到一旦远程执行开始,就没有任何反应。我尝试了多个论坛讨论的几种方法,但没有用。
Main.ps:
Invoke-Command -ScriptBlock {C:\PSInstall.bat}
Run Code Online (Sandbox Code Playgroud)
PSInstall.bat:
C:\Tools\psexec.exe -i -d "\\server2" -u "domain\admin" -p "abcd" -f -w cmd "C:\Install.bat"
Run Code Online (Sandbox Code Playgroud)
我的构建日志:
[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.
Run Code Online (Sandbox Code Playgroud)
我被困在这一点上,不知道发生了什么,非常感谢任何帮助。我已经在远程机器上设置了 EULA。
PsExec您在示例中给出的命令/参数格式错误,请尝试:
C:\Tools\PsExec.exe \\server2 -u "domain\admin" -p "abcd" "C:\Install.bat" -i -d -f -w
Run Code Online (Sandbox Code Playgroud)
另外,将它们与改编自我之前编写的内容的示例放在一起。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
}
Run Code Online (Sandbox Code Playgroud)
PSInstall.cmd修改如下:
#PSInstall.cmd
C:\PsExec.exe \\server2 -u "domain\admin" -p "abcd" "C:\Install.bat" -i -d -f -w
Run Code Online (Sandbox Code Playgroud)
Install.bat存根:
#Install.bat
echo Hello world!
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11808 次 |
| 最近记录: |