2016-01-13 62 views
0

試圖寫一個PowerShell腳本,執行以下操作:PowerShell的服務 - 輸出讀數錯誤

  1. 查找服務存在
  2. 如果找到,檢查服務狀態
  3. 報告服務狀態
  4. 如果服務狀態未運行啓動服務
  5. 報告服務狀態

問題:如果我運行腳本一次,最終的服務狀態將會顯示正在運行,但是之前打印的信息是它無法啓動服務。如果我再次運行腳本,它會翻轉並說服務已經啓動,但最終狀態已停止。

目前代碼:

# Setting variables 

$date = Get-Date # setting date 
$LogFile = "$env:UserProfile\Desktop\Log.txt" # setting log file - change as needed 
$ServiceName = "Spooler" # setting service name - change as needed 
$arrService = Get-Service -Name $ServiceName 
$arrServiceCheck = Get-Service -Name $ServiceName -ErrorAction SilentlyContinue -ErrorVariable NoService 


<# =============== DO NOT CHANGE ANYTHING BELOW THIS POINT =============== #> 

# Creating functions for re-use throughout script 

function CurrentServiceStatus { 
    Write-Output "Status of '$ServiceName' service:" | Out-File $LogFile -append 
    Get-Service $ServiceName | Select Name,DisplayName,Status | Format-List | Out-File $LogFile -append 
} 

# Starting script operation 

Write-Output "=========================================================================" | Out-File $LogFile 
Write-Output " Starting '$ServiceName' Service Monitor Script on $date" | Out-File $LogFile -append 
Write-Output "=========================================================================" | Out-File $LogFile -append 
Write-Output " " | Out-File $LogFile -append 

# Looking for service. If service was found, checking it's status. If status is not running, starting the service. 

if ($arrServiceCheck){ 
    Write-Output "'$ServiceName' service found on $env:ComputerName..." | Out-File $LogFile -append 
    Write-Output " " | Out-File $LogFile -append 

    ServiceStatus 

    if ($arrService.Status -ne "Running"){ 
     Start-Service $ServiceName | Out-File $LogFile -append 
    } 

    if ($arrService.Status -eq "Running"){ 
     Write-Output "$date - '$ServiceName' started..." | Out-File $LogFile -append 
     Write-Output " " | Out-File $LogFile -append 
     ServiceStatus 
    } 
    else{ 
     Write-Output "Error: '$ServiceName' service could not be started..." | Out-File $LogFile -append 
     Write-Output " " | Out-File $LogFile -append 
     ServiceStatus 
    } 
} 

# If service was not found, making note of it to log file 

if ($NoService){ 
    Write-Output " " | Out-File $LogFile -append 
Write-Output $NoService[0].exception.message | Out-File $LogFile -append 
    Write-Output " " | Out-File $LogFile -append 
} 

# Completing running of script 

Write-Output "=========================================================================" | Out-File $LogFile -append 
Write-Output " Finished '$ServiceName' Service Monitor Script on $date" | Out-File $LogFile -append 
Write-Output "=========================================================================" | Out-File $LogFile -append 
Write-Output " " | Out-File $LogFile -append 
Write-Output " " | Out-File $LogFile -append 

這裏是我的輸出...

運行1:

========================================================================= 
    Starting 'Spooler' Service Monitor Script on 01/13/2016 12:06:49 
========================================================================= 

'Spooler' service found on MW762OXI5K7M8D... 

Current status of 'Spooler' service: 


Name  : Spooler 
DisplayName : Print Spooler 
Status  : Stopped 



01/13/2016 12:06:49 - 'Spooler' started... 

Final status of 'Spooler' service: 


Name  : Spooler 
DisplayName : Print Spooler 
Status  : Stopped 



========================================================================= 
    Finished 'Spooler' Service Monitor Script on 01/13/2016 12:06:49 
========================================================================= 

運行2:

========================================================================= 
    Starting 'Spooler' Service Monitor Script on 01/13/2016 12:15:58 
========================================================================= 

'Spooler' service found on MW762OXI5K7M8D... 

Current status of 'Spooler' service: 


Name  : Spooler 
DisplayName : Print Spooler 
Status  : Stopped 



'Spooler' service could not be started... 

Final status of 'Spooler' service: 


Name  : Spooler 
DisplayName : Print Spooler 
Status  : Running 



========================================================================= 
    Finished 'Spooler' Service Monitor Script on 01/13/2016 12:15:58 
========================================================================= 

==========================================

這裏是校正後的代碼:

# Setting variables 

$date = Get-Date # setting date 
$LogFile = "$env:UserProfile\Desktop\NXLogMonitor\Logging\NXLogMonitor.txt" # setting log file - change as needed 
$ServiceName = "Spooler" # setting service name - change as needed 
$arrService = Get-Service -Name $ServiceName 
$arrServiceCheck = Get-Service -Name $ServiceName -ErrorAction SilentlyContinue -ErrorVariable NoService 


<# =============== DO NOT CHANGE ANYTHING BELOW THIS POINT =============== #> 

# Creating functions for re-use throughout script 

function ServiceStatus { 
    Write-Output "Status of '$ServiceName' service:" | Out-File $LogFile -append 
    Get-Service $ServiceName | Select Name,DisplayName,Status | Format-Table -AutoSize | Out-File $LogFile -append 
} 

# Starting script operation 

Write-Output "=========================================================================" | Out-File $LogFile 
Write-Output " Starting '$ServiceName' Service Monitor Script on $date" | Out-File $LogFile -append 
Write-Output "=========================================================================" | Out-File $LogFile -append 
Write-Output " " | Out-File $LogFile -append 

# Looking for service. If service was found, checking it's status. If status is not running, starting the service. 

if ($arrServiceCheck){ 
    Write-Output "'$ServiceName' service found on $env:ComputerName..." | Out-File $LogFile -append 
    Write-Output " " | Out-File $LogFile -append 

    if ($arrService.Status -eq "Running"){ 
     Write-Output "'$ServiceName' is already started..." | Out-File $LogFile -append 
     Write-Output " " | Out-File $LogFile -append 
     ServiceStatus 
    } 

    if ($arrService.Status -ne "Running"){ 
     Write-Output "'$ServiceName' service is not started..." | Out-File $LogFile -append 
     Write-Output " " | Out-File $LogFile -append 

     ServiceStatus 

     $arrService = Start-Service $ServiceName -PassThru 
     if ($arrService.Status -eq "Running"){ 
      Write-Output "$date - '$ServiceName' service started..." | Out-File $LogFile -append 
      Write-Output " " | Out-File $LogFile -append 
      ServiceStatus 
     } 
     elseif ($arrService.Status -ne "Running"){ 
      Write-Output "Error: '$ServiceName' service could not be started..." | Out-File $LogFile -append 
      Write-Output " " | Out-File $LogFile -append 
      ServiceStatus 
     } 
    } 
} 

# If service was not found, making note of it to log file 

if ($NoService){ 
    Write-Output " " | Out-File $LogFile -append 
Write-Output $NoService[0].exception.message | Out-File $LogFile -append 
    Write-Output " " | Out-File $LogFile -append 
} 

# Completing running of script 

Write-Output "=========================================================================" | Out-File $LogFile -append 
Write-Output " Finished '$ServiceName' Service Monitor Script on $date" | Out-File $LogFile -append 
Write-Output "=========================================================================" | Out-File $LogFile -append 
Write-Output " " | Out-File $LogFile -append 
Write-Output " " | Out-File $LogFile -append 

這裏是校正代碼的輸出:

========================================================================= 
    Starting 'Spooler' Service Monitor Script on 01/13/2016 13:53:08 
========================================================================= 

'Spooler' service found on MW762OXI5K7M8D... 

'Spooler' is already started... 

Status of 'Spooler' service: 

Name DisplayName Status 
---- ----------- ------ 
Spooler Print Spooler Running 


========================================================================= 
    Finished 'Spooler' Service Monitor Script on 01/13/2016 13:53:08 
========================================================================= 
+1

'$ arrS ervice.Status'在你的不同if/else語句之間不會改變,它在整個腳本中保持不變。再次運行'Get-Service'而不是檢查和「舊」變量值 –

回答

2

拿你做什麼開頭很好看:前

$arrService = Get-Service -Name $ServiceName 

$arrService.Status現在反映服務的狀態你開始做任何事情 - 讓我們想象一下,狀態爲Stopped

然後,在你隨後的腳本:

if ($arrService.Status -ne "Running"){ 
    Start-Service $ServiceName | Out-File $LogFile -append 
} 

這部分按預期工作 - 該服務沒有運行W¯¯如果你發佈了劇本,那麼現在就開始了,太棒了!

然後是腳本的問題的一部分:

if ($arrService.Status -eq "Running"){ 
     Write-Output "$date - '$ServiceName' started..." | Out-File $LogFile -append 
     Write-Output " " | Out-File $LogFile -append 
     FinalServiceStatus 
} 
else{ 
    Write-Output "Error: '$ServiceName' service could not be started..." | Out-File $LogFile -append 
    Write-Output " " | Out-File $LogFile -append 
    FinalServiceStatus 
} 

由於$arrService.Status仍然具有完全相同的設置如前(即使該服務本身可能現在已經改變了它的地位Running)時,else無論服務是否成功啓動,都會執行塊。


你需要再次調用Get-Service獲得服務的新價值,或者(我個人最喜歡的)使用Start-Service -PassThru「更新」的$arrService變量:

if($arrService.Status -ne 'Running') 
{ 
    $arrService = Start-Service $ServiceName -PassThru 
} 

if($arrService.Status -eq 'Running') 
{ 
    "Service is running" # although we don't know whether it was just started or already had been 
} 
else 
{ 
    "Service not running, starting must have failed" 
} 
+0

謝謝你,@Mathias。我在你的協助下工作。完全錯過了,但肯定明白這可能是一個問題。 -passthru開關也很好用:)。 現在,如果我可以只是格式正確... – Ilya

+0

這是一個不同的問題(隨意問一個新的問題)。提示:不要使用'Format-List',它會添加不需要的換行符。 –

+0

明白了。感謝您幫助解決@Mathias最大的問題。 – Ilya

0

任何有興趣,根據馬蒂亞斯的建議,這裏是我的代碼最終看起來像(邏輯去):

if ($arrServiceCheck){ 
    Write-Output "'$ServiceName' service found on $env:ComputerName..." | Out-File $LogFile -append 
    Write-Output " " | Out-File $LogFile -append 

    if ($arrService.Status -eq "Running"){ 
      Write-Output "'$ServiceName' is already started..." | Out-File $LogFile -append 
      Write-Output " " | Out-File $LogFile -append 
      FinalServiceStatus 
} 

    if ($arrService.Status -ne "Running"){ 
     CurrentServiceStatus 
     $arrService = Start-Service $ServiceName -PassThru 
     if ($arrService.Status -eq "Running"){ 
      Write-Output "$date - '$ServiceName' started..." | Out-File $LogFile -append 
      Write-Output " " | Out-File $LogFile -append 
      FinalServiceStatus 
     } 
     elseif ($arrService.Status -ne "Running"){ 
      Write-Output "Error: '$ServiceName' service could not be started..." | Out-File $LogFile -append 
      Write-Output " " | Out-File $LogFile -append 
      FinalServiceStatus 
     } 
    } 
}