Skip to content

Instantly share code, notes, and snippets.

@quonic
Last active April 24, 2018 18:46
Show Gist options
  • Select an option

  • Save quonic/c3c696f818ae9be35ccae972e22c0455 to your computer and use it in GitHub Desktop.

Select an option

Save quonic/c3c696f818ae9be35ccae972e22c0455 to your computer and use it in GitHub Desktop.
AD Group assigner
$Users = Get-ADUser
$GroupData = Import-Clixml -Path ".\Groups.clixml"
$GroupsToAdd = $Users | ForEach-Object {
$User = $_
$Company = $GroupData.Companies | Where-Object {$_.Name -eq $User.Company}
$Departments = $Company.Departments | Where-Object {$_.Name -eq $User.Department}
$Departments.Groups
}
# How it would be ran
. .\SaveGroupData.ps1
. .\AssignGroups.ps1
$GroupsToAdd
# Example with out the other scripts to show proof of concept
$GroupData = @{
Companies = @(
[PSCustomObject]@{
Name = "CompanyA"
Departments = @(
[PSCustomObject]@{
Name = "DepartmentA"
Groups = @("Group1", "Group2")
}, [PSCustomObject]@{
Name = "DepartmentB"
Groups = @("Group3", "Group4")
}
, [PSCustomObject]@{
Name = "DepartmentB"
Groups = @("Group1", "Group3")
}
)
},
[PSCustomObject]@{
Name = "CompanyB"
Departments = @(
[PSCustomObject]@{
Name = "DepartmentA"
Groups = @("Group4", "Group8")
}, [PSCustomObject]@{
Name = "DepartmentB"
Groups = @("Group5", "Group9")
}
, [PSCustomObject]@{
Name = "DepartmentB"
Groups = @("Group1", "Group7")
}
)
}
)
}
$Users = [PSCustomObject]@{
Name = "Spyingwind"
Department = "DepartmentA"
Company = "CompanyB"
}
$GroupsToAdd = $Users | ForEach-Object {
$User = $_
$Company = $GroupData.Companies | Where-Object {$_.Name -eq $User.Company}
$Departments = $Company.Departments | Where-Object {$_.Name -eq $User.Department}
$Departments.Groups
}
$Groups = Get-ADGroup
$GroupData = @{
Companies = @(
[PSCustomObject]@{
Name = "CompanyA"
Departments = @(
[PSCustomObject]@{
Name = "DepartmentA"
Groups = $Groups | Where-Object {$_.Name -in @("Group1", "Group2")}
}, [PSCustomObject]@{
Name = "DepartmentB"
Groups = $Groups | Where-Object {$_.Name -in @("Group3", "Group4")}
}
, [PSCustomObject]@{
Name = "DepartmentB"
Groups = $Groups | Where-Object {$_.Name -in @("Group1", "Group3")}
}
)
},
[PSCustomObject]@{
Name = "CompanyB"
Departments = @(
[PSCustomObject]@{
Name = "DepartmentA"
Groups = $Groups | Where-Object {$_.Name -in @("Group4", "Group8")}
}, [PSCustomObject]@{
Name = "DepartmentB"
Groups = $Groups | Where-Object {$_.Name -in @("Group5", "Group9")}
}
, [PSCustomObject]@{
Name = "DepartmentB"
Groups = $Groups | Where-Object {$_.Name -in @("Group1", "Group7")}
}
)
}
)
}
$Data = . .\GetMyGroups.ps1
$Data | Export-Clixml -Path ".\Groups.clixml" -Force
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment