PI Services

Le blog des collaborateurs de PI Services

Script Powershell–Scom: Surveillance consommation reguliere de fichiers dans un dossier

 

Le script ci-dessous surveille la consommation (création / suppression) régulière de fichiers dans un dossiers en excluant potentiellement des noms de fichiers et des heures de dernieres modification puis remonte l’état a SCOM.

Lien du script plus bas.

##################################################################################################### #SCRIPT DE SUPERVISION DU CHANGEMENT DE CONTENU DU DOSSIER ARRIVEE POUR L'APPLICATION INVENTAIRE #ETAT KO SI DES FICHIERS SONT PRESENT APRES 15 minutes (CES FICHIERS SONT CENSES ETRE RAPIDEMENT CONSOMMES) ##################################################################################################### #NB: L'INTERVALLE DE TEMPS DE COMPARAISON EST CELLE DE L'INTERVALLE D'EXECUTION DU SCRIPT #SONT EXCLUS DANS LES CRITERES DE RECHERCHE LES FICHIERS CREES ENTRE 22:00 ET 00:59 Param( [string]$computerName, [string]$DirPath, [regex]$excludedfiles="^(FILETOEXCLUDE)|(FILETOEXCLUDE)$", [regex]$filename="^.*(.*)$", [regex]$excludetime="^(22:).*|(23:).*|(00:).*$" ) $scriptname="CheckDirFileConso" #API Scom $api = new-Object -ComObject 'Mom.ScriptAPI' #Verification de l'existence d'une source ayant le nom du script dans l'eventlog operation manager pour loguer certains events Function NewEventSource { if(!(Test-Path "HKLM:\SYSTEM\CurrentControlSet\services\eventlog\Operations Manager\$scriptname")) { New-EventLog -LogName "Operations Manager" -Source $scriptname } } #Verification de l'existence du dossier $DirPath if (!(test-path -Path $DirPath)) { write-host "le dossier $DirPath est introuvable" NewEventSource Write-EventLog -LogName "Operations Manager" -Source $scriptname -EntryType Error -EventId 1004 -Message "le dossier $DirPath est introuvable" Exit } $Files=Get-ChildItem -Path $DirPath | Where-Object {$_.name -match $filename -AND $_.name -notmatch $excludedfiles -AND $_.lastwritetime -lt (get-date).AddMinutes(-15) -AND $_.LastWriteTime.ToLongTimeString() -notmatch $excludetime} | Select-Object -Property Name -ExpandProperty Name If ($Files) { write-host -ForegroundColor yellow -BackgroundColor black "Les fichiers suivants sont présent depuis 15 minutes dans le dossier $DirPath:" foreach ($file in $Files) { write-host $file } NewEventSource Write-EventLog -LogName "Operations Manager" -Source $scriptname -EntryType Warning -EventId 1005 -Message "Les fichiers suivants sont présent depuis 15 minutes dans le dossier $DirPath: $Files" $status="KO" write-host "" write-host -ForegroundColor yellow -BackgroundColor black "ETAT $status" } Else { write-host -ForegroundColor green "Tout les fichiers présents il y a 15 minutes dans le dossier $DirPath ont été consommés" NewEventSource Write-EventLog -LogName "Operations Manager" -Source $scriptname -EntryType Information -EventId 1000 -Message "Tout les fichiers présent il y a 15 minutes dans le dossier $DirPath ont été consommés" $status="OK" write-host "" write-host -ForegroundColor green "ETAT $status" } #Envoi de l'état a SCOM $bag = $API.CreatePropertyBag() If ($status -eq "KO") { $bag.AddValue("Status","Warning") } Else { $bag.AddValue("Status","Success") } $bag

 

 

Ajouter un commentaire

Loading