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을 찾을 수 있습니다.

$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 테이블이 예뻐 보일 수 있습니다 내 조각이야, CSS를 사용하는 것입니다 Html.

+0

감사합니다 크리스 :) 그 작동 .. 당신이 테이블 위에 컴퓨터 이름을 인쇄하는 방법을 말해 줄 수 있어요. – Tim

+0

컴퓨터 당 하나의 테이블을 원하십니까 (컴퓨터를 제목으로 사용 하시겠습니까?) 그렇다면 실현 가능성을 높이기 위해 조금 변경해야합니다. –

+0

예 Chris, 스크립트가 여러 서버에 대해 실행되므로 각 서버 서비스 및 서버 이름에 대해 테이블 ​​상단에 하나의 테이블이 필요합니다. – Tim

관련 문제