Question listing fichier avec couleur

Plus d'informations
il y a 5 ans 6 mois #27683 par nwx
listing fichier avec couleur a été créé par nwx
Bonjour,

J'ai mis en place il y a quelques temps un petit script me permettant la vérification d'exports qui sont réalisés a heure fixe tout les jours et qui m'envoie le résultat par mail.

Comme le résultat de cette liste est plutôt grande (quelques centaines de ligne) j'aurais souhaité pourvoir mettre ce fichier en forme a l'aide de couleur.

Voici mon code actuel :

#initialisation des dimensions de la fenêtre

$host.UI.RawUI.BufferSize = new-object System.Management.Automation.Host.Size(150,3000)

$date = Get-Date -Format F
$myComputer = Get-Content env:computername

$emailSmtpServer = \"$IP\"
$emailSmtpServerPort = \"$port\"

# Récupération d'information :
$corpDuMail = @()

$corpDuMail += get-childitem -recurse \\$partage1 -include \"*.html\"
$corpDuMail += get-childitem -recurse \\$partage2 -include \"*.html\"
$corpDuMail += get-childitem -recurse \\$partage3 -include \"*.html\"
$corpDuMail += get-childitem -recurse \\$partage4 -include \"*.html\"
$corpDuMail += get-childitem -recurse \\$partage5 -include \"*.html\"
$corpDuMail += get-childitem -recurse \\$partage6 -include \"*.html\"
$corpDuMail += get-childitem -recurse \\$partage7 -include \"*.html\"
$corpDuMail += get-childitem -recurse \\$partage8 -include \"*.html\"
$corpDuMail += get-childitem -recurse \\$partage9 -include \"*.html\"
$corpDuMail += get-childitem -recurse \\$partage10 -include \"*.html\"
$corpDuMail += get-childitem -recurse \\$partage11 -include \"*.html\"
$corpDuMail += get-childitem -recurse \\$partage12 -include \"*.html\"
$corpDuMail += get-childitem -recurse \\$partage13 -include \"*.html\"
$corpDuMail += get-childitem -recurse \\$partage14 -include \"*.html\"
$corpDuMail += get-childitem -recurse \\$partage15 -include \"*.html\"
$corpDuMail += get-childitem -recurse \\$partage16 -include \"*.html\"


# Préparation du mail :
$emailMessage = New-Object System.Net.Mail.MailMessage
$emailMessage.From = \"$Expéditeur\"
$emailMessage.To.Add(\"$Destintaire\")
$emailMessage.Subject = \"Rapport du $date\"
$emailMessage.IsBodyHtml = $false
$emailMessage.Body = $corpDuMail | Out-String

$SMTPClient = New-Object System.Net.Mail.SmtpClient($emailSmtpServer,$emailSmtpServerPort)

$SMTPClient.Send($emailMessage)

Voici un exemple de ce qui m'est retourné :

Répertoire : \\$partage1\SecteurA


Mode LastWriteTime Length Name
----

----
d---- 29/06/2018 18:06 Photos
-a--- 16/10/2018 02:21 231175 002154_dossier_.HTML
-a--- 16/10/2018 02:22 108930 002596_dossier_.HTML
-a--- 16/10/2018 02:21 216524 0001377_dossier_.HTML
-a--- 16/10/2018 18:20 186391 30002001_dossier_.HTML

Actuellement je vérifie humainement les dates et heures d'export des fichiers ce qui prend un peu de temps !

Dans le meilleur des cas j'aurais souhaité avoir le même type de résultat avec les condition suivantes :

si le fichier a une date de création supérieur a 12h alors le fichier n'a pas été généré il faut inscrire la ligne en rouge
si le fichier a une date inférieur a 12h alors le fichier a été généré correctement la ligne est inscrite en vert.

Est ce que cela est possible ?
Merci pour votre aide et vos conseils.

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 5 ans 6 mois #27685 par de França
Réponse de de França sur le sujet Re:listing fichier avec couleur
Hello,

Premier truc qui me fait mal aux yeux:

[code:1]
$corpDuMail += get-childitem -recurse \$partage1 -include \"*.html\"

$corpDuMail += get-childitem -recurse \$partage2 -include \"*.html\"

$corpDuMail += get-childitem -recurse \$partage3 -include \"*.html\"

$corpDuMail += get-childitem -recurse \$partage4 -include \"*.html\"

$corpDuMail += get-childitem -recurse \$partage5 -include \"*.html\"

$corpDuMail += get-childitem -recurse \$partage6 -include \"*.html\"

$corpDuMail += get-childitem -recurse \$partage7 -include \"*.html\"

$corpDuMail += get-childitem -recurse \$partage8 -include \"*.html\"

$corpDuMail += get-childitem -recurse \$partage9 -include \"*.html\"

$corpDuMail += get-childitem -recurse \$partage10 -include \"*.html\"

$corpDuMail += get-childitem -recurse \$partage11 -include \"*.html\"

$corpDuMail += get-childitem -recurse \$partage12 -include \"*.html\"

$corpDuMail += get-childitem -recurse \$partage13 -include \"*.html\"

$corpDuMail += get-childitem -recurse \$partage14 -include \"*.html\"

$corpDuMail += get-childitem -recurse \$partage15 -include \"*.html\"

$corpDuMail += get-childitem -recurse \$partage16 -include \"*.html\"

[/code:1]

N'y vois-tu pas une opportunité de le faire dans une boucle? Seul un chiffre change dans chacune de tes commandes, tu fais une boucle avec un $i qui s'incrémente:

[code:1]
# tant que i n'est pas égal à 16:
$corpDuMail += get-childitem -recurse $partage$i -include \"*.html\"[/code:1]


Sinon probablement une idée de solution du côté de cette réponse trouvé sur un thread. Apparemment il color le texte en output console avec -foregroundcolor puis le converti ensuite en HTML:

I found a solution for converting the Console Output to HTML, not the best way but it's working :-)



[code:1]
$content = get-content \"C:\Raid-Status_TMP.txt\"
foreach ($i in $content) {
If($i -Match \"Online\" -oR $i -like \"* Optimal\"«») {
write-host -ForegroundColor green $i
}
elseif($i -Match \"Offline\" -oR $i -Match \"Failed\" -oR $i -Match \"Ready\" -oR $i -Match \"Hot Spare\" -oR $i -Match \"Sub-optimal\" -oR $i -Match \"Degraded\" -oR $i -Match \"Rebuild\" -oR $i -Match \"Impact\" -oR $i -Match \"Error\"«») {
write-host -ForegroundColor red $i
}
else {
write-host -ForegroundColor black $i
}
}

# Check the host name and exit if the host is not the Windows PowerShell console host.
if ($host.Name -ne 'ConsoleHost')
{
write-host -ForegroundColor Red \"This script runs only in the console host. You cannot run this script in $($host.Name).\"
exit -1
}

# The Windows PowerShell console host redefines DarkYellow and DarkMagenta colors and uses them as defaults.
# The redefined colors do not correspond to the color names used in HTML, so they need to be mapped to digital color codes.
#
function Normalize-HtmlColor ($color)
{
if ($color -eq \"DarkYellow\"«») { $color = \"#eeedf0\" }
if ($color -eq \"DarkMagenta\"«») { $color = \"#012456\" }
return $color
}

# Create an HTML span from text using the named console colors.
#
function Make-HtmlSpan ($text, $forecolor = \"DarkYellow\", $backcolor = \"DarkMagenta\"«»)
{
$forecolor = Normalize-HtmlColor $forecolor
$backcolor = Normalize-HtmlColor $backcolor

# You can also add font-lifting training:bold tag here if you want a bold font in output.
return \"<span style='font-family:Courier New;color:$forecolor;background:$backcolor'>$text</span>\"
}

# Generate an HTML span and append it to HTML string builder
#
function Append-HtmlSpan
{
$spanText = $spanBuilder.ToString()
$spanHtml = Make-HtmlSpan $spanText $currentForegroundColor $currentBackgroundColor
$null = $htmlBuilder.Append($spanHtml)
}

# Append line break to HTML builder
#
function Append-HtmlBreak
{
$null = $htmlBuilder.Append(\"<br>\"«»)
}

# Initialize the HTML string builder.
$htmlBuilder = new-object system.text.stringbuilder
$null = $htmlBuilder.Append(\"<pre style='MARGIN: 0in 10pt 0in;line-height:normal';font-size:10pt>\"«»)

# Grab the console screen buffer contents using the Host console API.
$bufferWidth = $host.ui.rawui.BufferSize.Width
$bufferHeight = $host.ui.rawui.CursorPosition.Y
$rec = new-object System.Management.Automation.Host.Rectangle 0,0,($bufferWidth - 1),$bufferHeight
$buffer = $host.ui.rawui.GetBufferContents($rec)

# Iterate through the lines in the console buffer.
for($i = 0; $i -lt $bufferHeight; $i++)
{
$spanBuilder = new-object system.text.stringbuilder

# Track the colors to identify spans of text with the same formatting.
$currentForegroundColor = $buffer[$i, 0].Foregroundcolor
$currentBackgroundColor = $buffer[$i, 0].Backgroundcolor

for($j = 0; $j -lt $bufferWidth; $j++)
{
$cell = $buffer[$i,$j]

# If the colors change, generate an HTML span and append it to the HTML string builder.
if (($cell.ForegroundColor -ne $currentForegroundColor) -or ($cell.BackgroundColor -ne $currentBackgroundColor))
{
Append-HtmlSpan

# Reset the span builder and colors.
$spanBuilder = new-object system.text.stringbuilder
$currentForegroundColor = $cell.Foregroundcolor
$currentBackgroundColor = $cell.Backgroundcolor
}

# Substitute characters which have special meaning in HTML.
switch ($cell.Character)
{
'>' { $htmlChar = '>' }
'<' { $htmlChar = '<' }
'&' { $htmlChar = '&' }
default
{
$htmlChar = $cell.Character
}
}

$null = $spanBuilder.Append($htmlChar)
}

Append-HtmlSpan
Append-HtmlBreak
}

# Append HTML ending tag.
$null = $htmlBuilder.Append(\"</pre>\"«»)

return $htmlBuilder.ToString() > \"C:\Raid-Status_Mail.html\"[/code:1]


Message édité par: unfam0us, à: 16/10/18 13:10<br><br>Message édité par: unfam0us, à: 16/10/18 13:11

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 5 ans 6 mois #27687 par Laurent Dardenne
unfam0us écrit:

Premier truc qui me fait mal aux yeux:

:side:

Tutoriels PowerShell

Connexion ou Créer un compte pour participer à la conversation.

Temps de génération de la page : 0.072 secondes
Propulsé par Kunena