我想撰寫一個 PowerShell 腳本,它將計算機名稱作為引數并驗證所有設定為自動運行的服務是否正在運行。如果沒有,那么應該啟動這些服務
- 確保我們恢復狀態
- 如果服務已停止,我們能否獲取有關其停止時間等的詳細資訊。
- 將輸出生成為 html(例如,在默認站點上,以便可以在 IIS 上訪問它)
知道我怎樣才能做到最好嗎?
uj5u.com熱心網友回復:
$global:resultRow=$null
$global:result=$null
$global:htmlTemplate=$null
$global:table_content=$null
function HtmlTable
{
[OutputType([System.Object])]
Param (
[Parameter(Mandatory=$True)]
[String[]]
$tableRows,
[Parameter(Mandatory=$True)]
[String[]]
$tableHeaders,
[Parameter(Mandatory=$True)]
$tableWidthPercentage
)
$htmTable = "<html>
<style>
body {font-family:Segoe UI, Helvetica Neue, Helvetica, Arial, Verdana;font-size:12px;}
table{width: $tableWidthPercentage;border-collapse: collapse;}
th{background-color:#106ebe; color:white;font-family:Segoe UI, Helvetica Neue, Helvetica, Arial, Verdana;font-size:12px;border: 1px solid black;padding: 4px;text-align: left;border: 1px solid #ddd;}
tr:nth-child(even){background-color: #f2f2f2;}
td{font-family:Segoe UI, Helvetica Neue, Helvetica, Arial, Verdana;font-size:12px;border: 1px solid #ddd;padding: 4px;}
</style>
<table>
<tr>"
Foreach ($header in $tableHeaders)
{
$htmTable = "<th>$header</th>"
}
$htmTable ="</tr>
$tableRows
</table>"
return $htmTable
}
function get-serviceStatus{
Param (
[Parameter(Mandatory=$true)]
[String[]]
$computerName
)
$serviceList = get-service
foreach($service in $serviceList){
if($service.StartType -contains 'Automatic' -and $service.status -ne 'Running' ){
$resultRow = "
<tr>
<td>$service.Name</td>
<td>$service.DisplayName</td>
<td>$service.StartType</td>
<td>$service.Status</td>
</tr>
"
$table_content = $resultRow
}
}
$htmlTemplate= HtmlTable -tableWidthPercentage 80% -tableRows $table_content -tableHeaders Name,DisplayName,Title,StartType,status
return $htmlTemplate
}
$result = get-serviceStatus -computerName <Replace with ComputerName>
$result| Out-File service.htm
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/316449.html
標籤:电源外壳
