-
Notifications
You must be signed in to change notification settings - Fork 63
/
Create-MCShares.ps1
418 lines (380 loc) · 14.2 KB
/
Create-MCShares.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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
# Create-MCShares.ps1
# mmessano 12-20-2012
## Function to return a sql query, return the results in an array.
function SQL-Query{
param([string]$Query,
[string]$SqlServer = $DEFAULT_SQL_SERVER,
[string]$DB = $DEFAULT_SQL_DB,
[string]$RecordSeparator = "`t"
)
$conn_options = ("Data Source=$SqlServer; Initial Catalog=$DB;" + "Integrated Security=SSPI")
$conn = New-Object System.Data.SqlClient.SqlConnection($conn_options)
$conn.Open()
$sqlCmd = New-Object System.Data.SqlClient.SqlCommand
$sqlCmd.CommandTimeout = "300"
$sqlCmd.CommandText = $Query
$sqlCmd.Connection = $conn
$reader = $sqlCmd.ExecuteReader()
[array]$serverArray
$arrayCount = 0
while($reader.Read()){
$serverArray += ,($reader.GetValue(0))
$arrayCount++
}
$serverArray
}
# // New-Share: Creates new Share on local or remote PC, with custom permissions.
# // Required Parameters: FolderPath, ShareName
# //
# // New-ACE: Creates ACE Objects, for use when running New-Share.
# // Required Parameters: Name, Domain
# //
# // New-SecurityDescriptor: used by New-Share to prepare the permissions.
# // Required Parameters: ACEs
Function New-SecurityDescriptor (
$ACEs = (throw "Missing one or more Trustees"),
[string] $ComputerName = ".")
{
#Create SeCDesc object
$SecDesc = ([WMIClass] "\\$ComputerName\root\cimv2:Win32_SecurityDescriptor").CreateInstance()
#Check if input is an array or not.
if ($ACEs -is [System.Array])
{
#Add Each ACE from the ACE array
foreach ($ACE in $ACEs )
{
$SecDesc.DACL += $ACE.psobject.baseobject
}
}
else
{
#Add the ACE
$SecDesc.DACL = $ACEs
}
#Return the security Descriptor
return $SecDesc
}
Function New-ACE (
[string] $Name = (throw "Please provide user/group name for trustee"),
[string] $Domain = (throw "Please provide Domain name for trustee"),
[string] $Permission = "Read",
[string] $ComputerName = ".",
[switch] $Group = $false)
{
#Create the Trusteee Object
$Trustee = ([WMIClass] "\\$ComputerName\root\cimv2:Win32_Trustee").CreateInstance()
#Search for the user or group, depending on the -Group switch
if (!$group)
{ $account = [WMI] "\\$ComputerName\root\cimv2:Win32_Account.Name='$Name',Domain='$Domain'" }
else
{ $account = [WMI] "\\$ComputerName\root\cimv2:Win32_Group.Name='$Name',Domain='$Domain'" }
#Get the SID for the found account.
$accountSID = [WMI] "\\$ComputerName\root\cimv2:Win32_SID.SID='$($account.sid)'"
#Setup Trusteee object
$Trustee.Domain = $Domain
$Trustee.Name = $Name
$Trustee.SID = $accountSID.BinaryRepresentation
#Create ACE (Access Control List) object.
$ACE = ([WMIClass] "\\$ComputerName\root\cimv2:Win32_ACE").CreateInstance()
#Select the AccessMask depending on the -Permission parameter
switch ($Permission)
{
"Read" { $ACE.AccessMask = 1179817 }
"Change" { $ACE.AccessMask = 1245631 }
"Full" { $ACE.AccessMask = 2032127 }
default { throw "$Permission is not a supported permission value. Possible values are 'Read','Change','Full'" }
}
#Setup the rest of the ACE.
$ACE.AceFlags = 3
$ACE.AceType = 0
$ACE.Trustee = $Trustee
#Return the ACE
return $ACE
}
Function New-Share (
[string] $FolderPath = (throw "Please provide the share folder path (FolderPath)")
, [string] $ShareName = (throw "Please provide the Share Name")
, $ACEs
, [string] $Description = ""
, [string] $ComputerName = ".")
{
#Start the Text for the message.
$text = "$ShareName ($FolderPath): "
#Package the SecurityDescriptor via the New-SecurityDescriptor Function.
$SecDesc = New-SecurityDescriptor $ACEs -ComputerName $ComputerName
#Create the share via WMI, get the return code and create the return message.
if ( ! (GET-WMIOBJECT Win32_Share -ComputerName $ComputerName -Filter "name='$Sharename'") )
{
$Share = [WMICLASS] "\\$ComputerName\Root\Cimv2:Win32_Share"
$result = $Share.Create($FolderPath, $ShareName, 0, $NULL , $Description, $false , $SecDesc)
}
ELSE
{
$Share = GET-WMIOBJECT Win32_Share -ComputerName $ComputerName -Filter "name='$Sharename'"
$result = $Share.SetShareInfo( $NULL, $Description, $SecDesc )
}
switch ($result.ReturnValue)
{
0 {$text += "has been successfully created" }
2 {$text += "Error 2: Access Denied" }
8 {$text += "Error 8: Unknown Failure" }
9 {$text += "Error 9: Invalid Name"}
10 {$text += "Error 10: Invalid Level" }
21 {$text += "Error 21: Invalid Parameter" }
22 {$text += "Error 22 : Duplicate Share"}
23 {$text += "Error 23: Redirected Path" }
24 {$text += "Error 24: Unknown Device or Directory" }
25 {$text += "Error 25: Net Name Not Found" }
}
#Create Custom return object and Add results
$return = New-Object System.Object
$return | Add-Member -type NoteProperty -name Message -value $text
$return | Add-Member -type NoteProperty -name ReturnCode -value $result.ReturnValue
#Return result object
$return
}
###################
##Start Script Here
###################
$ENV = $args[0]
if ($ENV -eq $null){
$ENV = "DEN-PROD"
}
switch ($ENV)
{
"PA-PROD"{ $DBServer = "XSQLUTIL18";
$ArchiveDrive = "E"
$DB = "Status";
$ServerQuery = "SELECT server_name
, domain
, ip_address
, dns_host_name
, perfmon_path
, perfmon_drive
, perfmon_start_time
, perfmon_end_time
FROM t_server s
INNER JOIN t_server_properties sp ON s.server_id = sp.server_id
INNER JOIN t_perfmon_properties pp ON s.server_id = pp.server_id
WHERE Active = '1'
ORDER BY server_name
";
}
"PA-STAGE"{ $DBServer = "FINREP01V";
$ArchiveDrive = "E"
$DB = "Status";
$ServerQuery = "SELECT server_name
, domain
, ip_address
, dns_host_name
, perfmon_path
, perfmon_drive
, perfmon_start_time
, perfmon_end_time
FROM t_server s
INNER JOIN t_server_properties sp ON s.server_id = sp.server_id
INNER JOIN t_perfmon_properties pp ON s.server_id = pp.server_id
WHERE Active = '1'
ORDER BY server_name
";
}
"PA-IMP"{ $DBServer = "ISQLDEV610";
$ArchiveDrive = "E"
$DB = "StatusStage";
$ServerQuery = "SELECT server_name
, domain
, ip_address
, dns_host_name
, perfmon_path
, perfmon_drive
, perfmon_start_time
, perfmon_end_time
FROM t_server s
INNER JOIN t_server_properties sp ON s.server_id = sp.server_id
INNER JOIN t_perfmon_properties pp ON s.server_id = pp.server_id
WHERE Active = '1'
ORDER BY server_name
";
}
"PA-QA"{ $DBServer = "ISQLDEV610";
$ArchiveDrive = "E"
$DB = "StatusIMP";
$ServerQuery = "SELECT server_name
, domain
, ip_address
, dns_host_name
, perfmon_path
, perfmon_drive
, perfmon_start_time
, perfmon_end_time
FROM t_server s
INNER JOIN t_server_properties sp ON s.server_id = sp.server_id
INNER JOIN t_perfmon_properties pp ON s.server_id = pp.server_id
WHERE Active = '1'
ORDER BY server_name
";
}
"DEN-PROD"{ $DBServer = "sqlutil01";
$ArchiveDrive = "D"
$DB = "Status";
$ServerQuery = "SELECT server_name
, domain
, ip_address
, dns_host_name
, perfmon_path
, perfmon_drive
, perfmon_start_time
, perfmon_end_time
FROM t_server s
INNER JOIN t_server_properties sp ON s.server_id = sp.server_id
INNER JOIN t_perfmon_properties pp ON s.server_id = pp.server_id
WHERE Active = '1'
--AND dns_host_name LIKE 'sql%'
ORDER BY server_name
";
}
"LOU-PROD"{ $DBServer = "sqlutil02";
$ArchiveDrive = "D"
$DB = "Status";
$ServerQuery = "SELECT server_name
, domain
, ip_address
, dns_host_name
, perfmon_path
, perfmon_drive
, perfmon_start_time
, perfmon_end_time
FROM t_server s
INNER JOIN t_server_properties sp ON s.server_id = sp.server_id
INNER JOIN t_perfmon_properties pp ON s.server_id = pp.server_id
WHERE Active = '1'
--AND server_name LIKE 'HOpusSQL%'
--OR server_name LIKE 'SQLUTIL%'
ORDER BY server_name
";
}
"FINALE"{ $DBServer = "FINREP01V";
$ArchiveDrive = "C"
$DB = "Status";
$ServerQuery = "SELECT server_name
, domain
, ip_address
, dns_host_name
, perfmon_path
, perfmon_drive
, perfmon_start_time
, perfmon_end_time
FROM t_server s
INNER JOIN t_server_properties sp ON s.server_id = sp.server_id
INNER JOIN t_perfmon_properties pp ON s.server_id = pp.server_id
WHERE Active = '1'
--AND server_name LIKE 'FinRep%'
ORDER BY server_name
";
}
}
$ArchiveIPQuery = "SELECT ip_address FROM t_server_properties where server_id = (select server_id from t_server where server_name = '$DBServer')"
$OperationsSharename = "Operations"
$PerfmonLogsShareName = "PerfmonLogsArchive"
$title = "PerfmonCollector"
$Servers = ( Invoke-SQLCmd -query $ServerQuery -Server $DBServer -Database $DB )
$ArchiveIP = ( Invoke-SQLCmd -query $ArchiveIPQuery -Server $DBServer -Database $DB | Select-Object ip_address)
$domain = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain().name
# create permissions
if ( $domain -eq "mortgagecadence.internal" ) {
$DUACE = New-ACE -Name "Domain Users" -Domain MC -Permission Read -Group
$PerfmonACE = New-Ace -Name svc_perfmon -Domain MC -Permission Full
$mmessanoACE = New-Ace -Name mm_admin -Domain MC -Permission Full
$jmckayACE = New-Ace -Name jmckay -Domain MC -Permission Full
$rhaagACE = New-Ace -Name rhaag -Domain MC -Permission Full
$tdodsonACE = New-Ace -Name tdodson -Domain MC -Permission Full
$sbrownACE = New-Ace -Name sbrown -Domain MC -Permission Full
$jfalconerACE = New-Ace -Name jf_admin -Domain MC -Permission Full
$MCACES = ($DUACE,$PerfmonACE,$mmessanoACE,$jmckayACE,$rhaagACE,$tdodsonACE,$sbrownACE,$jfalconerACE)
}
ELSEIF ( $domain -eq "mcfinale.net" ) {
$DUACE = New-ACE -Name "Domain Users" -Domain MCFinale -Permission Read -Group
$PerfmonACE = New-Ace -Name svc_perfmon -Domain MCFinale -Permission Full
$mmessanoACE = New-Ace -Name mm_admin -Domain MCFinale -Permission Full
$jmckayACE = New-Ace -Name jm_admin -Domain MCFinale -Permission Full
$rhaagACE = New-Ace -Name rh_admin -Domain MCFinale -Permission Full
$tdodsonACE = New-Ace -Name td_admin -Domain MCFinale -Permission Full
$sbrownACE = New-Ace -Name sb_admin -Domain MCFinale -Permission Full
$jfalconerACE = New-Ace -Name jf_admin -Domain MCFinale -Permission Full
$MCACES = ($DUACE,$PerfmonACE,$mmessanoACE,$jmckayACE,$rhaagACE,$tdodsonACE,$sbrownACE,$jfalconerACE)
}
# begin updating servers
foreach ($Server in $Servers)
{
if ($Server -ne $null)
{
# servers are named the same in each domain therefore unroutable; use the full dns name
$ServerName = $($Server.dns_host_name)
$ServerIPAddress = $($Server.ip_address)
$PerfmonDrive = $($Server.perfmon_drive)
$OldPerfLogsDir = "\\" + $ServerIPAddress + "\" + $PerfmonDrive + "$\" + "PerfmonLogs"
$OperationsLocalPath = $PerfmonDrive + ":\" + $OperationsSharename
$PerfmonLogsArchiveLP = $ArchiveDrive + ":\" + $PerfmonLogsShareName
$ArchiveDir = "\\" + $ArchiveIP.ip_address + "\" + $PerfmonLogsShareName + "\" + $ServerName + "\"
$PerfmonLogsArchive = "\\" + $DBServer + "\" + $ArchiveDrive + "$\" + $PerfmonLogsShareName
$OperationsRoot = "\\" + $ServerIPAddress + "\" + $PerfmonDrive + "$\" + $OperationsSharename
Write-Host "----- Begin $ServerName. -----"
# remove old PerfmonLogs directory
if ( Test-Path -Path $OldPerfLogsDir ) {
Write-Host "Removing old PerfmonLogs directory."
Remove-Item -path $OldPerfLogsDir
}
# create operations directory if it does not exist
if (!(Test-Path -path $OperationsRoot)) {
Write-Host "Creating directories."
New-Item -Path $OperationsRoot -type directory
# create sub-directories
if (!( Test-Path -path ($OperationsRoot + "\Logs") )) {
New-Item -Path ($OperationsRoot + "\Logs") -type directory
}
if (!( Test-Path -path ($OperationsRoot + "\Support") )) {
New-Item -Path ($OperationsRoot + "\Support") -type directory
}
if (!( Test-Path -path ($OperationsRoot + "\Support\Perfmon") )) {
New-Item -Path ($OperationsRoot + "\Support\Perfmon") -type directory
}
}
ELSE {
Write-Host "Operations directory exists, checking for sub-directories."
if (!( Test-Path -path ($OperationsRoot + "\Logs") )) {
New-Item -Path ($OperationsRoot + "\Logs") -type directory
}
if (!( Test-Path -path ($OperationsRoot + "\Bin") )) {
New-Item -Path ($OperationsRoot + "\Bin") -type directory
}
if (!( Test-Path -path ($OperationsRoot + "\SSIS") )) {
New-Item -Path ($OperationsRoot + "\SSIS") -type directory
}
if (!( Test-Path -path ($OperationsRoot + "\Support") )) {
New-Item -Path ($OperationsRoot + "\Support") -type directory
}
if (!( Test-Path -path ($OperationsRoot + "\Support\Perfmon") )) {
New-Item -Path ($OperationsRoot + "\Support\Perfmon") -type directory
}
}
# always create share and apply/modify permissions
New-Share -FolderPath $OperationsLocalPath -ShareName $OperationsShareName -Computer $ServerName -ACEs $MCACES
# check for archive server
if ( $($Server.server_name) -eq $DBServer ) {
if (!(Test-Path -path $PerfmonLogsArchive)) {
New-Item -Path $PerfmonLogsArchive -type directory
}
# always create share and apply/modify permissions
New-Share -FolderPath $PerfmonLogsArchiveLP -ShareName $PerfmonLogsShareName -Computer $ServerName -ACEs $MCACES
}
# create archive share if it does not exist
if (!(Test-Path -path $ArchiveDir)) {
New-Item $ArchiveDir -type directory
}
Write-Host "----- End $ServerName. -----"
Write-Host ""
}
}
#C:\users\mm_admin\Documents\WindowsPowerShell\Scripts\Create-MCShares.ps1 DEN-PROD
# rebuild MOF file in an admin ps window
# C:\Windows\System32\wbem\MOFComp CIMWIN32.MOF