2016-08-17 146 views
2

我想知道服務已停止,哪些設置爲自動,並且輸出文件轉到HTML頁面。在那個HTML輸出中,我想要爲該表服務器名稱之上的停止服務創建表。有人可以告訴我..在Powershell中使用HTML創建表格

$ServerListFile = "C:\Dv\Server_List.txt" 
$ServerList = Get-Content $ServerListFile -ErrorAction SilentlyContinue 
foreach ($Computername in $ServerList) { 
Write-Host "Automatic Services Stopped :" $Computername 
Get-wmiobject win32_service -computername $Computername -Filter "startmode 
= 'auto' AND state != 'running'" | Select DisplayName,Name,State,startmode 
| Format-Table -auto | Out-File C:\Dv\Report.html 
} 
+2

使用'ConvertTo-Html'。 –

回答

2

這樣的事情應該工作。

單桌

Get-Content "C:\Dv\Server_List.txt" -ErrorAction SilentlyContinue | ForEach-Object { 
    Write-Host "Automatic Services Stopped :" $_ 
    Get-WmiObject Win32_Service -ComputerName $_ -Filter "startmode = 'auto' AND state != 'running'" 
} | 
    Select-Object DisplayName, Name, State, StartMode | 
    ConvertTo-Html | 
    Out-File C:\Dv\Report.html 

多個表

這個版本創建使用ConvertTo-Html -Fragment表的順序。每個表格前面都有一個帶有計算機名稱的HTML標頭(在本例中爲h2)。單個表格在最後連接併合併成單個HTML文檔,使用ConvertTo-Html的裸(不輸入)調用。

# Generate the content which should be inserted as a set of HTML tables 
$preContent = Get-Content "C:\Dv\Server_List.txt" -ErrorAction SilentlyContinue | ForEach-Object { 
    $ComputerName = $_ 

    Write-Host "Automatic Services Stopped :" $ComputerName 

    # A table (and heading) will only be generated for $ComputerName if there are services matching the filter. 
    Get-WmiObject Win32_Service -ComputerName $ComputerName -Filter "startmode = 'auto' AND state != 'running'" | 
     Select-Object DisplayName, Name, State, StartMode | 
     ConvertTo-Html -PreContent "<h2>$ComputerName</h2>" -Fragment 
} 

# Generate the document which holds all of the individual tables. 
$htmlDocument = ConvertTo-Html -Head $htmlHead -PreContent $preContent | Out-String 
# Because the document has no input object it will have an empty table (<table></table>), this should be removed. 
$htmlDocument -replace '<table>\r?\n</table>' | Out-File C:\Dv\Report.html 

造型

你會發現這些生成的是相當原始的HTML。其中一個更好的方法來解決是使用CSS,這裏是使HTML表格看起來更漂亮我的一個片段:

$HtmlHead = '<style> 
    body { 
     background-color: white; 
     font-family:  "Calibri"; 
    } 

    table { 
     border-width:  1px; 
     border-style:  solid; 
     border-color:  black; 
     border-collapse: collapse; 
     width:   100%; 
    } 

    th { 
     border-width:  1px; 
     padding:   5px; 
     border-style:  solid; 
     border-color:  black; 
     background-color: #98C6F3; 
    } 

    td { 
     border-width:  1px; 
     padding:   5px; 
     border-style:  solid; 
     border-color:  black; 
     background-color: White; 
    } 

    tr { 
     text-align:  left; 
    } 
</style>' 

# Use the Head parameter when calling ConvertTo-Html 
... | ConvertTo-Html -Head $HtmlHead | ... 

注:當片段參數是提供與ConvertTo- 的頭僅適用HTML。

+0

謝謝克裏克:)它的工作..你能告訴我如何打印表上的計算機名稱。 – Tim

+0

你想每臺電腦一臺(用電腦作爲標題)嗎?如果是這樣,它必須改變一點,以使其可行。 –

+0

是的Chris,腳本將運行多個服務器,所以我需要一個桌面上的每個服務器服務和服務器名稱。 – Tim