2017-05-08 37 views
1

我正在開發一個簡單的解決方案,如果在遠程服務器上停止任何服務,需要發送電子郵件警報。在HTML報告中獲取服務名稱

下面是流量:

  1. 在一個記事本文件寫我要監控
  2. PowerShell腳本會從文件中讀取所有服務的所有服務,並檢查其狀態。
  3. 如果有任何服務停止,它會將內容添加到我的HTML格式併發送一封電子郵件。

一切正常工作正常,但我得到System.ServiceProcess.ServiceController而不是實際的服務名稱。我在PowerShell控制檯上打印一些東西,然後在那裏獲取服務名稱。

代碼:

$Services = Get-Content D:\Services.txt 
foreach ($Service in $Services) { 
    Write-Host "Service Name: "$Service 
} 

當我執行此,我得到的服務名稱如預期。

if ($Service.Status -eq "Stopped") { 
    Write-Host "Service is stopped" 
    $dataRow = "<tr><td width='10%'>199.199.50.512</td><td width='10%' >$Service</td><td width='10%' align='center'>Stopped</td></tr>" 
    Add-Content $ServiceReport $dataRow; 
} 

這裏那裏$Service寫,我expecing我的服務名稱,但我在HTML報告中得到System.ServiceProcess.ServiceController

HTML報告:

Wrong HTML Report

回答

2

你需要具體說明要返回對象的哪個屬性。你可能想顯示名稱,所以這樣做:

$dataRow = "<tr><td width='10%'>199.199.50.512</td><td width='10%' >$($Service.displayname)</td><td width='10%' align='center'>Stopped</td></tr>" 

注意,這是在一個子表達式運算符$()爲了從一個雙引號的字符串中正確地訪問對象的屬性所包圍。