1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
| Get-Date -Format dd/MM/yyyy-HH:mm:ss
# Positionnement dans le répertoire du script
$Dir = $MyInvocation.MyCommand.Path
$Dir = $Dir.Substring(0,($Dir.LastIndexOf("\")+1))
Set-Location $Dir
# Creation des dossiers sites, serveurs et log s'ils n'existent pas
$CurrentEAP = $ErrorActionPreference
$ErrorActionPreference = "silentlycontinue"
$FicExclu = ".\Clients-Exclu.txt"
New-Item -Path $FicExclu -ItemType File
$RepSites = ".\Sites\"
New-Item -Path $RepSites -ItemType directory
$RepClients = ".\Clients\"
New-Item -Path $RepClients -ItemType directory
$RepEtats = ".\Etats\"
New-Item -Path $RepEtats -ItemType directory
$RepLog = ".\Log\"
New-Item -Path $RepLog -ItemType directory
$RepLogWOL = ".\Log\WOL\"
New-Item -Path $RepLogWOL -ItemType directory
$ErrorActionPreference = $CurrentEAP
# Verifie si les fichiers necessaires existent
if((Test-Path ".\PARAMETRES.txt") -and (Test-Path ".\wol.exe")){
#Récupération des parametres
$Parametres = Get-Content -Path ".\PARAMETRES.txt"
foreach($param in $Parametres){
switch -Wildcard ($param){
"ORGANISME:*" {
$index = $param.IndexOf(":")+1
$nbre = $param.Length - $index
$ORG = ($param.Substring($index,$nbre)).Trim()
}
"ADDRIPSITE:*" {
$index = $param.IndexOf(":")+1
$nbre = $param.Length - $index
$AddrIPSite = ($param.Substring($index,$nbre)).Trim()
}
"ADDRIPSITEPRINC1:*" {
$index = $param.IndexOf(":")+1
$nbre = $param.Length - $index
$AddrIPSiteP1 = ($param.Substring($index,$nbre)).Trim()
}
"SERVEURP1:*" {
$index = $param.IndexOf(":")+1
$nbre = $param.Length - $index
$SRVP1 = ($param.Substring($index,$nbre)).Trim()
}
"ADDRIPSITEPRINC2:*" {
$index = $param.IndexOf(":")+1
$nbre = $param.Length - $index
$AddrIPSiteP2 = ($param.Substring($index,$nbre)).Trim()
}
"SERVEURP2:*" {
$index = $param.IndexOf(":")+1
$nbre = $param.Length - $index
$SRVP2 = ($param.Substring($index,$nbre)).Trim()
}
}
}
# Recherche des Etendues de notre Foret AD
$forest = [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest()
$sites = $forest.Sites
$scopes = $sites | where Name -like "$ORG*"
$scopes = $scopes.subnets
$scopes = $scopes | where Name -like "$AddrIPSite*"
# Parcour des Etendues IP
foreach($scope in $scopes){
$scopex = $scope.name
$scopex = $scopex.ToString()
$scopex = $scopex.Substring(0,$scopex.IndexOf("/"))
# Liste les plages adresses de l'Etendue
$pooladdr = netsh dhcp server \\dhcpserver scope $scopex show iprange
foreach($lign in $pooladdr){
if ($lign.contains("DHCP")){
$LignPool1 = $lign.Trim()
# Récupère Plage de début
$PoolAddrD = $LignPool1.Substring(0,$LignPool1.IndexOf("-"))
$PoolAddrD = $PoolAddrD.Trim()
# Récupère IP de début
$i1 = $PoolAddrD.IndexOf(".")+1
$PoolAddrIP1 = $PoolAddrD.Remove(0,$i1)
$i2 = $PoolAddrIP1.IndexOf(".")+1
$PoolAddrIP1 = $PoolAddrIP1.Remove(0,$i2)
$AddrIPD = $PoolAddrIP1.Substring(0,$PoolAddrIP1.IndexOf("."))
$i3 = $i1+$i2
# Supprime la plage de début pour la suite
$LignPool2 = $LignPool1.Remove(0,($LignPool1.IndexOf("-")+1))
$LignPool2 = $LignPool2.Trim()
# Récupère Plage de Fin
$PoolAddrF = $LignPool2.Substring(0,$LignPool2.IndexOf("-"))
$PoolAddrF = $PoolAddrF.Trim()
#Récupère IP de fin
$i4 = $PoolAddrF.IndexOf(".")+1
$PoolAddrIP2 = $PoolAddrF.Remove(0,$i4)
$i5 = $PoolAddrIP2.IndexOf(".")+1
$PoolAddrIP2 = $PoolAddrIP2.Remove(0,$i5)
$AddrIPF = $PoolAddrIP2.Substring(0,$PoolAddrIP2.IndexOf("."))
$i6 = $i4+$i5
}
}
# Liste les serveurs de l'AD
$scopeserveur = $scopex.Substring(0,$i3)
$listserveurs = Get-ADComputer -LDAPFilter '(&(objectCategory=computer)(OperatingSystem=*Server*))' -property * | where ipv4address -Like "$scopeserveur*"
# Recherche du serveur qui enverra le WOL
if (($scopex -like "$AddrIPSiteP1*") -or ($scopex -like "$AddrIPSiteP2*")){
# Forçage du nom de la machine qui enverra le WOL pour sitePrincipal1 et sitePrincipal2
if ($scopex -like "$AddrIPSiteP1*"){ $SRV = $SRVP1 }
if ($scopex -like "$AddrIPSiteP2*"){ $SRV = $SRVP2 }
}else{
# Recherche du serveur de proximité qui enverra le WOL
foreach($srvwol in $listserveurs){
$AddrSrvIP = ($srvwol.ipv4address).Remove(0,$srvwol.IPv4Address.IndexOf(".")+1)
$AddrSrvIP = $AddrSrvIP.Remove(0,$AddrSrvIP.IndexOf(".")+1)
$AddrSrv = $AddrSrvIP.Substring(0,($AddrSrvIP.IndexOf(".")))
if (([int]$AddrSrv -ge [int]$AddrIPD) -and ([int]$AddrSrv -le [int]$AddrIPF)){
$SRV = $srvwol.Name
$SRVIP = $srvwol.IPv4Address
}
}
}
# Liste des clients de l'Etendue
$listclients = netsh dhcp server \\dhcpserver scope $scopex show clients
# Initialise le chemin de la liste des clients du site
$cheminwolsites = $RepSites + $scope.site + ".txt"
# Enregistre la liste des clients du site
Set-Content -Path $cheminwolsites -Value $listclients
# Initialise le chemin de la liste des Mac clients du serveur
$cheminwolsrvmac = $RepClients + $SRV + "-Clients_Mac.txt"
# Initialise la liste des Macs clients du serveur
New-Item -Path $cheminwolsrvmac -ItemType File -Force
# Initialise le chemin de la liste des IP clients du serveur
$cheminwolsrvip = $RepClients + $SRV + "-Clients_IP.txt"
# Initialise la liste des IP clients du serveur
New-Item -Path $cheminwolsrvip -ItemType File -Force
# Initialise le chemin du Log des wol des clients du serveur
$cheminwollogmac = $RepLogWOL + $SRV + "-Log_WOL.txt"
# Initialise le Log des clients du serveur
New-Item -Path $cheminwollogmac -ItemType File -Force
# Liste l'adresse MAC, IP et le Masque sous réseau des clients
foreach($ligne in $listclients){
if ($ligne.contains($AddrIPSite)){
if($ligne.Contains("masque sous reseau")){
$ligne = $ligne.Trim()
# Récupère l'adresse IP
$IP = $ligne.Substring(0,$ligne.IndexOf("-"))
$LigneMasque = $ligne.Remove(0,($ligne.IndexOf("-")+1))
$LigneMasque = $LigneMasque.Trim()
$IP = $IP.Trim()
# Récupère le Masque sous réseau
$Masque = $LigneMasque.Substring(0,$LigneMasque.IndexOf("-"))
$LigneMac = $LigneMasque.Remove(0,$LigneMasque.IndexOf("-")+1)
$LigneMac = $LigneMac.Trim()
$Masque = $Masque.Trim()
# Récupère l'adresse MAC
$Mac = $LigneMac.Substring(0,17)
$Mac = $Mac.replace("-","")
# Enregistre le Log des clients du Serveur
Add-Content -Path $cheminwolsrvmac -Value $Mac
Add-Content -Path $cheminwolsrvip -Value $IP
}
}
}
# Suppression des exclusions
$ListMacEx = Get-Content $FicExclu
$ListMacFinal = Get-Content $cheminwolsrvmac
foreach($macex in $ListMacEx){
$ListMacFinal = $ListMacFinal -replace $macex,"$null"
}
$ListMacFinal = $ListMacFinal | where { $_ -ne "$null" }
Set-Content -Path $cheminwolsrvmac -Value $ListMacFinal
# Récupère le disque dur à utiliser
$DriveSRV = Get-WmiObject -Class Win32_LogicalDisk -ComputerName $SRV | where DriveType -EQ 3 | sort
$DriveSRVD = $DriveSRV[1].Name[0]
$DriveSRVL = $DriveSRV[1].Name
# Creation dossier sur serveur
$DirWolSrv = "\\" + $SRV + "\" + $DriveSRVD + "$\WOL\"
New-Item -Path $DirWolSrv -ItemType directory -Force
# Recopie de WOL.exe et liste des clients
$WolExe = ".\wol.exe"
Copy-Item $WolExe -Destination $DirWolSrv -Force
$WolListMac = $RepClients + $SRV + "-Clients_Mac.txt"
Copy-Item $WolListMac -Destination $DirWolSrv -Force
# Creation et Recopie du fichier témoin pour activer/desactiver le WOL
$WolEtat = $RepEtats + $SRV + "-Etat.txt"
if(!(Test-Path $WolEtat)){
New-Item -Path $WolEtat -ItemType File
Set-Content -Path $WolEtat -Value "1"
}
Copy-Item $WolEtat -Destination $DirWolSrv -Force
# Envoie de la commande WOL à partir du serveur vers la machine
$JobInvoke = Invoke-Command -ComputerName $SRV -ScriptBlock {
# Récupère le disque dur à utiliser
param($DriveLoc)
# Chemin local
$DirLoc = $DriveLoc + "\WOL\"
$DirLocWolExe = $DirLoc + "wol.exe"
# Verifie si le Wol est activé pour ce site
$WolEtatSrv = $DirLoc + $env:COMPUTERNAME + "-Etat.txt"
$EtatSrv = Get-Content $WolEtatSrv
if($EtatSrv.Contains("1")){
# Initialise le chemin du Log du serveur
$dateD = Get-Date -Format dd/MM/yyyy-HH:mm:ss
$cheminwollogmac = $DirLoc + $env:COMPUTERNAME + "-Log_WOL.txt"
Add-Content -Value $dateD -Path $cheminwollogmac
# Initialise le chemin des clients du serveur
$cheminwolclients = $DirLoc + $env:COMPUTERNAME + "-Clients_Mac.txt"
$lmac = Get-Content $cheminwolclients
# Execution du wol.exe sur les clients
foreach($cmac in $lmac){
Invoke-Expression "$DirLocWolExe $cmac"
# Enregistre le log de la machine
Add-Content -value ("Sending Magic Packet to $cmac") -path $cheminwollogmac
}
$dateF = Get-Date -Format dd/MM/yyyy-HH:mm:ss
Add-Content -Value $dateF -Path $cheminwollogmac
}
} -AsJob -Args $DriveSRVL
# Temporisation, on poursuit une fois la commande Invoke soit terminée
Wait-Job -Id $JobInvoke.Id
Remove-Job -Id $JobInvoke.Id
# Chemin du Log mac
$DirLogWolSrv = $DirWolSrv + $SRV + "-Log_WOL.txt"
# Recopie des logs de chaque serveur vers le serveur principal
Copy-Item $DirLogWolSrv -Destination $RepLogWOL -Force
# Suppression du répertoire WOL sur le serveur
Remove-Item $DirWolSrv -Recurse
}
}else{
Write-Host "Problème de répertoire, vérifier le répertoire d'execution de la tâche planifié ou vérifier que les fichiers/dossiers soient au bon endroit"
}
Get-Date -Format dd/MM/yyyy-HH:mm:ss |
Partager