Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
| wiki:windows:scripting:adpowershell [2022/11/18 12:04] – DEROUET Valentin | wiki:windows:scripting:adpowershell [2022/11/21 17:46] (Version actuelle) – DEROUET Valentin | ||
|---|---|---|---|
| Ligne 6: | Ligne 6: | ||
| Cette petite fiche concerne la gestion d'un AD avec Powershell. | Cette petite fiche concerne la gestion d'un AD avec Powershell. | ||
| - | ## Créer des OU et des sous OU | + | ### Vérifier un fichier .CSV | 
| + | |||
| + | ```powershell | ||
| + | Import-Csv C: | ||
| + | ``` | ||
| + | |||
| + | ## Créer des OU et des sous-OU | ||
| - | ### Exemple de fichier .CSV : | + | ### Exemple de fichier .CSV | 
| - | [Téléchargeable ici](http:// | + | [Téléchargeable ici](http:// | 
| #### Le script de création des OU : | #### Le script de création des OU : | ||
| Ligne 32: | Ligne 38: | ||
| ``` | ``` | ||
| - | ## Lister | + | ## Powershell sur les utilisateurs | 
| + | ### Lister les utilisateurs existant dans une OU | ||
| ```powershell | ```powershell | ||
| Get-ADUser | Get-ADUser | ||
| Ligne 40: | Ligne 47: | ||
| ``` | ``` | ||
| - | ## Création d' | + | {{ : | 
| - | ### Vérifier | + | ### Création d' | 
| - | ```powershell | + | ### Exemple de fichier .CSV | 
| - | Import-Csv C: | + | |
| - | ``` | + | |
| - | + | ||
| - | ### Exemple de fichier .CSV : | + | |
| - | [Téléchargeable ici](http:// | + | [Téléchargeable ici](http:// | 
| #### Le script de création utilisateurs : | #### Le script de création utilisateurs : | ||
| Ligne 108: | Ligne 111: | ||
| } | } | ||
| - | Read-Host -Prompt " | + | Read-Host -Prompt " | 
| ``` | ``` | ||
| ## Création des groupes | ## Création des groupes | ||
| - | ### Exemple de fichier .CSV : | + | ### Exemple de fichier .CSV | 
| - | [Téléchargeable ici](http:// | + | [Téléchargeable ici](http:// | 
| #### Le script de création des groupes : | #### Le script de création des groupes : | ||
| Ligne 140: | Ligne 143: | ||
|  |  | ||
| } | } | ||
| - | Read-Host -Prompt "Press Enter to exit" | + | Read-Host -Prompt "Appuyer sur ENTRER pour terminer." | 
| ``` | ``` | ||
| - | ## Définir les permissions | + | ## Mettre le groupe local dans le groupe global | 
| + | |||
| + | ### Exemple de fichier .CSV | ||
| + | |||
| + | [Téléchargeable ici](http:// | ||
| + | |||
| + | #### Le script d' | ||
| + | |||
| + | ```powershell | ||
| + | Import-Module ActiveDirectory | ||
| + | |||
| + | $List = Import-Csv " | ||
| + | |||
| + | |||
| + | foreach ( $Group in $List ) { | ||
| + | foreach ( $MemberOf in $Group.memberof -split ", " ) { | ||
| + | Add-ADGroupMember -Identity $MemberOf -Members $Group.group | ||
| + | } | ||
| + | write-Host " | ||
| + | } | ||
| + | ``` | ||
| ## Ajouter un utilisateur dans un groupe | ## Ajouter un utilisateur dans un groupe | ||
| + | |||
| + | ### Exemple de fichier .CSV | ||
| + | |||
| + | [Téléchargeable ici](http:// | ||
| + | |||
| + | #### Le script d' | ||
| + | |||
| + | ```powershell | ||
| + | Import-Module ActiveDirectory | ||
| + | |||
| + | $List = Import-Csv " | ||
| + | |||
| + | foreach ($User in $List) { | ||
| + | |||
| + | $UserSam = $User.SamAccountName | ||
| + | $Groups = $User.Group | ||
| + | |||
| + | $ADUser = Get-ADUser -Filter " | ||
| + | $ADGroups = Get-ADGroup -Filter * | Select-Object DistinguishedName, | ||
| + | |||
| + | if ($ADUser -eq $null) { | ||
| + | Write-Host " | ||
| + | Continue | ||
| + | } | ||
| + | |||
| + | if ($Groups -eq $null) { | ||
| + | Write-Host " | ||
| + | Continue | ||
| + | } | ||
| + | |||
| + | $ExistingGroups = Get-ADPrincipalGroupMembership $UserSam | Select-Object DistinguishedName, | ||
| + | |||
| + | foreach ($Group in $Groups.Split(';' | ||
| + | |||
| + | if ($ADGroups.SamAccountName -notcontains $Group) { | ||
| + | Write-Host "$Le groupe n' | ||
| + | Continue | ||
| + | } | ||
| + | |||
| + | if ($ExistingGroups.SamAccountName -eq $Group) { | ||
| + | Write-Host " | ||
| + | } | ||
| + | else { | ||
| + | |||
| + | Add-ADGroupMember -Identity $Group -Members $UserSam | ||
| + | Write-Host " | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | ``` | ||
| + | |||
| + | ## Création de la structure des dossiers | ||
| + | |||
| + | *Fonctionnalité :* | ||
| + | |||
| + | - Création de la structure des dossiers | ||
| + | - Suppression de l' | ||
| + | - Suppression des utilisateurs indésirables | ||
| + | - Ajout des groupes et définitions des permissions à partir d'un .csv | ||
| + | |||
| + | ### Exemple de fichier .CSV | ||
| + | |||
| + | [Téléchargeable ici](http:// | ||
| + | |||
| + | #### Le script d' | ||
| + | |||
| + | ```powershell | ||
| + | Set-Location " | ||
| + | write-Host " | ||
| + | |||
| + | |||
| + | $Folders = Import-Csv " | ||
| + | |||
| + | ForEach ($Folder in $Folders) { | ||
| + | |||
| + | if (Test-Path -Path $Folder.Name) { | ||
| + | write-Host "Le dossier existe déjà !" -ForegroundColor Red | ||
| + | } else { | ||
| + | New-Item $Folder.Name -type directory | ||
| + | write-Host " | ||
| + | } | ||
| + |  | ||
| + | echo " | ||
| + | echo ' | ||
| + | |||
| + | write-Host " | ||
| + |  | ||
| + | $acl = Get-ACL -Path $Folder.Name | ||
| + | $acl.SetAccessRuleProtection($True, | ||
| + | Set-Acl -Path $Folder.Name -AclObject $acl | ||
| + | write-Host " | ||
| + | |||
| + | write-Host " | ||
| + | |||
| + | $acl = Get-ACL -Path $Folder.Name | ||
| + | icacls C: | ||
| + | $usersid = New-Object System.Security.Principal.Ntaccount (" | ||
| + | $acl.PurgeAccessRules($usersid) | ||
| + | $acl | Set-Acl -Path $Folder.Name | ||
| + | write-Host " | ||
| + | |||
| + | write-Host " | ||
| + | |||
| + | echo $Folder.Group | ||
| + | echo $Folder.ACL | ||
| + | $acl = Get-Acl -Path $Folder.Name | ||
| + | $AccessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($Folder.Group, | ||
| + | $acl.SetAccessRule($AccessRule) | ||
| + | $acl | Set-Acl -Path $Folder.Name | ||
| + | write-Host " | ||
| + | } | ||
| + | ``` | ||
| ## Mes sources | ## Mes sources | ||
| 1. [https:// | 1. [https:// | ||
| - | 2. Les documentations | + | 2. [https:// | 
| + | 3. [https:// | ||
| + | 4. [https:// | ||
| + | 5. [https:// | ||
| + | 6. [https:// | ||