2016-09-16 30 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]可以帮助你

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 

行:

if ($_ -like '<tr><td>*') { 

...检查在HTML输出为一个表行线。

行:

$_ -replace '^(.*?)(<td>.*?</td>)<td>(.*?)</td>(.*)','$1$2<td><font color="green">$3</font></td>$4' 

...使用正则表达式与绿颜色的字体标签来代替第二表格单元格的内容。 这是一个非常简单的正则表达式搜索&替换,只会将第二列的颜色变为

而这里的控制台只有着色的另一种实现方式,基于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 
+0

非常感谢TechSpud的回复。我无法将您的逻辑应用于我的代码,因此您可以将您的逻辑应用于我的代码并粘贴到此处 – Franklin

+0

Great Tech Spud。请帮我保留列标题,例如HostName,ServiceDisplayName,ServiceName,StartMode,ServiceAccountName,State,Status以及边框。 – Franklin

+0

@Franklin你是什么意思的边界? HTML边界?如果您想对表格进行样式设置,请运行“Get-Help ConvertTo-Html -Full”。您可以指定样式表的位置以附加到生成的html。搜索/谷歌的CSS样式。如果你的意思是在你的属性中保留空格,那么用单引号将它们和所有对它们的引用包装起来。我觉得我已经回答了你的问题,那么你能把这个标记为答案吗?我需要给你一点工作来做你自己:) – TechSpud