<# CheckGateway.ps1 V2 - 22/11/2011 (Auteur: Damien BENOIT) Script permettant de trouver quelle(s) machine(s) possède la passerelle x.x.x.x et de la modifier. Modifier au besoin les variables globales $OU, $GoodGateway et $LogPath. V2 : Ajout de la modification automatique de la passerelle et des variables globales. #> # Récupération de la date/heure pour le log $Date = Get-Date -uformat "%d-%m-%Y_%Hh%Mm" $DateLog = Get-Date -uformat "%d-%m-%Y %Hh %Mm" # Variables globales ############################## $OU = "OU=ordinateurs,DC=ndlp,DC=fr" $GoodGateway = "172.16.122.240" $LogPath = "D:\Logs_scripts\Check_Gateway_$Date.log" ################################################### # Formatage du fichier de log Write "# Vérification du $DateLog #" > $LogPath Write "######################################" >> $LogPath Write " " >> $LogPath # Recheche des objets (ordinateurs) dans l'OU et les sous-ou $Objet = "computer" $objDomaine = New-Object System.DirectoryServices.DirectoryEntry("LDAP://$OU") $objSearcher = New-Object System.DirectoryServices.DirectorySearcher($objDomaine,"(objectCategory=$Objet)",@('name')) $ComputerList = $objSearcher.FindAll() | %{$_.properties.name} # Somme totale des objets dans l'OU et les sous-ou $Total=$ComputerList.count Foreach ($PC IN $ComputerList) { $tot++ # Barre de progression Write-Progress -Activity "Recheche en cours..." -CurrentOperation "Ordinateur: $PC" -Status "Effectué: $tot/$Total" -PercentComplete ($tot/$Total*100) Write-Host -ForegroundColor Yellow "Test de l'ordinateur $PC en cours.`r" # Test si l'ordinateur est allumé $PingResult = Get-WmiObject Win32_PingStatus -f "Address='$PC'" $StatusCode = $PingResult.statuscode # Si le statuscode = 0 alors l'ordinateur est allumé IF($StatusCode -eq 0) { # Conversion de l'adresse IP en chaine du type 172.16.x.x $IPcible = $PingResult.IPV4Address.IPAddressToString Write-Host -ForegroundColor Green "`t$PC (IP: $IPcible) online.`r" # Récupération de la passerelle par défaut de l'ordinateur $GetDefaultGateway = Get-WmiObject -Class "Win32_NetworkAdapterConfiguration" -ComputerName "$PC" -Filter "IPEnabled = $true" $Gateway = $GetDefaultGateway.DefaultIPGateway # Si la mauvaise passerelle est trouvé, changement + envoi dans le fichier de log IF($Gateway -ne $GoodGateway) { $GetComputerName = Get-WmiObject -Class "Win32_ComputerSystem" -ComputerName "$PC" $ComputerName = $GetComputerName.Name $ComputerDomain = $GetComputerName.Domain $fqdn = $ComputerName+"."+$ComputerDomain Write-Host -ForegroundColor Red "`tL'ordinateur: $fqdn possède la passerelle $Gateway" # Modification automatique de la passerelle $GetDefaultGateway.SetGateways("$GoodGateway") # Vérification de la nouvelle passerelle $GetNewGateway = Get-WmiObject -Class "Win32_NetworkAdapterConfiguration" -ComputerName "$PC" -Filter "IPEnabled = $true" $NewGateway = $GetNewGateway.DefaultIPGateway Write-Host -ForegroundColor Green "`tLa nouvelle passerelle est: $NewGateway" # Ecriture dans le fichier de log Write "L'ordinateur: $fqdn possède la passerelle $Gateway" >> $LogPath Write "La nouvelle passerelle de $fqdn est: $NewGateway" >> $LogPath } } }