簡體   English   中英

如何修改多種服務?

[英]How to modify for multiple services?

我一直在玩我在這里找到的腳本...這是一個非常優雅的腳本。 但是,每次嘗試修改它以對多個服務執行停止/啟動時,我都會破壞它。 缺少個人列出的每個。 有任何想法嗎? 這是用於啟動/停止服務的腳本。

$SvcName需要讀取以下服務: ftSysPolicyftSysInventoryftSysAlarmftSysSSNftSysRpcProvftSysEServiceftSysRASftSysMad

#Change these values to suit your needs:
$SvcName = 'Spooler'
$SvrName = 'localhost'

#Initialize variables:
[string]$WaitForIt = ""
[string]$Verb = ""
[string]$Result = "FAILED"
$svc = (Get-Service -ComputerName $SvrName -Name $SvcName)
Write-Host "$SvcName on $SvrName is $($svc.status)"
switch ($svc.Status) {
    'Stopped' {
        Write-Host "Starting $SvcName..."
        $Verb = "start"
        $WaitForIt = 'Running'
        $svc.Start()}
    'Running' {
        Write-Host "Stopping $SvcName..."
        $Verb = "stop"
        $WaitForIt = 'Stopped'
        $svc.Stop()}
    Default {
        Write-Host "$SvcName is $($svc.Status).  Taking no action."
    }
}
if ($WaitForIt -ne "") {
    try {  # For some reason, we cannot use -ErrorAction after the next statement:
        $svc.WaitForStatus($WaitForIt, '00:02:00')
    } catch {
        Write-Host "After waiting for 2 minutes, $SvcName failed to $Verb."
    }
    $svc = (Get-Service -ComputerName $SvrName -Name $SvcName)
    if ($svc.status -eq $WaitForIt) {$Result = 'SUCCESS'}
    Write-Host "$Result`: $SvcName on $SvrName is $($svc.Status)"
}

創建一個由服務名稱組成的數組,並遍歷重新啟動它們的邏輯:

$svcs = @('ftSysPolicy', 'ftSysInventory', 'ftSysAlarm', 'ftSysSSN', 'ftSysRpcProv', 'ftSysEService', 'ftSysRAS', 'ftSysMad')

foreach (svcName in $svcs) {

  [string]$WaitForIt = ""
  [string]$Verb = ""
  [string]$Result = "FAILED"
  $svc = (get-service -computername $SvrName -name $SvcName)
  Write-host "$SvcName on $SvrName is $($svc.status)"
  Switch ($svc.status) {
      'Stopped' {
          Write-host "Starting $SvcName..."
          $Verb = "start"
          $WaitForIt = 'Running'
          $svc.Start()}
      'Running' {
          Write-host "Stopping $SvcName..."
          $Verb = "stop"
          $WaitForIt = 'Stopped'
          $svc.Stop()}
      Default {
          Write-host "$SvcName is $($svc.status).  Taking no action."}
  }
  if ($WaitForIt -ne "") {
      Try {  # For some reason, we cannot use -ErrorAction after the next     statement:
          $svc.WaitForStatus($WaitForIt,'00:02:00')
      } Catch {
      Write-host "After waiting for 2 minutes, $SvcName failed to $Verb."
      }
      $svc = (get-service -computername $SvrName -name $SvcName)
      if ($svc.status -eq $WaitForIt) {$Result = 'SUCCESS'}
      Write-host "$Result`: $SvcName on $SvrName is $($svc.status)"
  }

}

或者,甚至更好的是,將邏輯放入函數中並進行調用。

另外,請檢查get-servicestop-servicestart-service cmdlet

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM