sql >> Database teknologi >  >> RDS >> Sqlserver

Sådan anvender du farver i powershell-output

Se mit svar til lignende spørgsmål til dette.

Communary.ConsoleExtensions [link] kan hjælpe dig

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

Ovenstående kommando vil farvelægge filtyper og fremhæve dumpfiler.

For at gemme et farveoutput skal du gemme i et format, der bevarer farve, som RTF eller HTML. Txt (almindelig tekstfil) gemmer kun tekst.

Koden nedenfor gemmer dit output som en html-fil.

$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

Linjen:

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

...kontrollerer for linje i html-outputtet, der er en tabelrække.

Linjen:

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

...bruger et RegEx til at erstatte indholdet af 2. tabelcelle med et skrifttype-tag med farven grøn. Dette er en meget enkel RegEx-søgning og -erstatning, der kun vil farve den anden kolonne .

Og her er endnu en implementering af kun konsol farvelægning, baseret på dette 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

REDIGER. at besvare OPs anmodning

$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



  1. Objektreference, c#

  2. PostgreSQL - næste seriel værdi i en tabel

  3. Flere stigende rækkefølge virker ikke i Mysql

  4. Django Call Stored Procedure på Second Database