-
Notifications
You must be signed in to change notification settings - Fork 3
/
create_admin_account.ps1
58 lines (48 loc) · 1.98 KB
/
create_admin_account.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<#
.SYNOPSIS
Created this script copy make admin accounts for best practices. It copies the security groups and creates a new AD
Account named admin
.EXAMPLE
create_admin_account.ps1 -user someuser
#>
Param(
[Parameter(mandatory)]
[string]$User
)
function Get-GroupMember($User) {
#gets groups that are not distribution groups
foreach ($group in ((Get-ADUser -Identity $user -Properties memberof).memberof)) {
if ($group.GroupType -match "SecurityEnabled") {
return $group
}
}
}
function Set-NewUser($User) {
#creates new user
$newuser = ($user.substring(0, 4) + "admin" )
$oldaccount = Get-ADUser -Identity $user -Properties *
#Write-Output "User account created... $newuser"
#parses out OU path to set to new users OU
$LikeUN = $oldaccount.DistinguishedName | Out-String
$OU = $LikeUN.Substring($LikeUN.IndexOf("OU="))
#Write-Output "User will be located in $OU"
#sets password
$password = "Strong Password" | ConvertTo-SecureString -AsPlainText -Force
New-ADUser -Name ($($oldaccount).displayname + " (Admin)") -SamAccountName $newuser -AccountPassword $password -UserPrincipalName ($newuser + "@CONTOSO.LOCAL") -GivenName $($oldaccount).givenname -Surname $($oldaccount).Surname `
-DisplayName ($($oldaccount).displayname + " (Admin)") -Path $OU -ChangePasswordAtLogon $true -Enabled $true
$newuser
#sets email address for new admin account to admins primary account
Set-ADUser -Identity $newuser -add @{mail = (Get-ADUser -Identity $user -Properties mail).mail }
Set-ADUser -Identity $newuser -EmailAddress (Get-ADUser -Identity $user -Properties mail).mail
}
function Add-Groups {
foreach ($addgroup in $groupmember.samaccountname) {
Write-Output "added to $addgroup"
Add-ADGroupMember $addgroup -Members $brandnewuser
}
}
Get-GroupMember
Set-Newuser
Start-Sleep -Sleep 5
Write-Output "Waiting for AD to catch up with the script"
Add-Groups