Skip to content

Instantly share code, notes, and snippets.

@Lucho00Cuba
Created December 3, 2022 23:38
Show Gist options
  • Select an option

  • Save Lucho00Cuba/3eecb7e4c50c1f796fd2456f7d0636ef to your computer and use it in GitHub Desktop.

Select an option

Save Lucho00Cuba/3eecb7e4c50c1f796fd2456f7d0636ef to your computer and use it in GitHub Desktop.
IFP AD
# IFP AD
function add_user($user){
Write-Host "Action:" $user.Action
Write-Host "Unit:" $user.Unit
Write-Host "Group:" $user.Group
$user."Name" = $user.FirtsName + " " + $user.LastName
$user."Sam" = $user.FirtsName.ToLower() + "." + $user.LastName.ToLower()
Write-Host "User:" $user.Name
Write-Host "Sam:" $user.Sam
if ( $user.Action -like "add" ) {
try {
$path_user = "ou=" + $user.Unit + "," + $DC
$user."Email" = $user.FirtsName + "@" + $DOMAIN
Write-Host "Email:" $user.Email
New-ADUser -Name $user.Name -SamAccountName $user.Sam -AccountPassword (ConvertTo-SecureString "p@ssw0rd" -AsPlainText -Force) -ChangePasswordAtLogon $false -Enabled $true -Path $path_user -UserPrincipalName $user.Email -DisplayName $user.Name -Initials $user.Initials -EmailAddress $user.Email -GivenName $user.FirtsName -Surname $user.LastName -City $user.City -Company $user.Company -Office $user.Office
Add-ADGroupMember -Identity $user.Group -Members $user.Sam
} catch {
#Write-Host "An error occurred:"
#Write-Host $_.Exception
Write-Host "$PSItem.Exception.Message - User:" $user.Email
}
} elseif ( $user.Action -like "del" ) {
try {
$identity_user = "cn=" + $user.Name + "," + "ou=" + $user.Unit + "," + $DC
Write-Host $identity_user
Remove-ADUser -Identity $identity_user
} catch {
#Write-Host "An error occurred:"
#Write-Host $_.Exception
Write-Host "$PSItem.Exception.Message - User:" $user.Sam
}
} else {
Write-Host "Action not found" $user.Action
}
}
function add_group($group){
Write-Host "Action:" $group.Action
Write-Host "Unit:" $group.Unit
Write-Host "Group:" $group.Name
if ( $group.Action -like "add" ) {
try {
$path_group = "ou=" + $group.Unit + "," + $DC
New-ADGroup -Name $group.Name -Path $path_group -GroupScope Global -GroupCategory Security
} catch {
Write-Host "$PSItem.Exception.Message - Group:" $group.Name
}
} elseif ( $group.Action -like "del" ) {
try {
$identity_group = "cn=" + $group.Name + "," + "ou=" + $group.Unit + "," + $DC
Write-Host $identity_group
Remove-ADGroup -Identity $identity_group
} catch {
Write-Host "$PSItem.Exception.Message - Group:" $group.Name
}
} else {
Write-Host "Action not found" $group.Action
}
}
function add_unit($unit) {
Write-Host "Action:" $unit.Action
Write-Host "OrganizationUnit:" $unit.Name
if ( $unit.Action -like "add" ) {
try {
New-ADOrganizationalUnit -Name $unit.Name -Path $DC -ProtectedFromAccidentalDeletion $false
} catch {
Write-Host "$PSItem.Exception.Message - OrganizationUnit:" $unit.Name
}
} elseif ( $unit.Action -like "del" ) {
try {
$identity_unit = "ou=" + $unit.Name + "," + $DC
Write-Host $identity_unit
Remove-ADOrganizationalUnit -Identity $identity_unit
} catch {
Write-Host "$PSItem.Exception.Message - OrganizationUnit:" $unit.Name
}
} else {
Write-Host "Action not found" $unit.Action
}
}
$DOMAIN = "mota.ifp.es"
$DC = "dc=mota,dc=ifp,dc=es"
# constants
$DELIM = ','
#$CSV_F = 'C:\Users\Administrator\Desktop\Data\data.csv' # ALL
#$CSV_F = 'C:\Users\Administrator\Desktop\Data\unit.csv' # Units
#$CSV_F = 'C:\Users\Administrator\Desktop\Data\group.csv' # Groups
$CSV_F = 'C:\Users\Administrator\Desktop\Data\user.csv' # Users
# parse keys
$keys = (gc "${CSV_F}" -TotalCount 1).Split($DELIM)
$csv = Import-CSV "${CSV_F}"
$data = @()
# hashtables
foreach ($r in $csv) {
$tmp_h = @{}
# Create hash of key-value pairs.
foreach($k in $keys) {
$tmp_h[$k] = $r.($k)
}
# Add hash to array of hashes.
$data += $tmp_h
}
# Display data
$data | ForEach {[PSCustomObject]$_} | Format-Table -AutoSize
foreach($entry in $data){
#Write-Host $entry.Type
if ( $entry.Type -like "unit" ) {
add_unit($entry)
Write-Host "`n"
} elseif ( $entry.Type -like "group" ) {
add_group($entry)
Write-Host "`n"
} elseif ( $entry.Type -like "user" ) {
add_user($entry)
Write-Host "`n"
} else {
Write-Host "Type not found" $entry.Type
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment