首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >启动服务前检查服务器

启动服务前检查服务器
EN

Stack Overflow用户
提问于 2016-09-13 21:26:27
回答 2查看 203关注 0票数 0

我需要编辑一个脚本。有一个服务在两个Windows Server2008 R2上。它们是负载均衡的。我需要它,所以当我运行在主服务器和辅助服务器上启动服务的脚本时,甚至在启动这两个服务器上的服务之前,都会出去检查以确保主服务器已启动并正在运行,然后照常继续在两个服务器上启动服务。

代码语言:javascript
复制
# Start Appian
function StartAppian {
  $APNSVC = Get-Service -Name $AppianService

  if (!$APNSVC) {
    Write-Host "Appian Service does not exist"
    return
  }
  # Check to see if Appian's service is already started
  if ($APNSVC.Status -eq "Running") {
    if ($LB) {
      if ($MULEAPNSVC.Status -eq "Running") {
        Write-Host "Appian Service on the Load Balanced Server already is started!" -ForegroundColor Yellow
        return
      }
    }

    Write-Host "Appian Service already is started!" -ForegroundColor Yellow
    Read-Host "Press any key to return"
    return
  }

  # Check if DEV's Process Design has a writing_*.kdb file and delete it
  if ($Server -eq "DEV") {
    #gw1
    if (Test-Path $APPIAN_HOME\server\process\design\gw1\writing_*.kdb) {
      Write-Host "Removing writing_*.kdb from GW1" -ForegroundColor Yellow
      Remove-Item $APPIAN_HOME\server\process\design\gw1\writing_*.kdb
    }
    #gw2
    if (Test-Path $APPIAN_HOME\server\process\design\gw2\writing_*.kdb) {
      Write-Host "Removing writing_*.kdb from GW2" -ForegroundColor Yellow
      Remove-Item $APPIAN_HOME\server\process\design\gw2\writing_*.kdb
    }
  }
  Write-Host "Starting Appian"

  # Place the name of the service here to start for Appian
  Start-Service $AppianService
  Notify("StartAppian")
  if ($LB) {
    (Get-Service $MULEAPNSVC.Name -ComputerName $MULE).Start()
    Write-Host "Starting Mule's Appian" -ForegroundColor Magenta
  }

  cmd.exe "/C $APPIAN_HOME\server\_scripts\diagnostic\checkengine.bat -s >    $logdir\Startup.log"

  # These lines check the Startup log for fatals and errors at the beginning
  $fatals = Select-String FATAL $logdir\Startup.log
  $errs = Select-String ERROR $logdir\Startup.log

  # Check for errors and fatals again
  $fatals = Select-String FATAL $logdir\Startup.log
  $errs = Select-String ERROR $logdir\Startup.log

  Write-Host "Still warnings or Errors in CE" -ForegroundColor Yellow
  # Increment times
  $times = $times + 1

  # If times > threshold, email out error message
  if ($times -gt $threshold) {
    SendAlert("There is a problem with Appian - It won't start")
    Write-Host "There was a problem with Appian..it took too long to start -  emailing alert" -ForegroundColor Red
    Read-Host "Press any key to exit"
  }
}
Write-Host "Appian Started" -ForegroundColor Green
Read-Host "Press any key to return"
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-09-13 22:09:09

您可以使用脚本开头的一个简单的Test-Connection来完成此操作。

代码语言:javascript
复制
if ($ping = Test-Connection -ComputerName PrimaryServerName -Quiet) {
Write-Host "Host avalible"
}

else {
Write-Host "Host unavalible"
Exit
}
票数 0
EN

Stack Overflow用户

发布于 2016-09-16 02:50:05

还有其他建议吗?

代码语言:javascript
复制
if (Test-Connection ServerName -Count 1 -Quiet) {
Write-Host "Host avalible"
}

else {
Write-Host "Host unavalible"
Exit
}

这不管用。我想让它测试服务器是否启动,如果没有,退出脚本,如果是,继续执行脚本。在ISE上测试时,它可以工作,但当我启动脚本时,它不能

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39471301

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档