forked from joerod/powershell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_admin_account.ps1
65 lines (50 loc) · 1.89 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
59
60
61
62
63
64
<#
.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(Position=0,mandatory=$true)]
[string]$user
)
function get-groupmember{
#gets groups that are not distribution groups
$Groups = Get-ADUser -Identity $user -Properties * |select -expand memberof
foreach($group in $groups){
$group = Get-Group $group
if ($group.GroupType -like "*SecurityEnabled*"){
Write-Output $group
}
}
}
function create-newuser{
#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 = "Welcome1" |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 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
}
$groupmember = get-groupmember
function Add-Groups{
foreach($addgroup in $groupmember.samaccountname){
write-output "added to $addgroup"
Add-ADGroupMember $addgroup -Members $brandnewuser
}
}
$brandnewuser = create-newuser
Write-Output "Waiting for AD to catch up with the script"
Start-Sleep -s 5
add-groups