Le script ci-dessous génère un rapport formaté dans la ligne de commande, de l’état des instances d’une liste de classe.
#SCOM - SCRIPT DE REQUETE D'ETAT DES OBJETS D'UNE LISTE DE CLASSE
#Variables
$MGroup = "MyGroup"
$MS= "MyMS"
$cred = Get-Credential "MyDomain\"
#Liste de classe a recuperer
$ClassList = ('SQL DB Engine','SQL Database','SQL DB File','SQL Agent','SQL Reporting Services','SQL Analysis Services')
#Import du module SCOM
try
{
Import-Module -Name OperationsManager -ErrorAction stop
}
catch
{
write-host -ForegroundColor red "Erreur lors de l'import du module SCOM"
}
#Connection au management group $MGroup
New-SCOMManagementGroupConnection -ComputerName $MS -Credential $cred
#Recuperation des classes
$MonitoringClasses = $ClassList | foreach {Get-SCOMClass -DisplayName $_}
if ($MonitoringClasses -eq $null)
{
write-host "ERROR - UNABLE TO RETRIEVE CLASSES" -ForegroundColor red
}
write-host "#################################################### `n"
write-host "SCOM Management Group: $MGroup `n"
write-host "#################################################### `n`n"
#Recuperation des instances de chaque classe
foreach ($class in $MonitoringClasses)
{
$MonitoringObjects = $class | Get-SCOMClassInstance -erroraction silentlycontinue | Sort-Object -Descending -Property HealthState
write-host -BackgroundColor white -ForegroundColor blue " **************************************************************** `n"
write-host -BackgroundColor white -ForegroundColor blue " *** OBJETS DE LA CLASSE "$class.displayname" `n"
write-host -BackgroundColor white -ForegroundColor blue " **************************************************************** `n`n"
write-host " --- $(get-date -Format F) --- `n`n"
write-host " NUMBER OF OBJECT: $($MonitoringObjects | measure-object | select-object -property count -expandproperty count) `n`n"
write-host " STATE ----------------- OBJECT`n`n"
foreach ($object in $MonitoringObjects)
{
switch ($object.HealthState)
{
"Success" {write-host " " -NoNewline ; write-host -ForegroundColor green $object.HealthState -NoNewline ; write-host " -------- " -NoNewline ; write-host $object.FullName}
"Error" {write-host " " -NoNewline ; write-host -ForegroundColor red $object.HealthState -NoNewline ; write-host " -------- " -NoNewline ;write-host $object.FullName}
"Warning" {write-host " " -NoNewline ; write-host -ForegroundColor yellow $object.HealthState -NoNewline ; write-host " -------- " -NoNewline ;write-host $object.FullName}
"Uninitialized" {write-host " " -NoNewline ; write-host -ForegroundColor blue $object.HealthState -NoNewline ; write-host " -------- " -NoNewline ;write-host $object.FullName}
}
}
"`n"
}
#Fermeture de la connexion
Get-SCOMManagementGroupConnection | Remove-SCOMManagementGroupConnection