jeudi 18 décembre 2014

Console avec minuteur (timer)

Nous allons voir dans cet article deux méthodes pour créer un timer et afficher le résultat d'une commande à intervalle régulier. Dans les exemples suivants, je vais afficher le résultat de la commande "Get-Process" toutes les 5 secondes. Mon premier script montre comment créer un timer dans une console PowerShell.

Exemple 1


 
#Création d'un timer.
$timer = new-object timers.timer
$timer.Interval = 1000 #Démarrage.
$nom = "T1"            #Identifiant de mon timer.
$boucle = 5            #Arrête le timer après 5 exécutions.

#Action.
$action = {
            #Initialisation.
            $timer.Interval = 5000  #En milliseconde.
            #Action.
            Write-Host (Get-Process | Out-String)
            #Supprimer le timer.
            if (--$boucle -eq 0)
              {
                $timer.stop()
                Unregister-Event $nom
                Write-host "Timer" $Nom "supprimé."
              }
          }

#Lancement du timer.
Register-ObjectEvent -InputObject $timer -EventName elapsed –SourceIdentifier $nom -Action $action
$timer.start()

#Fin.


Exemple 2


Le script suivant est une solution alternatif. Il  utilise le timer des fenêtres (Forms) pour afficher toutes les 5 secondes le résultat de la commande "Get-Process".

 
######################
# Console avec timer
######################

#Assembly.
[Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

#Plugin.
Get-Command -PSSnapin VirtualMachineManagerSnapin

#Paramêtres.
$global:consw = 600      #Largeur de la console au démarrage.
$global:consh = 300      #Hauteur de la console au démarrage.
$global:intvl = 5        #Intervale d'execution du timer (en seconde).
$global:titre = "Console avec timer"  #Titre de la fenêtre.

###########
# Timers
###########

#Timer.
$timer1 = new-object System.Windows.forms.timer
$timer1.Interval = 1000      #Démarrage rapide.

#Action.
$timer1.Add_Tick({
                   #Initialisation.
                   $global:labelA2.text = "Scan en cours ..."
                   #Intervalle après démarrage.
                   $timer1.Interval = ($global:intvl * 1000)
                   #Commande.
                   $global:labelA1.text = (Get-Process | Out-String)
                   #Fin.
                   $global:labelA2.text = "Dernier scan le " + (date -Format "dd-MM-yyyy à HH:mm:ss")
                })

#########################
# Fenêtre style console
#########################

#Ouvre une nouvelle fenêtre.
$formA = New-Object Windows.Forms.Form
$formA.text = $global:titre          
$formA.Size = New-Object System.Drawing.Size($global:consw,$global:consh)
$formA.BackColor = [System.Drawing.Color]::FromArgb(255,1,36,86)

#Création d'un label (Label).
$global:labelA1 = New-Object Windows.Forms.Label
$global:labelA1.Location = New-Object Drawing.Point 9,0
$global:labelA1.Size = New-Object System.Drawing.Size($formA.Size.Width,$formA.Size.Height)
$global:labelA1.ForeColor = [System.Drawing.Color]::FromArgb(255,255,255,255)
$global:labelA1.Font = New-Object System.Drawing.Font("Consolas","9",[System.Drawing.FontStyle]::Regular,3,0)
$global:labelA1.text = ""

#Création d'un label (Label).
$global:labelA2 = New-Object Windows.Forms.Label
$global:labelA2.Location = New-Object Drawing.Point 0,(($formA.Size.Height) - 56)
$global:labelA2.Size = New-Object System.Drawing.Size(($formA.Size.Width),20)
$global:labelA2.ForeColor = [System.Drawing.Color]::FromArgb(255,0,0,0)
$global:labelA2.BackColor = [System.Drawing.Color]::FromArgb(255,222,222,222)
$global:labelA2.Font = New-Object System.Drawing.Font("Consolas","9",[System.Drawing.FontStyle]::Regular,3,0)
$global:labelA2.TextAlign = "MiddleLeft"
$global:labelA2.text = "Initialisation ..."

#Events.
$formA.Add_Resize({
                    $global:labelA1.Size = New-Object System.Drawing.Size($formA.Size.Width,$formA.Size.Height)
                    $global:labelA2.Location = New-Object Drawing.Point 0,(($formA.Size.Height) - 56)
                    $global:labelA2.Size = New-Object System.Drawing.Size(($formA.Size.Width),20)
                 })

#Attache les contrôles à la fenêtre.
$formA.controls.add($global:labelA2)
$formA.controls.add($global:labelA1)

#Lance le timer.
$timer1.Start()

#Affiche le tout.
$formA.ShowDialog()

#Libère le timer.
$timer1.Stop()

#Fin.