Generate an AD environment based of a JSON configured structure

main
John Hammond 2022-05-19 22:35:13 -07:00
parent 3afd170b06
commit ff9c90c1ad
3 changed files with 90 additions and 0 deletions

27
code/ad_schema.json Normal file
View File

@ -0,0 +1,27 @@
{
"domain": "xyz.com",
"groups" : [
{
"name": "Employees"
}
],
"users": [
{
"name": "Alice Lice",
"password":"P@ssw0rd789",
"groups": [
"Employees"
]
},
{
"name": "Bob Ob",
"password":"P@ssw0rdABC",
"groups": [
"Employees"
]
}
]
}

51
code/gen_ad.ps1 Normal file
View File

@ -0,0 +1,51 @@
param( [Parameter(Mandatory=$true)] $JSONFile )
function CreateADGroup(){
param( [Parameter(Mandatory=$true)] $groupObject )
$name = $groupObject.name
New-ADGroup -name $name -GroupScope Global
}
function CreateADUser(){
param( [Parameter(Mandatory=$true)] $userObject )
# Pull out the name from the JSON object
$name = $userObject.name
$password = $userObject.password
# Generate a "first initial, last name" structure for username
$firstname, $lastname = $name.Split(" ")
$username = ($firstname[0] + $lastname).ToLower()
$samAccountName = $username
$principalname = $username
# Actually create the AD user object
New-ADUser -Name "$name" -GivenName $firstname -Surname $lastname -SamAccountName $SamAccountName -UserPrincipalName $principalname@$Global:Domain -AccountPassword (ConvertTo-SecureString $password -AsPlainText -Force) -PassThru | Enable-ADAccount
# Add the user to its appropriate group
foreach($group_name in $userObject.groups) {
try {
Get-ADGroup -Identity "$group_name"
Add-ADGroupMember -Identity $group_name -Members $username
}
catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException]
{
Write-Warning "User $name NOT added to group $group_name because it does not exist"
}
}
}
$json = ( Get-Content $JSONFile | ConvertFrom-JSON)
$Global:Domain = $json.domain
foreach ( $group in $json.groups ){
CreateADGroup $group
}
foreach ( $user in $json.users ){
CreateADUser $user
}

View File

@ -11,3 +11,15 @@
Install-WindowsFeature AD-Domain-Services -IncludeManagementTools
```
```
Get-NetIPAddress
```
# Joining the Workstation to the domain
```
Add-Computer -Domainname xyz.com -Credential xyz\Administrator -Force -Restart
```