-
Notifications
You must be signed in to change notification settings - Fork 0
/
New-ADUGGroup.ps1
86 lines (77 loc) · 1.99 KB
/
New-ADUGGroup.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<#
.Synopsis
Takes information from NewUGGroups.csv, and creates new security groups with the specified users
#>
param(
[Switch]$Test,
[Switch]$Live
)
$StartTime = Get-Date
If (($Test) -AND (!$Live)) {
$OU = "OU=users,DC=testdomain,DC=com"
$Domain = "testdomain.com"
$AdminUser = "TESTDOMAIN\ADMINACCOUNT"
}
ElseIf ((!$Test) -AND ($Live)) {
$OU = "OU=users,DC=domain,DC=com"
$Domain = "domain.com"
$AdminUser = "DOMAIN\ADMINACCOUNT"
}
ElseIf ((!$Test) -AND (!$Live)) {
Write-Error "No parameters specified. -Live or -Test required."
Exit
}
Else {
Write-Error "Please select only 1 switch. Either -Live or -Test."
Exit
}
$File = "NewUGGroups.csv"
$Check = Test-Path $File
If ($Check) {
$UGGroups = Import-Csv -Path $File
}
Else {
Write-Output "`n$File does not exist. Exiting."
Exit
}
Write-Output "`nSetting variables..."
Write-Output "`nRetrieving credentials..."
Try {
$SecureCreds = Get-Credential -Credential "$AdminUser"
}
Catch {
Write-Error "`nCredentials required to proceed. Exiting.`n"
Exit
}
Foreach ($UGGroup in $UGGroups) {
If ($Test) {
$Displayname = "TEST" + $UGGroup.group.Trim()
}
Else {
$Displayname = $UGGroup.group.Trim()
}
$ManagedBy = $UGGroup.ManagedBy
$Description = $UGGroup.description
$Members = $UGGroup.Members.split(";")
$Opts = @{
Credential = $SecureCreds
Server = $Domain
Name = $DisplayName
DisplayName = $DisplayName
GroupCategory = "Security"
GroupScope = "Universal"
Description = $Description
Path = $OU
ManagedBy = $ManagedBy
}
New-ADGroup @Opts
$Opts = @{
Credential = $SecureCreds
Server = $Domain
Identity = $DisplayName
}
$Members | ForEach-Object { Add-ADGroupMember @Opts -Members $_ }
}
$Runtime = (Get-Date) - $StartTime
$Runtime = $Runtime -f ("HH:mm:ss")
Write-Output "`nTotal Runtime:"$Runtime"`n"