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
| 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 du répertoire Sites et Etats
$CurrentEAP = $ErrorActionPreference
$ErrorActionPreference = "silentlycontinue"
$RepSites = ".\Sites\"
New-Item -Path $RepSites -ItemType directory
$RepEtats = ".\Etats\"
New-Item -Path $RepEtats -ItemType directory
$RepLog = ".\Log\"
New-Item -Path $RepLog -ItemType directory
$FicExclu = ".\Clients-Exclu.txt"
New-Item -Path $FicExclu -ItemType file
$ErrorActionPreference = $CurrentEAP
# Verifie si les fichiers necessaires existent
if(Test-Path ".\PARAMETRES.txt"){
#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()
}
}
}
# 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
$site = $scope.Site
$scopex = $scopex.ToString()
$scopex = $scopex.Substring(0,$scopex.IndexOf("/"))
# Initialise le chemin de la liste des clients du site
$CheminSitesPC = $RepSites + $site + ".txt"
New-Item -Path $CheminSitesPC -ItemType File -Force
# Creation du fichier témoin pour activer/desactiver le Shutd
$ShutdEtat = $RepEtats + $site + ".txt"
if(!(Test-Path $ShutdEtat)){
New-Item -Path $ShutdEtat -ItemType File -Force
Set-Content -Path $ShutdEtat -Value "1"
}
# Liste les plages adresses de l'Etendue
$pooladdr = netsh dhcp server \\serverDHCP 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
$PoolAddrIP1 = $PoolAddrD.Remove(0,7)
$AddrIPD = $PoolAddrIP1.Substring(0,$PoolAddrIP1.IndexOf("."))
# 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
$PoolAddrIP2 = $PoolAddrF.Remove(0,7)
$AddrIPF = $PoolAddrIP2.Substring(0,$PoolAddrIP2.IndexOf("."))
}
}
# Récupération des machines de l'étendue
$i = $null
[int]$i = $AddrIPD
$ListePC = $null
$AddrIPD..$AddrIPF | foreach{
$PcIP = $scopex.Substring(0,7) + $i +"."
$PCs = $null
$PCs = Get-ADComputer -LDAPFilter '(&(objectCategory=computer))' -property * | where {($_.IPv4Address -Like "$PcIP*") -and (($_.Name -Like "PCA*") -or ($_.Name -Like "PCB*") -or ($_.Name -Like "TOTO*"))}
$ListePC = $ListePC + $PCs.name
$i = $i + 1
}
$ListePC = $ListePC | sort
# Enregistrement de la liste des pc du site
Add-Content -Path $CheminSitesPC -Value $ListePC
}
# Récupération des Sites
$ListSites = Get-Item -Path $RepSites* | sort
foreach($sitePcs in $ListSites){
$DirEtatSite = $RepEtats + $sitePcs.Name
$EtatSrv = Get-Content $DirEtatSite
if($EtatSrv.Contains("1")){
$ListPCs = Get-Content $sitePcs
# Suppression des exclusions
$ListPCsEx = Get-Content $FicExclu
foreach($Pcex in $ListPCsEx){
$ListPCs = $ListPCs -replace $pcex,"$null"
}
$ListPCsFinal = $ListPCs | where { $_ -ne "$null" }
# fichier de log du site
$NomLog = $RepLog + $sitePcs.Name
New-Item -Path $NomLog -ItemType File -Force
Add-Content -Value "----------------- `n" -path $NomLog
$DateD = Get-Date -Format dd/MM/yyyy-HH:mm:ss
Add-Content -Value $DateD -Path $NomLog
Add-Content -Value "----------------- `n" -path $NomLog
# Shutd des machines
Stop-Computer -ComputerName $ListPCsFinal -Force
# Enregistre le log des Shutds des machines
Add-Content -Value "----------------- `n" -path $NomLog
$DateF = Get-Date -Format dd/MM/yyyy-HH:mm:ss
Add-Content -Value $DateF -Path $NomLog
Add-Content -Value "----------------- `n" -path $NomLog
}
}
}
Get-Date -Format dd/MM/yyyy-HH:mm:ss |
Partager