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

par | Déc 30, 2014 | PowerShell | 0 commentaires

 

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 LAPPLICATION INVENTAIRE #ETAT KO SI DES FICHIERS SONT PRESENT APRES 15 minutes (CES FICHIERS SONT CENSES ETRE RAPIDEMENT CONSOMMES) ##################################################################################################### #NB: LINTERVALLE DE TEMPS DE COMPARAISON EST CELLE DE LINTERVALLE DEXECUTION 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 = newObject ComObject Mom.ScriptAPI #Verification de lexistence dune source ayant le nom du script dans leventlog operation manager pour loguer certains events Function NewEventSource { if(!(TestPath « HKLM:\SYSTEM\CurrentControlSet\services\eventlog\Operations Manager\$scriptname« )) { NewEventLog LogName « Operations Manager«  Source $scriptname } } #Verification de lexistence du dossier $DirPath if (!(testpath Path $DirPath)) { writehost « le dossier $DirPath est introuvable«  NewEventSource WriteEventLog LogName « Operations Manager«  Source $scriptname EntryType Error EventId 1004 Message « le dossier $DirPath est introuvable«  Exit } $Files=GetChildItem Path $DirPath | WhereObject {$_.name match $filename AND $_.name notmatch $excludedfiles AND $_.lastwritetime lt (getdate).AddMinutes(15) AND $_.LastWriteTime.ToLongTimeString() notmatch $excludetime} | SelectObject Property Name ExpandProperty Name If ($Files) { writehost ForegroundColor yellow BackgroundColor black « Les fichiers suivants sont présent depuis 15 minutes dans le dossier $DirPath:«  foreach ($file in $Files) { writehost $file } NewEventSource WriteEventLog 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«  writehost «  » writehost ForegroundColor yellow BackgroundColor black « ETAT $status«  } Else { writehost ForegroundColor green « Tout les fichiers présents il y a 15 minutes dans le dossier $DirPath ont été consommés«  NewEventSource WriteEventLog 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«  writehost «  » writehost 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

 

 

0 commentaires

Soumettre un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *