이와 유사한 질문은 my answer을 참조하십시오.
Communary.ConsoleExtensions [link]는
Invoke-ColorizedFileListing C:\Windows -m *.dmp
위의 명령은 파일 형식 및 하이라이트 덤프 파일을 colorise합니다 도움이 될 수 있습니다.
색상 출력을 저장하려면 RTF 또는 HTML과 같은 색상을 유지하는 형식으로 저장해야합니다. Txt (일반 텍스트 파일)는 텍스트 만 저장합니다.
아래 코드는 결과를 html 파일로 저장합니다.
$time = (Get-Date).AddYears(-2)
Get-ChildItem -Recurse | Where-Object {$_.LastWriteTime -lt $time} |
Select Directory,Name,LastWriteTime |
ConvertTo-Html -Title "Services" -Body "<H2>The result of Get-ChildItem</H2> " -Property Directory,Name,LastWriteTime |
ForEach-Object {
if ($_ -like '<tr><td>*') {
$_ -replace '^(.*?)(<td>.*?</td>)<td>(.*?)</td>(.*)','$1$2<td><font color="green">$3</font></td>$4'
} else {
$_
}
} | Set-Content "$env:TEMP\ColorDirList.html" -Force
라인 : 테이블 행 인 HTML 출력의 라인
if ($_ -like '<tr><td>*') {
... 체크한다.
라인은 :
$_ -replace '^(.*?)(<td>.*?</td>)<td>(.*?)</td>(.*)','$1$2<td><font color="green">$3</font></td>$4'
... 색상 녹색 글꼴 태그 2 테이블 셀의 내용을 대체 할 정규식을 사용합니다. 이것은 매우 간단한 RegEx 검색 & 대체입니다.이 경우 두 번째 열은에만 해당됩니다.
그리고 여기가 this link
$linestocolor = @(
'CSName Version OSArchitecture'
'------ ------- --------------'
'BENDER 6.1.7601 64-bit '
'LEELA 6.1.7601 64-bit '
'FRY 6.1.7600 64-bit '
'FARNSWORTH 6.1.7601 32-bit '
)
# http://www.bgreco.net/powershell/format-color/
function Format-Color {
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline=$true,Mandatory=$true)]
$ToColorize
, [hashtable][email protected]{}
, [switch]$SimpleMatch
, [switch]$FullLine
)
Process {
$lines = ($ToColorize | Out-String).Trim() -replace "`r", "" -split "`n"
foreach($line in $lines) {
$color = ''
foreach($pattern in $Colors.Keys){
if (!$SimpleMatch -and !$FullLine -and $line -match "([\s\S]*?)($pattern)([\s\S]*)") { $color = $Colors[$pattern] }
elseif (!$SimpleMatch -and $line -match $pattern) { $color = $Colors[$pattern] }
elseif ($SimpleMatch -and $line -like $pattern) { $color = $Colors[$pattern] }
}
if ($color -eq '') { Write-Host $line }
elseif ($FullLine -or $SimpleMatch) { Write-Host $line -ForegroundColor $color }
else {
Write-Host $Matches[1] -NoNewline
Write-Host $Matches[2] -NoNewline -ForegroundColor $color
Write-Host $Matches[3]
}
}
}
}
$linestocolor | Format-Color -Colors @{'6.1.7600' = 'Red'; '32-bit' = 'Green'}
# doesn't work...
# (Get-ChildItem | Format-Table -AutoSize) | Format-Color -Colors @{'sql' = 'Red'; '08/07/2016' = 'Green'}
# does work...
Format-Color -ToColorize (Get-ChildItem | Format-Table -AutoSize) -Colors @{'sql' = 'Red'; '08/07/2016' = 'Green'}
return
편집을 기반으로, 콘솔에만 색상의 다른 구현이다. OP 요청에 답하십시오
$Result = @()
foreach($server in Get-Content C:\PowerSQL\List.txt)
{
$Services=gwmi win32_service -computername $server | where {$_.Name -like ‘*SQL*’}
if(!(Test-Connection -Cn $server -BufferSize 16 -Count 1 -ea 0 -quiet))
{“Problem still exists in connecting to $server”}
else {
$services | ForEach {
If ($_)
{ $Result += New-Object PSObject -Property @{
HostName = $_.Systemname
ServiceDisplayName = $_.Displayname
ServiceName = $_.Name
StartMode = $_.Startmode
ServiceAccountName = $_.Startname
State = $_.State
Status = $_.Status
}
}
}
}
}
$Result | ConvertTo-HTML `
-Title "Services" `
-Body "<H2>The result of gwmi win32_service</H2> " `
-Property HostName,ServiceDisplayName,ServiceName,StartMode,ServiceAccountName,State,Status |
ForEach-Object {
if ($_ -like '<tr><td>*') {
switch ($_) {
{ $_ -like '*<td>Stopped</td>*' } {$color='red'}
{ $_ -like '*<td>Running</td>*' } {$color='green'}
Default {$color='white'}
}
$_.Replace('<tr>', "<tr bgcolor=`"$color`">")
} else {
$_
}
} | Set-Content C:\PowerSQL\service.htm -Force
답장을 보내 주신 많은 분들께 고맙습니다. 내 코드에 논리를 적용 할 수 없으므로 코드에 논리를 적용하고 여기에 붙여 넣으십시오. – Franklin
Great Tech Spud. HostName, ServiceDisplayName, ServiceName, StartMode, ServiceAccountName, State, Status와 같은 열 머리글을 테두리로 유지하여 도와주십시오. – Franklin
@ 프란 클린 국경이란 무엇입니까? HTML 테두리? 테이블의 스타일을 지정하려면'Get-Help ConvertTo-Html -Full'을 실행하십시오. 생성 된 html에 첨부 할 스타일 시트의 위치를 지정할 수 있습니다. css 테이블 스타일에 대한 검색/Google. 속성 속성에 공백을 두는 것을 의미한다면 작은 따옴표로 공백을 묶어 라. 나는 당신의 질문에 대답했다고 느낍니다. 그래서 이것을 답으로 표시 할 수 있습니까? 나는 너에게 약간의 일을 맡겨야한다. – TechSpud