2016-08-17 6 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)が付いています。 ConvertTo-Htmlへの裸の(入力なし)呼び出しを使用して、個々のテーブルが連結され、最後に単一の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

コンピュータごとにテーブルを1つ(タイトルはコンピュータ)にしたいですか?もしそうなら、それを実現可能にするために少し変更しなければならない。 –

+0

はいChris、スクリプトは複数のサーバーで実行されるので、テーブルの上に各サーバーサービスとサーバー名ごとに1つのテーブルが必要です。 – Tim

関連する問題