2016-09-16 1 views
0

要件:powershell出力での色の適用方法

私はpowershellで初心者です。以下のpsスクリプトは、サービスが開始状態または停止状態にあることについての詳細を提供していますが、私の必要条件はサービスがグリーンで強調表示されている場合は「スカイブルー」の背景色として表示する必要があります。色。どのように達成するのですか?

これに関するヘルプは非常に充実しています。

$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 @{ 

‘Host Name’ = $_.Systemname 
‘Service Display Name’ = $_.Displayname 
‘Service Name’ = $_.Name 
‘Start Mode’ = $_.Startmode 
‘Service Account Name’ = $_.Startname 
‘State’ = $_.State 
‘Status’= $_.Status 
} 
} 
} 
} 
} 

$Result | ConvertTo-HTML | Out-File C:\PowerSQL\service.htm 

答えて

1

my answerを参照してください。

Communary.ConsoleExtensions [link]は、上記のコマンドは、ファイルの種類をcoloriseし、ダンプファイルをハイライト表示されますあなた

Invoke-ColorizedFileListing C:\Windows -m *.dmp 

に役立つかもしれません。

カラー出力を保存するには、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' 

...色の緑とフォントタグと第二表のセルの内容を置き換えるために正規表現を使用しています。 これは非常に単純なRegEx検索&を置き換えるもので、2列目の色はとなります。

そして、ここでは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 

EDITに基づいて、コンソールのみ着色の別の実装です。 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 
+0

多くの方にお返事をいただき、ありがとうございます。私のコードにあなたのロジックを適用することができないので、あなたのロジックを自分のコードに適用してここに貼り付けてください。 – Franklin

+0

Great Tech Spud HostName、ServiceDisplayName、ServiceName、StartMode、ServiceAccountName、State、Statusなどの列ヘッダーを境界線で保持することで助けてください。 – Franklin

+0

@フランクリンボーダーはどういう意味ですか? HTMLの枠線ですか?テーブルをスタイルする場合は、 'Get-Help ConvertTo-Html -Full'を実行します。生成されたHTMLに添付するスタイルシートの場所を指定することができます。 cssの表のスタイリングのための検索/ Google。あなたのプロパティ属性にスペースを入れておくことを意味するならば、それらを単一引用符で囲んでラップしてください。私はあなたの質問に答えてくれたと感じていますので、答えとしてマークしてください。私はあなた自身をするために少し仕事を残す必要があります:) – TechSpud

関連する問題