-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddToGroupUser.ps1
More file actions
144 lines (111 loc) · 5.04 KB
/
Copy pathAddToGroupUser.ps1
File metadata and controls
144 lines (111 loc) · 5.04 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<#
.SYNOPSIS
Add user accounts from a CSV file to an Active Directory group
.DESCRIPTION
Script is used for automating the addition of users to groups in Active Directory.
.PARAMETER Name
-CSVFile
Specifies the location of the Csv file.
Required? true
Position? 0
Default value
Accept pipeline input? false
Accept wildcard characters?
.PARAMETER Extension
Specifies the extension. "Csv" is the default.
Required? True
Position? 1
Default value
Accept pipeline input? false
Accept wildcard characters?
.EXAMPLE
C:\PS> AddToGroupUser.ps1 -Csvfile "C:\temp\UserAccounts.csv"
.COPYRIGHT
MIT License, feel free to distribute and use as you like, please leave author information.
.LINK
BLOG: http://www.hcconsult.dk
Twitter: @dk_hcandersen
.DISCLAIMER
This script is provided AS-IS, with no warranty - Use at own risk.
#>
Param(
[Parameter(Mandatory=$True,
Position=0,
HelpMessage="Enter path and filename of CSV file"
)]
[String]$csvfile
)
Function AddToGroup{
$GroupExists = Get-ADGroup -Filter { Name -eq $Group }
If($GroupExists)
{
Add-ADGroupMember -Identity $Group -Members $Accountname
Write-host "Added user $accountname to $group"
}
Else
{
write-output "Group $Group doesn't exist. User $Accountname hasn't been added."
}
}
Function EmptyGroup{
$GroupExists = Get-ADGroup -Filter { Name -eq $GroupToEmpty }
If($GroupExists)
{
Get-ADGroupMember -Identity $GroupToEmpty | ForEach-Object {Remove-ADGroupMember $GroupToEmpty $_ -Confirm:$false}
Write-host "Removed users from $GroupToEmpty"
}
Else
{
write-output "Group $GroupToEmpty doesn't exist"
Send-MailMessage -From "$MailFrom" -To "$MailTo" -Subject "Add user to group: Error emptying group $GroupToEmpty" -SmtpServer $SmtpServer -UseSsl
stop-transcript;
exit
}
}
#Parameters declaration
$csvfirstline = Import-csv "$csvfile" -Header 'Account','Group' -Delimiter ";" | Select-Object -Skip 1 -First 1
$GroupToEmpty = $csvfirstline.Group
$LogFile = "\\serv\scripts\AD\GPO\Logs\AddToGroupUser_log_$GroupToEmpty.txt"
$SmtpServer = "serv.local"
$MailFrom = "ad@xyz.dk"
$MailTo = "hca@xyz.com"
$MailSubject = "Add users to AD group report"
# Import active directory module for running AD cmdlets
Import-Module activedirectory
#Starting transcript
start-transcript -Path $LogFile
If(!(Test-Path $csvfile))
{
write-output "Error: CSV file doesn't exist. Executed on $((get-date).DateTime)"
#Stopping transcript
stop-transcript;
Exit
}
Try {
#Imports the Csv file
$csv = Import-Csv "$csvfile" -Header 'Account','Group' -Delimiter ";" | Select-Object -Skip 1
#Empty group before adding users
$csvfirstline = Import-csv "$csvfile" -Header 'Account','Group' -Delimiter ";" | Select-Object -Skip 1 -First 1
$GroupToEmpty = $csvfirstline.Group
EmptyGroup
#Loops through every user
ForEach($account in $csv)
{
#Read user data from each field in each row and assign the data to a variable as below
$Accountname = $account."Account"
$Group = $account."Group"
AddToGroup
}
#Email is sent with information about users that have been added
write-output "Users has been added to group $((get-date).DateTime)"
Send-MailMessage -From "$MailFrom" -To "$MailTo" -Subject "$MailSubject" -Body "Add users to group: The following users in the attached file has been added to a group" -Attachments "$csvfile" -SmtpServer $SmtpServer -UseSsl
Remove-Item -Path "$csvfile" -Confirm:$false -Verbose
}
#Catch if add users to group fails. Logged to file and email sent.
catch {
write-output "Error: Executed the add users to group script on $((get-date).DateTime) with the error $_"
Send-MailMessage -From "$MailFrom" -To "$MailTo" -Subject 'Add users to group: Errror running add users to group script' -Body "Error: Executed the add users to group script on $((get-date).DateTime) with the error $_" -SmtpServer $SmtpServer -UseSsl
}
#Stopping transcript
stop-transcript;
exit