################################################################################## # Version 2.0 : -- Stable ################################################################################## # GET-REMOTELOG.ps1 #************************************************************************ ##################################################################################################################### # Fonction : Script pour filtré et scruté les eventslogs à distance sur les machines non inclus dans un domaine ##################################################################################################################### #DESCRIPTION DÉTAILLÉE: #Obtient des informations à propos des journaux des événements locaux ou des entrées stockées dans ces journaux #des événements de logs de type ERROR sur l'emsemble des serveurs du domaine ##################################################################################################################### #****************************# # Connexion sécurisé # #****************************# $cred = get-Credential "workgroup\lol" #****************************# # TITRE DE LA CONSOLE # #****************************# function Get-InputBox { param ([string]$Message, [string]$Title) $a = new-object -comobject MSScriptControl.ScriptControl $a.language = "vbscript" $a.addcode("function getInput() getInput = inputbox(`"$Message`",`"$Title`") end function") $InputTitle = $a.eval("getInput") $Host.UI.RawUI.WindowTitle = "$InputTitle" } get-inputbox -Message "Veuillez entrez un Titre dedie a la Console afin de la distinguer." -Title "Wizzard Title Console" ######################################################################### #|----------------------------------------------------| | #|**********************GET-EMAIL*********************| | #|####################################################| | #| # ** Function: ** # | | #| # ** Envoi d'email ** # | | #|----------------------------------------------------| | ######################################################################### ################## Information des paramétres pour l'envoi de mail PAR POWERSHELL ##############################################################| #$Server = "MyServeurSMTP" | #$From = "Emetteur@domaine.com" | #$To = "Destinataire@domaine.com" #,"Destinataire2@domaine.com" | #$Subject = "Mail de Test PowerShell" | #$Body = "TEXTE SOUHAITE ,"C:\boot.ini",(Get-Content -Path C:\fichier_a_integre_dans_lecorps | ForEach-Object {$_ + "`n"}) | ################################################################################################################################################| function Get-Email { param ([string]$Server, [string]$From, [string]$To, [string]$Subject, [string]$Body, [string]$Attach) ########################### # Constitution du message # ########################### #****************************# # Déclaration des éléments # # Server/TO/Subject/From # #****************************# $Email_Client = New-Object System.Net.Mail.SmtpClient $Email_Client.Host = $Server $Email_Client.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials $Email_Message = New-Object System.Net.Mail.MailMessage $Email_Message.From = $From $Email_Message.Subject = $Subject $EMAIL_Message.IsBodyHtml = $True $Email_Adresses = $Email_Message.To if ($To -is "System.Array") { foreach ($Adr in $To) { $Email_Adress = New-Object System.Net.Mail.MailAddress $Adr $Email_Adresses.Add($Email_Adress) } } else { $Email_Adress = New-Object System.Net.Mail.MailAddress $To $Email_Adresses.Add($Email_Adress) } $WorkBody = "" if ($Body -is "System.Array") { foreach ($Elt in $Body) { if ($Elt -like "*") { $File = $Elt.SubString(6,$Elt.Length-6) $WorkBody += "`n" + (Get-Content -Path $File) } else { $WorkBody += "`n" + $Elt } } } else { if ($Body -like "*") { $File = $Body.SubString(6,$Body.Length-6) $WorkBody = Get-Content -Path $File } else { $WorkBody = $Body } } $Email_Message.Body = $WorkBody #****************************# # OPTIONNEL # # Piece jointe # #****************************# if ($Attach -eq $null) {} { $Email_Attachments = $Email_Message.Attachments { if ($Attach -is "System.Array") { foreach ($Elt in $Attach) { if ($Elt -like "*" ) { $Dir = $Elt.SubString(5,$Elt.Length-5) foreach ($File in (Get-ChildItem -Path $Dir -Include "*.*" -Name)) { $File = $Dir + $File $Email_Attachment = New-Object System.Net.Mail.Attachment $File $Email_Attachments.Add($Email_Attachment) } } else { $File = $Elt $Email_Attachment = New-Object System.Net.Mail.Attachment $File $Email_Attachments.Add($Email_Attachment) } } } else { if ($Attach -like "*" ) { $Dir = $Attach.SubString(5,$Attach.Length-5) foreach ($File in (Get-ChildItem -Path $Dir -Include "*.*" -Name)) { $File = $Dir + $File $Email_Attachment = New-Object System.Net.Mail.Attachment $File $Email_Attachments.Add($Email_Attachment) } } else { $File = $Attach $Email_Attachment = New-Object System.Net.Mail.Attachment $File $Email_Attachments.Add($Email_Attachment) } } } } #################################### # Quelle epopee, enfin on envoi !! # #################################### $Email_Client.Send($Email_Message) } #################################### # Fin de la fonction Accolade !! # #################################### #*################################################################################################### ######################################################################### #|----------------------------------------------------| | #|******************GET-REMOTE LOG********************| | #|####################################################| | #| # ** Function: ** # | | #| # ** Récupération des logs** # | | #| # ** sur machine distante ** # | | #| # ** Direct ** # | | #|----------------------------------------------------| | ######################################################################### ################## Information des paramétres pour la requête de récuépration d'événement PAR POWERSHELL ########################################| #$Server = "SERVEUR" | #$Interval = "-10 ou +10" <<<<=== Exprimé en minutes | #$Type = "Information /Warning /Error" LE type d'énévement désiré | #$LogFile = "Application /SYSTEM ...." Type d'événément | #################################################################################################################################################| function Get-Remotelog { param ([string]$Server, [string]$Interval, [string]$Type, [string]$LogFile, [string]$MailServer, [string]$MailFrom, [string]$MailTo, [string]$MailSubject, [string]$MailBody) #[System.Diagnostics.EventLog]::GetEventLogs("DC") ############################# # ******** GENERAL ******** # # Déclaration des variables # ############################# ############################################################### # Déclaration des paramétres requis pour Get-EventLog. # # Declaration de la date du jour auquel la requête s'applique.# # Declaration du TYPE D'évenement SELECTIONER. # # Déclaration du TYPE DE journal d'évenement pour la requête. # # Déclaration des Serveurs. # # Déclaration des noms des fichier LOG temporaire unique # ############################################################### ############################## ### Infos Général ### ############################## #----------------------------------------| # JOURNAL D EVENEMENT ==> | #----------------------------# | # **Mise en place du script**# | # ** Récupération des logs** # | #----------------------------# | $StartDate = [DateTime]::now.Addminutes($Interval) $NewQueryEvent = New-Object system.Diagnostics.EventLog("$LogFile","$Server") $SearchQueryEvent = $NewQueryEvent.entries | Where-Object {$StartDate -le $_.TimeWritten -And $_.EntryType -eq "$Type"} $ViewQueryEvent = $SearchQueryEvent | Format-Table -property TimeGenerated,Source,EventID,Message -autosize $Road = "C:\PowerShell_SCRIPTS\EventLog_Remote\remote.log" $SaveQueryEvent = $ViewQueryEvent | Out-File $Road #########Envoi de mail si log detecté ######## ################################################### ######################################################## if ($ViewQueryEvent -eq $null) {} else { Get-Email -Server $MailServer -From $MailFrom -To $MailTo -Subject $MailSubject ` -Body (Get-Content -Path $Road | foreach { $_;"
" } ) #OU | ForEach-Object {$_ + "`n"} } Remove-Item $Road start-Sleep 1 } #################################### # Fin de la fonction Accolade !! # #################################### #*################################################################################################### #################################################################################################################################################| # BOUCLAGE + itégration de fonctions EMAIL & REMOTELOG | #################################################################################################################################################| #****************************# # DEBUT DE BOUCLE # #****************************# [System.Console]::TreatControlCAsInput = $TRUE while ($TRUE) { #-------------------------------# # Instruction dans la boucle # # On exécute les requetes # #-------------------------------# #---------------------------# # * Domain controller (DC)* # #---------------------------# #-----WARNING EVENT-----# Get-RemoteLog -Server ActiveDirectory -Interval "-10" -type Warning -logfile "System" -MailServer IPduSErveur ` -MailFrom moi@moi.com -MailTo moi@moi.com -MailSubject "SYSTEM !WARNING!" ` -Road 'C:\PowerShell_SCRIPTS\EventLog_Remote\remote.log' #-------------------------------# # PARAMETRE DE BOUCLAGE # #-------------------------------# $now = get-date Write-Host "" Write-Host "-------------------" Write-Host "$now Action While on Running time 9min " -foregroundcolor Blue -backgroundcolor Red Write-Host "-------------------" $endtime =Start-Sleep 540 while ((get-date) -le $endtime) { if ([System.Console]::KeyAvailable) { $key = [System.Console]::ReadKey($TRUE) # intercepter CTRL+C : if (($key.Modifiers -band [System.ConsoleModifiers]"control") -and ($key.Key -eq "C")) { # interrompre le script : exit } # fin du if (($key.Modifiers ... # si autre touche, abr?ger l'attente : else { break }}}} #****************************# # FIN DE BOUCLE # #****************************#