首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PowerShell脚本即服务

PowerShell脚本即服务
EN

Stack Overflow用户
提问于 2020-12-30 23:45:04
回答 1查看 40关注 0票数 0

我已经创建了一个脚本,希望将其作为服务运行。它使用Invoke-RestMethod来监视服务器上的应用程序,当它检测到不需要的条件时,它会重新启动应用程序。当我使用我的用户帐户运行它时,它工作得很好,但当我将它作为服务(作为系统)运行时,什么也没有发生。我将大多数控制台输出输出到一个变量,然后在循环结束时,将变量的内容作为临时日志写入文件。这个文件永远不会被写入。

我使用this guide创建了这个服务,它似乎启动正常并运行……只是什么都没有发生。

下面是我正在使用的脚本,服务应该执行该脚本。

代码语言:javascript
复制
#Configuration Options
$ESHost = ""
$ESPort = 9200
$ESPro = ""
$ESAPI = ""
$ESAPI.Cred = ""
$LogRoot = ""
$LSHost = ""
$LSPort = 9600
$LSPro = ""
$SMTP = @{
  server = ""
  to = ""
  from = ""
  port = 25
}

#Look for Logstash service
While (Get-Service Logstash) {
  $Date = Get-Date -Format "MMddyyyy_HHmmss"
  #Verify service is running
  if ((Get-Service Logstash).Status -eq "Running") {
    $log += "$(Get-Date -Format 'MM-dd-yyyy HH:mm:ss')  *****Start Health Check*****`n"
    #Collect list of pipelines
    $Pipelines = ((Invoke-RestMethod -Uri "$($LSPro)://$($LSHost):$($LSPort)/_node/stats/pipelines").pipelines.PSObject.Properties | Where-Object {$_.MemberType -eq "NoteProperty"}).Name
    $log += "$(Get-Date -Format 'MM-dd-yyyy HH:mm:ss')  Running pipelines: $($pipelines.count)`n"
    foreach ($pipeline in $pipelines) {
      $log += "  $($pipeline)`n"
    }
    $down = @()
    #Check health of each pipeline
    foreach ($pipeline in $pipelines) {
      $log += "$(Get-Date -Format 'MM-dd-yyyy HH:mm:ss')  Starting health check on $($pipeline) pipeline`n"
      $pipelineinfo = Invoke-RestMethod -Uri "$($LSPro)://$($LSHost):$($LSPort)/_node/stats/pipelines/$($pipeline)"
      $log += "$(Get-Date -Format 'MM-dd-yyyy HH:mm:ss')  $($pipeline) ephemeral id $($pipelineinfo.ephemeral_id)`n"
      $log += "$(Get-Date -Format 'MM-dd-yyyy HH:mm:ss')  $($pipeline) queue size (bytes) $([string]::Format('{0:N0}',($pipelineinfo.pipelines.$pipeline.queue.queue_size_in_bytes)))`n"
      $log += "$(Get-Date -Format 'MM-dd-yyyy HH:mm:ss')  $($pipeline) queue capacity (bytes) $([string]::Format('{0:N0}',($pipelineinfo.pipelines.$pipeline.queue.max_queue_size_in_bytes)))`n"
      $log += "$(Get-Date -Format 'MM-dd-yyyy HH:mm:ss')  $($pipeline) queued events $($pipelineinfo.pipelines.$pipeline.queue.events)`n"
      if ($pipelineinfo.pipelines.$pipeline.hash -eq $null) {
        $log += "$(Get-Date -Format 'MM-dd-yyyy HH:mm:ss')  $($pipeline) down`n"
        $down += $pipeline
      }
      if ($pipelineinfo.pipelines.$pipeline.queue.queue_size_in_bytes -eq $pipelineinfo.pipelines.$pipeline.queue.max_queue_size_in_bytes) {
        $log += "$(Get-Date -Format 'MM-dd-yyyy HH:mm:ss')  $($pipeline) queue at max capacity"
        Try {
          Send-MailMessage -SmtpServer $smtp.server -To $smtp.to -From $smtp.from -Subject "Pipeline Queue Full" -Port $smtp.port -Body "Pipeline $($pipeline)'s queue appears to be at full capacity"
        }
        Catch {
           log += "$(Get-Date -Format 'MM-dd-yyyy HH:mm:ss')  $($error[0].CategoryInfo.Activity)"+": "+"$($error[0].Exception.Message)`n"
        }
      }
      if (($pipelineinfo.pipelines.$pipeline.queue.queue_size_in_bytes -ne $pipelineinfo.pipelines.$pipeline.queue.max_queue_size_in_bytes) -and ($pipelineinfo.pipelines.$pipeline.hash -ne $null)) {
        $log += "$(Get-Date -Format 'MM-dd-yyyy HH:mm:ss')  $($pipeline) up`n"
      }
    }
    #Actions on unhealthy pipelines
    if ($down.count -gt 0) {
      foreach ($pipe in $down) {
        Write-Host "$(Get-Date)  Pipeline $($pipe) appears to be down. Collecting logs..."
        #Compress logs for each failed pipeline
        $log += "$(Get-Date -Format 'MM-dd-yyyy HH:mm:ss')  Collecting $($pipe) logs...`n"
        Try {
          Compress-Archive -LiteralPath "$($LogRoot)\pipeline_$($pipe).log" -CompressionLevel Fastest -DestinationPath "$($LogRoot)\pipeline_$($pipe)_$($Date).zip"
        }
        Catch {
           log += "$(Get-Date -Format 'MM-dd-yyyy HH:mm:ss')  $($error[0].CategoryInfo.Activity)"+": "+"$($error[0].Exception.Message)`n"
        }
      }
      #Compress logstash log
      $log += "$(Get-Date -Format 'MM-dd-yyyy HH:mm:ss')  Collecting logstash-plain.log...`n"
      Try {
        Compress-Archive -LiteralPath "$($LogRoot)\logstash-plain.log" -CompressionLevel Fastest -DestinationPath "$($LogRoot)\logstash-plain_$($Date).zip"
      }
      Catch {
        log += "$(Get-Date -Format 'MM-dd-yyyy HH:mm:ss')  $($error[0].CategoryInfo.Activity)"+": "+"$($error[0].Exception.Message)`n"
      }
      #Send compressed logs in email notification
      $log += "$(Get-Date -Format 'MM-dd-yyyy HH:mm:ss')  Sending mail notification`n"
      Try {
        Send-MailMessage -SmtpServer $smtp.server -To $smtp.to -From $smtp.from -Subject "Pipeline Down Detected" -Port $smtp.port -Attachments (Get-ChildItem "$LogRoot\*.zip")
      }
      Catch {
        log += "$(Get-Date -Format 'MM-dd-yyyy HH:mm:ss')  $($error[0].CategoryInfo.Activity)"+": "+"$($error[0].Exception.Message)`n"
      }
      #Remove compressed logs
      Remove-Item (Get-ChildItem "$LogRoot\*.zip") -Force -Confirm:$false
      Restart-Service Logstash
      Remove-Variable down
      Do {
        (Get-Service Logstash).Status
        Start-Sleep -Seconds 5
      }
      Until (((Get-Service Logstash).Status) -eq "Running")
      Send-MailMessage -SmtpServer $smtp.server -To $smtp.to -From $smtp.from -Subject "Logstash Successfully Restarted" -Port $smtp.port
    #Actions on no unhealthy pipelines
    } else {
      $log += "$(Get-Date -Format 'MM-dd-yyyy HH:mm:ss')  All pipelines running`n"
      Write-Host "$(Get-Date)  All pipelines up"
    }
  }
  $log += "$(Get-Date -Format 'MM-dd-yyyy HH:mm:ss')  *****End Health Check*****`n"
  $log | Out-File "$($LogRoot)\MonitorService.log" -Append
  Remove-Variable log
  Start-Sleep -Seconds 300
}
EN

回答 1

Stack Overflow用户

发布于 2020-12-31 01:53:20

发现了一些我忽略的问题。

  1. 在用户系统下使用交互式PowerShell会话运行脚本时,我能够识别出文件访问权限的几个问题。已通过将文件复制到临时目录,然后对其执行操作来解决这些问题。

  1. 最重要的是,我使用的指南不能正常工作。运行'NSSM edit‘后,它显示该服务仅执行PowerShell,没有定义脚本的参数。因此,该服务只是在执行一个空的powershell会话*facepalm。
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65509622

复制
相关文章

相似问题

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