69 lines
2.4 KiB
PowerShell
69 lines
2.4 KiB
PowerShell
<#
|
|
.Exemple
|
|
|
|
#>
|
|
|
|
cls
|
|
|
|
#throw "This is not a robus script"
|
|
$location = Get-Location
|
|
Set-Location C:\Tools
|
|
|
|
Import-Module ActiveDirectory
|
|
|
|
$FichierAdmin = "Groupes-Administrateur.csv"
|
|
$FichierStandard = "Groupes-Standard.csv"
|
|
|
|
$GroupAdmins = Import-Csv -Path $FichierAdmin -Delimiter ";"
|
|
$GroupStandards = Import-Csv -Path $FichierStandard -Delimiter ";"
|
|
|
|
$dNC = (Get-ADRootDSE).defaultNamingContext
|
|
|
|
Foreach ($group in $GroupAdmins) {
|
|
$groupName = $group.Name
|
|
$groupOUPrefix = $group.OU
|
|
$destOU = $group.OU + "," + $dNC
|
|
$groupDN = "CN=" + $groupName + "," + $destOU
|
|
|
|
$checkForGroup = Get-ADGroup -filter 'Name -eq $groupName' -ErrorAction SilentlyContinue
|
|
|
|
If ($checkForGroup.count -eq 0 ) {
|
|
Write-Verbose "Creating new Group '$($Group.samAccountName)' under '$destOU'"
|
|
|
|
New-ADGroup -Name $Group.Name -SamAccountName $Group.samAccountName -GroupCategory $Group.GroupCategory -GroupScope $Group.GroupScope -DisplayName $Group.DisplayName -Path $destOU -Description $Group.Description
|
|
|
|
If ($Group.Membership -ne "") {
|
|
Write-Verbose "Adding Group Membership '$($Group.Membership)' for group '$($Group.samAccountName)'"
|
|
Add-ADPrincipalGroupMembership -Identity $Group.samAccountName -MemberOf $Group.Membership
|
|
}
|
|
$error.Clear()
|
|
}
|
|
Else {
|
|
Write-Verbose "Group '$($Group.samAccountName)'already exists."
|
|
}
|
|
}
|
|
|
|
Foreach ($group in $GroupStandards) {
|
|
$groupName = $group.Name
|
|
$groupOUPrefix = $group.OU
|
|
$destOU = $group.OU + "," + $dNC
|
|
$groupDN = "CN=" + $groupName + "," + $destOU
|
|
|
|
$checkForGroup = Get-ADGroup -filter 'Name -eq $groupName' -ErrorAction SilentlyContinue
|
|
|
|
If ($checkForGroup.count -eq 0 ) {
|
|
Write-host "Creating new Group '$($Group.samAccountName)' under '$destOU'"
|
|
|
|
New-ADGroup -Name $Group.Name -SamAccountName $Group.samAccountName -GroupCategory $Group.GroupCategory -GroupScope $Group.GroupScope -DisplayName $Group.DisplayName -Path $destOU -Description $Group.Description
|
|
|
|
If ($Group.Membership -ne "") {
|
|
Write-host "Adding Group Membership '$($Group.Membership)' for group '$($Group.samAccountName)'"
|
|
Add-ADPrincipalGroupMembership -Identity $Group.samAccountName -MemberOf $Group.Membership
|
|
}
|
|
$error.Clear()
|
|
}
|
|
Else {
|
|
Write-host "Group '$($Group.samAccountName)'already exists."
|
|
}
|
|
}
|