-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSPLAScriptv1.ps1
More file actions
304 lines (234 loc) · 13.5 KB
/
Copy pathSPLAScriptv1.ps1
File metadata and controls
304 lines (234 loc) · 13.5 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#For exporting or saving, this gets the directory from which the script is run.
$MyDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$Month = Get-Date -UFormat %B
$logfile = "$myDir\logs\logfile.log"
$confFile = "$myDir\company.conf"
$companyName = Get-Content $confFile | Select-Object -Index 0
$auditType = Get-Content $confFile | Select-Object -Index 1
$companyContact = Get-Content $confFile | Select-Object -Index 2
$companyContactEmail = Get-Content $confFile | Select-Object -Index 3
$now = Get-Date -f yyyy-MM-dd-hh-mm-ss
If ($auditType -match 2) {
$rdsGroup = @()
$rdsGroup = Get-Content $confFile | Select-Object -Index 5
$rdsGroup = $rdsGroup -split ","
Add-Content $logfile "The RDS Group to search is $rdsGroup"
}
If ($auditType -match 7) {
$sslvpnGroup = Get-Content $confFile | Select-Object -Index 7
Add-Content $logfile "The SSLVPN group to search is $sslvpnGroup"
}
If ($auditType -match 5) {
$officeGroup = Get-Content $confFile | Select-Object -Index 8
Add-Content $logfile "The Office group to search is $officeGroup"
}
If ($auditType -match 6) {
$BlaskGuardGroup = Get-Content $confFile | Select-Object -Index 9
Add-Content $logfile "The BlaskGuard group to search is $BlaskGuardGroup"
}
$filterednames = @("dmarc", "mailmonitor", "mimecast", "guest", "LDAP", "vmware", "dss", "opendns", "sp admin", "dcg", "qbdataservice", "sql", "st_bernard", "hosted", "ldapadmin", "spadmin", "test", "noc", "st. bernard", "st bernard", "managed care", "bbadmin", "besadmin", "compliance", "discovery", "rmmscan", "healthmailbox", "sharepoint", "windows sbs", "qbdata", "noc_helpdesk", "appassure", "scanner", "ftp", "app assure", "aspnet", "Dependable Computer Guys", "efax", "exchange", "INSTALR", "IUSR", "IWAM", "Quick Books")
$perEnvFilteredNames = Get-Content $confFile | Select-Object -Index 4
# https://www.reddit.com/r/PowerShell/comments/2h5elx/split_string_by_spaces_unless_in_quotes/
# splitting so that we can include quotes in per-env filtered names
# replace at the end to take the quotes out of the final product as that would then go on to mess with the regex
$perEnvFilteredNames = $perEnvFilteredNames -Split ' +(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)' -replace ("`"", "")
$filterednames = $filterednames += $perEnvFilteredNames
$regex = "(" + ($filterednames -join "|") + ")"
Add-Content $logfile "Filtering out the following names: $filterednames"
$PSEmailServer = "host-exch90.dcgla.com"
$SMTPPort = 2525
$SMTPUsername = "scriptsender@dcgla.net"
$EncryptedPasswordFile = "$mydir\scriptsender@dcgla.net.securestring"
$SecureStringPassword = Get-Content -Path $EncryptedPasswordFile | ConvertTo-SecureString
$EmailCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $SMTPUsername, $SecureStringPassword
$MailTo = "monitoring@dcgla.com"
$MailFrom = "scriptsender@dcgla.net"
$mailAttachments = @()
$extraMailBodyInfo = Get-Content $confFile | Select-Object -Index 6
If ($auditType -match 3) {
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
}
Get-Date >> $logfile
If ($auditType -match 1) {
Add-Content $logfile 'Beginning search of AD users'
$usernamesraw = (Get-ADUser -filter * | Where-Object { ($_.enabled -eq "True") }).name
$usernamesfiltered = $usernamesraw | Where-Object { $_ -notmatch $regex }
Add-Content $logfile 'Names of AD users: '
$usernamesfiltered >> $logfile
Add-Content $logfile 'Count of AD users: '
$usernamesfiltered.count >> $logfile
$usernamesfilteredCount = $usernamesfiltered.count
$usernamesfiltered | Sort-Object > "$MyDir\logs\$now`ADUsers.txt"
$adUsersAttachment = "$MyDir\logs\$now`ADUsers.txt"
}
If ($auditType -match 2) {
If ((Get-WmiObject -Class Win32_ComputerSystem).PartOfDomain -eq "True") {
Add-Content $logfile "Server is part of a domain"
Add-Content $logfile 'Beginning search of RDS Users.'
foreach ($groupname in $rdsGroup) {
$rdsUsersRaw = (Get-ADGroupMember -Identity $groupname -Recursive | Get-ADUser | Where-Object { ($_.enabled -eq "True") }).name
$rdsUsersFiltered = $rdsUsersRaw | Where-Object { $_ -notmatch $regex }
Add-Content $logfile 'Names of RDS Users:'
$rdsUsersFiltered >> $logfile
Add-Content $logfile 'Count of RDS Users:'
# This count is additive because some clients have multiple RDS groups. The next line adds each iteration of a count to the file.
$rdsUsersFiltered | Sort-Object >> "$MyDir\logs\$now`RemoteDesktopUsers.txt"
$rdsUsersAttachment = "$MyDir\logs\$now`RemoteDesktopUsers.txt"
}
$rdsUsersFiltered = Get-Content $rdsUsersAttachment | Sort-Object -Unique
$rdsUsersFiltered > $rdsUsersAttachment
}
Else {
Add-Content $logfile "Server is part of a workgroup"
Add-Content $logfile "Beginning search for RDS Users."
$rdsUsersRaw = (Get-LocalUser | Where-Object { ($_.enabled -eq "True") }).name
$rdsUsersFiltered = $rdsUsersRaw | Where-Object { $_ -notmatch $regex }
Add-Content $logfile 'Names of RDS Users:'
$rdsUsersFiltered >> $logfile
Add-Content $logfile 'Count of RDS Users:'
$rdsUsersFiltered.count >> $logfile
$rdsUsersFilteredCount = $rdsUsersFiltered.count
# This count is additive because some clients have multiple RDS groups. The next line adds each iteration of a count to the file.
$rdsUsersFiltered | Sort-Object >> "$MyDir\logs\$now`RemoteDesktopUsers.txt"
$rdsUsersAttachment = "$MyDir\logs\$now`RemoteDesktopUsers.txt"
}
$rdsUsersFilteredCount = 0
Get-Content $rdsUsersAttachment | ForEach-Object { $rdsUsersFilteredCount++ }
}
If ($auditType -match 3) {
Add-Content $logfile 'Beginning search of Exchange mailboxes.'
$mailAccountsRaw = ((Get-Mailbox -ResultSize Unlimited -WarningAction SilentlyContinue).emailaddresses | Where-Object PrefixString -CEQ SMTP | Select-Object SmtpAddress)
$mailAccountsFiltered = $mailAccountsRaw | Where-Object { $_ -notmatch $regex }
Add-Content $logfile 'Names of Exchange mailboxes:'
$mailAccountsFiltered >> $logfile
Add-Content $logfile 'Count of Exchange mailboxes:'
$mailAccountsFiltered.count >> $logfile
$mailAccountsFilteredCount = $mailAccountsFiltered.Count
$mailAccountsFiltered | Sort-Object > "$MyDir\logs\$now`ExchangeUsers.txt"
$exchangeUsersAttachment = "$MyDir\logs\$now`ExchangeUsers.txt"
}
If ($auditType -match 5) {
Add-Content $logfile "Beginning search of Office users."
If ((Get-WmiObject -Class Win32_ComputerSystem).PartOfDomain -eq "True") {
Add-Content $logfile "Server is part of a domain"
$officeUsersRaw = (Get-ADGroupMember -Identity $officeGroup | Get-ADUser | Where-Object { ($_.enabled -eq "True") }).name
$officeUsersFiltered = $officeUsersRaw | Where-Object { $_ -notmatch $regex }
Add-Content $logfile 'Names of Office Users:'
$officeUsersFiltered >> $logfile
Add-Content $logfile 'Count of Office Users:'
$officeUsersFiltered.count >> $logfile
$officeUsersFilteredCount = $officeUsersFiltered.count
$officeUsersFiltered | Sort-Object > "$MyDir\logs\$now`OfficeUsers.txt"
$officeUsersAttachment = "$MyDir\logs\$now`OfficeUsers.txt"
}
Else {
Add-Content $logfile "Server is part of a workgroup"
Add-Content $logfile "Beginning search for Office Users."
$officeUsersRaw = (Get-LocalUser | Where-Object { ($_.enabled -eq "True") }).name
$officeUsersFiltered = $officeUsersRaw | Where-Object { $_ -notmatch $regex }
Add-Content $logfile 'Names of Office Users:'
$officeUsersFiltered >> $logfile
Add-Content $logfile 'Count of Office Users:'
$officeUsersFiltered.count >> $logfile
$officeUsersFilteredCount = $officeUsersFiltered.count
$officeUsersFiltered | Sort-Object > "$MyDir\logs\$now`OfficeUsers.txt"
$officeUsersAttachment = "$MyDir\logs\$now`OfficeUsers.txt"
}
}
If ($auditType -match 6) {
Add-Content $logfile 'Beginning search of Blaskguard Users.'
If ((Get-WmiObject -Class Win32_ComputerSystem).PartOfDomain -eq "True") {
Add-Content $logfile "Server is part of a domain"
$BlaskGuardUsersRaw = (Get-ADGroupMember -Identity $BlaskGuardGroup | Get-ADUser | Where-Object { ($_.enabled -eq "True") }).name
$BlaskGuardUsersFiltered = $BlaskGuardUsersRaw | Where-Object { $_ -notmatch $regex }
Add-Content $logfile 'Names of BlaskGuard Users:'
$BlaskGuardUsersFiltered >> $logfile
Add-Content $logfile 'Count of BlaskGuard Users:'
$BlaskGuardUsersFiltered.count >> $logfile
$BlaskGuardUsersFilteredCount = $BlaskGuardUsersFiltered.count
$BlaskGuardUsersFiltered | Sort-Object > "$MyDir\logs\$now`BlaskGuardUsers.txt"
$BlaskGuardUsersAttachment = "$MyDir\logs\$now`BlaskGuardUsers.txt"
}
Else {
Add-Content $logfile "Server is part of a workgroup"
Add-Content $logfile "Beginning search for BlaskGuard Users."
$BlaskGuardUsersRaw = (Get-LocalUser | Where-Object { ($_.enabled -eq "True") }).name
$BlaskGuardUsersFiltered = $BlaskGuardUsersRaw | Where-Object { $_ -notmatch $regex }
Add-Content $logfile 'Names of BlaskGuard Users:'
$BlaskGuardUsersFiltered >> $logfile
Add-Content $logfile 'Count of BlaskGuard Users:'
$BlaskGuardUsersFiltered.count >> $logfile
$BlaskGuardUsersFilteredCount = $BlaskGuardUsersFiltered.count
$BlaskGuardUsersFiltered | Sort-Object > "$MyDir\logs\$now`BlaskGuardUsers.txt"
$BlaskGuardUsersAttachment = "$MyDir\logs\$now`BlaskGuardUsers.txt"
}
}
If ($auditType -match 7) {
Add-Content $logfile 'Beginning search of SSL VPN Users.'
$sslvpnUsersRaw = (Get-ADGroupMember -Identity $sslvpnGroup | Get-ADUser | Where-Object { ($_.enabled -eq "True") }).name
$sslvpnUsersFiltered = $sslvpnUsersRaw | Where-Object { $_ -notmatch $regex }
Add-Content $logfile 'Names of SSL VPN Users:'
$sslvpnUsersFiltered >> $logfile
Add-Content $logfile 'Count of SSL VPN Users:'
$sslvpnUsersFiltered.count >> $logfile
$sslvpnUsersFilteredCount = $sslvpnUsersFiltered.count
$sslvpnUsersFiltered | Sort-Object > "$MyDir\logs\$now`SSLVPNUsers.txt"
$sslvpnUsersAttachment = "$MyDir\logs\$now`SSLVPNUsers.txt"
}
$MailSubject = "$companyContact, Please review $companyName's DCG PrivateCLOUD SPLA counts before $Month 15th"
$MailBody = "DCG strives to maintain an accurate active user list, as it pertains to your PrivateCLOUD server SPLA licensing counts on your server(s). Attached is your current user count related Remote Desktop Services, MS Office, and SQL services on your PrivateCLOUD server. This will be referenced in your upcoming Monthly Services invoice that will be emailed to you on the 15th of this month.
"
If ($auditType -match 1) {
$mailBody = $mailBody += "Current Active Directory Users: $usernamesfilteredcount
"
$mailAttachments = $mailAttachments += $adUsersAttachment
}
If ($auditType -match 2) {
$MailBody = $MailBody += "Current Remote Desktop Users: $rdsUsersFilteredcount
"
$mailAttachments = $mailAttachments += $rdsUsersAttachment
}
If ($auditType -match 3) {
If ((Get-Content $confFile | Select-Object -Index 10) -match '^\d+$') {
[Int]$exchangePlusUsersCount = Get-Content $confFile | Select-Object -Index 10
If ($exchangePlusUsersCount -ge $mailAccountsFilteredCount) {
$MailBody = $MailBody += "Current Exchange Plus Users: $mailAccountsFilteredCount
"
}
Else {
$MailBody = $MailBody += "Current Exchange Users: $($mailAccountsFilteredCount - $exchangePlusUsersCount)
Current Exchange Plus Users: $exchangePlusUsersCount
"
}
}
Else {
$MailBody = $MailBody += "Current Exchange Users: $mailAccountsFilteredcount
"
}
$mailAttachments = $mailAttachments += $exchangeUsersAttachment
}
If ($auditType -match 5) {
$MailBody = $MailBody += "Current Office Users: $officeUsersFilteredCount
"
$mailAttachments = $mailAttachments += $officeUsersAttachment
}
If ($auditType -match 6) {
$MailBody = $MailBody += "Current BlaskGuard Users: $BlaskguardUsersFilteredCount
"
$mailAttachments = $mailAttachments += $BlaskguardUsersAttachment
}
If ($auditType -match 7) {
$MailBody = $MailBody += "Current SSL VPN Users: $sslvpnUsersFilteredCount
"
$mailAttachments = $mailAttachments += $sslvpnUsersAttachment
}
$MailBody = $MailBody += $extraMailBodyInfo
$MailBody = $MailBody += "
If any users should be removed from any of these lists, please contact DCG Technical Solutions before the 15th of the month. We'll be sure to disable and remove any users so you will not incur any further licensing charges for them on your upcoming Monthly Services invoice.
Credits and refunds will not be issued after the 15th of this month.
Thank you very much for taking the time to review these reports with us
DCG Accounting
This message was sent from $env:COMPUTERNAME
This message was sent from an unmonitored mailbox and cannot accept replies. Please send all replies to dispatch@dcgla.com"
Send-MailMessage -From $MailFrom -To $MailTo -Subject $MailSubject -Body $MailBody -Port $SMTPPort -Credential $EmailCredential -Attachments $mailAttachments
Send-MailMessage -From $MailFrom -To $companyContactEmail -Subject $MailSubject -Body $MailBody -Port $SMTPPort -Credential $EmailCredential -Attachments $mailAttachments