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 08:30] – DEROUET Valentin | wiki:windows:scripting:adpowershell [2022/11/21 16: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. | ||
| - | ## Lister les utilisateurs existant dans une OU | + | ### Vérifier un fichier .CSV |
| ```powershell | ```powershell | ||
| - | Get-ADUser -Filter * | + | Import-Csv C: |
| - | -SearchBase"OU=Megaproduction, | + | |
| - | -Properties Name, | + | |
| ``` | ``` | ||
| + | ## Créer des OU et des sous-OU | ||
| + | ### Exemple de fichier .CSV | ||
| + | [Téléchargeable ici](http:// | ||
| + | |||
| + | #### Le script de création des OU : | ||
| + | |||
| + | ```powershell | ||
| + | Import-Module activedirectory | ||
| + | |||
| + | $ADOU = Import-csv ' | ||
| + | |||
| + | foreach ($ou in $ADou) { | ||
| + | |||
| + | $name = $ou.name | ||
| + | $path = $ou.path | ||
| + | |||
| + | New-ADOrganizationalUnit ` | ||
| + | -Name $name ` | ||
| + | -path $path ` | ||
| + | |||
| + | write-Host "OU créée." | ||
| + | } | ||
| + | ``` | ||
| + | |||
| + | ## Powershell sur les utilisateurs | ||
| + | |||
| + | ### Lister les utilisateurs existant dans une OU | ||
| + | ```powershell | ||
| + | Get-ADUser | ||
| + | -Properties Name, | ||
| + | Select-Object Name, | ||
| + | ``` | ||
| + | |||
| + | {{ : | ||
| + | |||
| + | ### Création d' | ||
| + | |||
| + | ### Exemple de fichier .CSV | ||
| + | |||
| + | [Téléchargeable ici](http:// | ||
| + | |||
| + | #### Le script de création utilisateurs : | ||
| + | |||
| + | ```powershell | ||
| + | Import-Module ActiveDirectory | ||
| + | | ||
| + | $ADUsers = Import-Csv C: | ||
| + | $UPN = " | ||
| + | |||
| + | foreach ($User in $ADUsers) { | ||
| + | |||
| + | $username = $User.username | ||
| + | $password = $User.password | ||
| + | $firstname = $User.firstname | ||
| + | $lastname = $User.lastname | ||
| + | $initials = $User.initials | ||
| + | $OU = $User.ou | ||
| + | $email = $User.email | ||
| + | $streetaddress = $User.streetaddress | ||
| + | $city = $User.city | ||
| + | $zipcode = $User.zipcode | ||
| + | $state = $User.state | ||
| + | $country = $User.country | ||
| + | $telephone = $User.telephone | ||
| + | $jobtitle = $User.jobtitle | ||
| + | $company = $User.company | ||
| + | $department = $User.department | ||
| + | |||
| + | if (Get-ADUser -F { SamAccountName -eq $username }) { | ||
| + | Write-Warning " | ||
| + | } | ||
| + | else { | ||
| + | New-ADUser ` | ||
| + | -SamAccountName $username ` | ||
| + | -UserPrincipalName " | ||
| + | -Name " | ||
| + | -GivenName $firstname ` | ||
| + | -Surname $lastname ` | ||
| + | -Initials $initials ` | ||
| + | -Enabled $True ` | ||
| + | -DisplayName " | ||
| + | -Path $OU ` | ||
| + | -City $city ` | ||
| + | -PostalCode $zipcode ` | ||
| + | -Country $country ` | ||
| + | -Company $company ` | ||
| + | -State $state ` | ||
| + | -StreetAddress $streetaddress ` | ||
| + | -OfficePhone $telephone ` | ||
| + | -EmailAddress $email ` | ||
| + | -Title $jobtitle ` | ||
| + | -Department $department ` | ||
| + | -AccountPassword (ConvertTo-secureString $password -AsPlainText -Force) -ChangePasswordAtLogon $True | ||
| + | Write-Host " | ||
| + | } | ||
| + | } | ||
| + | |||
| + | Read-Host -Prompt " | ||
| + | ``` | ||
| + | |||
| + | ## Création des groupes | ||
| + | |||
| + | ### Exemple de fichier .CSV | ||
| + | |||
| + | [Téléchargeable ici](http:// | ||
| + | |||
| + | #### Le script de création des groupes : | ||
| + | |||
| + | ```powershell | ||
| + | Import-Module ActiveDirectory | ||
| + | |||
| + | $groups = Import-Csv ‘C: | ||
| + | |||
| + | foreach ($group in $groups) { | ||
| + | |||
| + | $groupProps = @{ | ||
| + | |||
| + | Name = $group.name | ||
| + | Path = $group.path | ||
| + | GroupScope | ||
| + | GroupCategory = $group.category | ||
| + | Description | ||
| + | |||
| + | } | ||
| + | |||
| + | New-ADGroup @groupProps | ||
| + | Write-Host " | ||
| + | | ||
| + | } | ||
| + | Read-Host -Prompt " | ||
| + | ``` | ||
| + | |||
| + | ## 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 | ||
| + | |||
| + | ### 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 | ||
| + | |||
| + | 1. [https:// | ||
| + | 2. [https:// | ||
| + | 3. [https:// | ||
| + | 4. [https:// | ||
| + | 5. [https:// | ||
| + | 6. [https:// | ||