forked from SSASQueryAnalyzer/SSASQueryAnalyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathASQA-Install.ps1
More file actions
807 lines (684 loc) · 25.5 KB
/
ASQA-Install.ps1
File metadata and controls
807 lines (684 loc) · 25.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
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
#region Legenda SQL Server versions
# 80 = SQL Server 2000 = 8.00.xxxx
# 90 = SQL Server 2005 = 9.00.xxxx
# 100 = SQL Server 2008 = 10.00.xxxx
# 105 = SQL Server 2008 R2 = 10.50.xxxx
# 110 = SQL Server 2012 = 1100.xxxx
# 120 = SQL Server 2014 = 1200.xxxx
# 130 = SQL Server 2016 = 1300.xxxx
# 140 = SQL Server 2017 = 1400.xxxx
#endregion Legenda SQL Server versions
#region Utility Functions --> This region contains all the "Utility Functions" used by the script
#region UF-Environment --> Utility functions related to the Environment
function CheckPSVersion()
{
[string]$PSMinimalVersion = [string]$global:PSMinimalMajorVersion + '.' + [string]$global:PSMinimalMinorVersion
$Major = $PSVersionTable.PSVersion.Major
$Minor = $PSVersionTable.PSVersion.Minor
[string]$global:PSVersion = [string]$Major + '.' + [string]$Minor
if( ($Major -lt $global:PSMinimalMajorVersion) -or (($Major -ge $global:PSMinimalMajorVersion) -and ($Minor -lt $global:PSMinimalMinorVersion)) )
{
WriteLog 0 "Error:" "red"
WriteLog 1 "Current Powershell Version: $PSVersion" "red"
WriteLog 1 "Minimal Powershell Version required by '$global:CurrentScriptName': $PSMinimalVersion" "red"
WriteLog 1 "Quit execution" "red"
Exit 1
}
}
function UpdateRegistrySkipLoadingProperty([string]$SSMSPackagesRegistryKey, [boolean]$CreateIfNotExists)
{
$PropertyName = "SkipLoading"
$PropertyValue = "00000001"
# Create "Packages" entry if not exists
if( ($CreateIfNotExists) -and !(Test-Path $SSMSPackagesRegistryKey) )
{
New-Item -Path $SSMSPackagesRegistryKey | Out-Null
}
$ASQAPackageRegistryKey = $SSMSPackagesRegistryKey + "\" + $global:ASQAPackageGUID
# Create ASQA package entry if not exists
if( ($CreateIfNotExists) -and !(Test-Path $ASQAPackageRegistryKey) )
{
New-Item -Path $ASQAPackageRegistryKey | Out-Null
}
# Set "SkipLoading" property
Set-ItemProperty -Path $ASQAPackageRegistryKey -Name $PropertyName -Value $PropertyValue -Type DWord
}
function UpdateRegistry()
{
$global:StepCounter++;
$CurrentTabs = 2
WriteLog 0 ""
WriteLog 0 ""
WriteLog $CurrentTabs "BEGIN step $global:StepCounter) Update Registry"
#region ASQA SSMSV SPackage2012
if( $global:ASQASSMSVSPackage2012IsInstalled )
{
$SSMSPackagesRegistryKey = "Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER\SOFTWARE\Microsoft\SQL Server Management Studio\11.0\Packages"
UpdateRegistrySkipLoadingProperty $SSMSPackagesRegistryKey $true
$CurrentTabs = 3
WriteLog $CurrentTabs "--> Updated registry for ASQA SSMS VSPackage 2012" "green"
}
#endregion ASQA SSMSV SPackage2012
#region ASQA SSMSV SPackage2014
if( $global:ASQASSMSVSPackage2014IsInstalled )
{
$SSMSPackagesRegistryKey = "Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER\SOFTWARE\Microsoft\SQL Server Management Studio\12.0\Packages"
UpdateRegistrySkipLoadingProperty $SSMSPackagesRegistryKey $true
$CurrentTabs = 3
WriteLog $CurrentTabs "--> Updated registry for ASQA SSMS VSPackage 2014" "green"
}
#endregion ASQA SSMSV SPackage2014
#region ASQA SSMSV SPackage2016
if( $global:ASQASSMSVSPackage2016IsInstalled )
{
$SSMSPackagesRegistryKey = "Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER\SOFTWARE\Microsoft\SQL Server Management Studio\13.0\Packages"
UpdateRegistrySkipLoadingProperty $SSMSPackagesRegistryKey $true
$CurrentTabs = 3
WriteLog $CurrentTabs "--> Updated registry for ASQA SSMS VSPackage 2016" "green"
}
#endregion ASQA SSMSV SPackage2016
#region ASQA SSMSV SPackage2017
if( $global:ASQASSMSVSPackage2017IsInstalled )
{
$SSMSPackagesRegistryKey = "Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER\SOFTWARE\Microsoft\SQL Server Management Studio\14.0\Packages"
UpdateRegistrySkipLoadingProperty $SSMSPackagesRegistryKey $true
$CurrentTabs = 3
WriteLog $CurrentTabs "--> Updated registry for ASQA SSMS VSPackage 2017" "green"
}
#endregion ASQA SSMSV SPackage2017
$CurrentTabs = 2
WriteLog $CurrentTabs "END step $global:StepCounter) Update Registry"
}
function RetrieveSSMSInstalledFolders()
{
#region SSMS 2012
if (Test-Path $global:SSMS2012_RegistryConfigKey)
{
$RegistryKey = Get-ItemProperty -Path $global:SSMS2012_RegistryConfigKey -Name InstallDir
$global:SSMS2012_InstallationFolder = $RegistryKey.InstallDir
}
#endregion SSMS 2012
#region SSMS 2014
if (Test-Path $global:SSMS2014_RegistryConfigKey)
{
$RegistryKey = Get-ItemProperty -Path $global:SSMS2014_RegistryConfigKey -Name InstallDir
$global:SSMS2014_InstallationFolder = $RegistryKey.InstallDir
}
#endregion SSMS 2014
#region SSMS 2016
if (Test-Path $global:SSMS2016_RegistryConfigKey)
{
$RegistryKey = Get-ItemProperty -Path $global:SSMS2016_RegistryConfigKey -Name InstallDir
$global:SSMS2016_InstallationFolder = $RegistryKey.InstallDir
}
#endregion SSMS 2016
#region SSMS 2017
if (Test-Path $global:SSMS2017_RegistryConfigKey)
{
$RegistryKey = Get-ItemProperty -Path $global:SSMS2017_RegistryConfigKey -Name InstallDir
$global:SSMS2017_InstallationFolder = $RegistryKey.InstallDir
}
#endregion SSMS 2017
}
function CheckInstalledSSMS()
{
$global:StepCounter++;
$CurrentTabs = 2
WriteLog 0 ""
WriteLog 0 ""
WriteLog $CurrentTabs "BEGIN step $global:StepCounter) Check ASQA SSMS VSPackages to install"
$global:CounterSSMSFounded = 0
# Retrieve installation folder for all the installed SSMS
RetrieveSSMSInstalledFolders
#region ASQA SSMS 2012
if ( (![string]::IsNullOrEmpty($global:SSMS2012_InstallationFolder)) -and (Test-Path $global:SSMS2012_InstallationFolder) )
{
$global:SSMS2012_ExtensionsFolder = $global:SSMS2012_InstallationFolder + $global:SSMSExtensionsFolder
$global:SSMS2012IsInstalled = $true
$global:CounterSSMSFounded++
$CurrentTabs = 3
WriteLog $CurrentTabs "--> SSMS 2012 founded! ASQA SSMS VSPackage 2012 will be installed!" "green"
}
else
{
$CurrentTabs = 3
WriteLog $CurrentTabs "--> SSMS 2012 not founded! ASQA SSMS VSPackage 2012 will NOT be installed!" "red"
}
#endregion ASQA SSMS 2012
#region ASQA SSMS 2014
if ( (![string]::IsNullOrEmpty($global:SSMS2014_InstallationFolder)) -and (Test-Path $global:SSMS2014_InstallationFolder) )
{
$global:SSMS2014_ExtensionsFolder = $global:SSMS2014_InstallationFolder + $global:SSMSExtensionsFolder
$global:SSMS2014IsInstalled = $true
$global:CounterSSMSFounded++
$CurrentTabs = 3
WriteLog $CurrentTabs "--> SSMS 2014 founded! ASQA SSMS VSPackage 2014 will be installed!" "green"
}
else
{
$CurrentTabs = 3
WriteLog $CurrentTabs "--> SSMS 2014 not founded! ASQA SSMS VSPackage 2014 will NOT be installed!" "red"
}
#endregion ASQA SSMS 2014
#region ASQA SSMS 2016
if ( (![string]::IsNullOrEmpty($global:SSMS2016_InstallationFolder)) -and (Test-Path $global:SSMS2016_InstallationFolder) )
{
$global:SSMS2016_ExtensionsFolder = $global:SSMS2016_InstallationFolder + $global:SSMSExtensionsFolder
$global:SSMS2016IsInstalled = $true
$global:CounterSSMSFounded++
$CurrentTabs = 3
WriteLog $CurrentTabs "--> SSMS 2016 founded! ASQA SSMS VSPackage 2016 will be installed!" "green"
}
else
{
$CurrentTabs = 3
WriteLog $CurrentTabs "--> SSMS 2016 not founded! ASQA SSMS VSPackage 2016 will NOT be installed!" "red"
}
#endregion ASQA SSMS 2016
#region ASQA SSMS 2017
if ( (![string]::IsNullOrEmpty($global:SSMS2017_InstallationFolder)) -and (Test-Path $global:SSMS2017_InstallationFolder) )
{
$global:SSMS2017_ExtensionsFolder = $global:SSMS2017_InstallationFolder + $global:SSMSExtensionsFolder
$global:SSMS2017IsInstalled = $true
$global:CounterSSMSFounded++
$CurrentTabs = 3
WriteLog $CurrentTabs "--> SSMS 2017 founded! ASQA SSMS VSPackage 2017 will be installed!" "green"
}
else
{
$CurrentTabs = 3
WriteLog $CurrentTabs "--> SSMS 2017 not founded! ASQA SSMS VSPackage 2017 will NOT be installed!" "red"
}
#endregion ASQA SSMS 2017
$CurrentTabs = 2
WriteLog $CurrentTabs "END step $global:StepCounter) Check ASQA SSMS VSPackages to install"
}
function InstallVSPackages()
{
$global:CurrentTabs = 2
$global:StepCounter++
$global:TaskCounter = 0
WriteLog 0 ""
WriteLog 0 ""
WriteLog $global:CurrentTabs "BEGIN step $global:StepCounter) Install all ASQA SSMS VSPackages"
WriteLog 0 ""
#region ASQA SSMSV SPackage2012
if ($global:SSMS2012IsInstalled)
{
$global:CurrentTabs = 4
$global:TaskCounter = 0
WriteLog $global:CurrentTabs "BEGIN ASQA SSMS VSPackage 2012"
if (!(Test-Path $global:SSMS2012_ExtensionsFolder))
{
$global:CurrentTabs = 4
if(!(CreateFolder $global:SSMS2012_ExtensionsFolder $false))
{
WriteLog $global:CurrentTabs "--> Quit installation" "red"
$global:CurrentTabs = 4
Exit
}
$global:CurrentTabs = 4
}
$ASQA2012_InstallFolder_Bin = $global:ASQA_InstallFolder_Bin + "\110"
Get-ChildItem $ASQA2012_InstallFolder_Bin | foreach {
CopyFile $_.FullName $global:SSMS2012_ExtensionsFolder
}
$global:ASQASSMSVSPackage2012IsInstalled = $true
WriteLog $global:CurrentTabs "END ASQA SSMS VSPackage 2012"
WriteLog 0 ""
}
#endregion ASQA SSMSV SPackage2012
#region ASQA SSMSV SPackage2014
if ($global:SSMS2014IsInstalled)
{
$global:CurrentTabs = 4
$global:TaskCounter = 0
WriteLog $global:CurrentTabs "BEGIN ASQA SSMS VSPackage 2014"
if (!(Test-Path $global:SSMS2014_ExtensionsFolder))
{
$global:CurrentTabs = 4
if(!(CreateFolder $global:SSMS2014_ExtensionsFolder $false))
{
WriteLog $global:CurrentTabs "--> Quit installation" "red"
$global:CurrentTabs = 4
Exit
}
$global:CurrentTabs = 4
}
$ASQA2014_InstallFolder_Bin = $global:ASQA_InstallFolder_Bin + "\120"
Get-ChildItem $ASQA2014_InstallFolder_Bin | foreach {
CopyFile $_.FullName $global:SSMS2014_ExtensionsFolder
}
$global:ASQASSMSVSPackage2014IsInstalled = $true
WriteLog $global:CurrentTabs "END ASQA SSMS VSPackage 2014"
WriteLog 0 ""
}
#endregion ASQA SSMSV SPackage2014
#region ASQA SSMSV SPackage2016
if ($global:SSMS2016IsInstalled)
{
$global:CurrentTabs = 4
$global:TaskCounter = 0
WriteLog $global:CurrentTabs "BEGIN ASQA SSMS VSPackage 2016"
if (!(Test-Path $global:SSMS2016_ExtensionsFolder))
{
$global:CurrentTabs = 4
if(!(CreateFolder $global:SSMS2016_ExtensionsFolder $false))
{
WriteLog $global:CurrentTabs "--> Quit installation" "red"
$global:CurrentTabs = 4
Exit
}
$global:CurrentTabs = 4
}
$ASQA2016_InstallFolder_Bin = $global:ASQA_InstallFolder_Bin + "\130"
Get-ChildItem $ASQA2016_InstallFolder_Bin | foreach {
CopyFile $_.FullName $global:SSMS2016_ExtensionsFolder
}
$global:ASQASSMSVSPackage2016IsInstalled = $true
WriteLog $global:CurrentTabs "END ASQA SSMS VSPackage 2016"
WriteLog 0 ""
}
#endregion ASQA SSMSV SPackage2016
#region ASQA SSMSV SPackage2017
if ($global:SSMS2017IsInstalled)
{
$global:CurrentTabs = 4
$global:TaskCounter = 0
WriteLog $global:CurrentTabs "BEGIN ASQA SSMS VSPackage 2017"
if (!(Test-Path $global:SSMS2017_ExtensionsFolder))
{
$global:CurrentTabs = 4
if(!(CreateFolder $global:SSMS2017_ExtensionsFolder $false))
{
WriteLog $global:CurrentTabs "--> Quit installation" "red"
$global:CurrentTabs = 4
Exit
}
$global:CurrentTabs = 4
}
$ASQA2017_InstallFolder_Bin = $global:ASQA_InstallFolder_Bin + "\140"
Get-ChildItem $ASQA2017_InstallFolder_Bin | foreach {
CopyFile $_.FullName $global:SSMS2017_ExtensionsFolder
}
$global:ASQASSMSVSPackage2017IsInstalled = $true
WriteLog $global:CurrentTabs "END ASQA SSMS VSPackage 2017"
WriteLog 0 ""
}
#endregion ASQA SSMSV SPackage2017
$global:CurrentTabs = 2
WriteLog $global:CurrentTabs "END step $global:StepCounter) Install all ASQA SSMS VSPackages"
}
#endregion UF-Environment
#region UF-Script --> Utility functions related to the Powershell script
function Get-ScriptDirectory
{
$Invocation = (Get-Variable MyInvocation -Scope 1).Value
Split-Path $Invocation.MyCommand.Path
}
#endregion UF-Script
#region UF-Log --> Utility functions related to the management of the Log
function CreateLogFile()
{
WriteLog $global:CurrentTabs "BEGIN step $global:StepCounter) Create log file"
$global:CurrentTabs++
$global:TaskCounter++
WriteLog $global:CurrentTabs "Task $global:TaskCounter) Creating log file '$global:LogFileFullName' ..."
try
{
New-Item -Path $global:LogFilePath -Name $global:LogFileName -ItemType File | out-null
# Log only on file
$global:logOnFile = $true
$global:logOnVideo = $false
WriteLogHeader
WriteLog $global:CurrentTabs "Task $TaskCounter) Creating log file '$global:LogFileFullName' ..."
$global:CurrentTabs++
# Log on both (file & video)
$global:logOnFile = $true
$global:logOnVideo = $true
WriteLog $global:CurrentTabs "--> The file was created successfully!" "green"
$global:CurrentTabs = 2
WriteLog $global:CurrentTabs "END step $global:StepCounter) Create log file"
}
catch
{
$global:CurrentTabs++
$CustomText = "The process failed!"
WriteLogError $_ $CustomText $global:CurrentTabs $true
Exit
}
}
function WriteLogHeader()
{
WriteLog 0 ""
WriteLog 0 $global:LogStarLine
WriteLogCentered "INFO"
WriteLog 1 "ENVINRONMENT"
WriteLog 1 " Powershell Version: $global:PSVersion" "yellow"
WriteLog 0 ""
WriteLog 1 "SCRIPT"
WriteLog 1 " Path: $global:CurrentScriptPath" "yellow"
WriteLog 1 " File name: $global:CurrentScriptName" "yellow"
WriteLog 1 " Author: Francesco De Chirico" "yellow"
WriteLog 0 ""
WriteLog 1 "CLIENT"
WriteLog 1 " CurrentDateTime: $global:StartTime" "yellow"
WriteLog 1 " ComputerName: $env:ComputerName" "yellow"
WriteLog 1 " UserDomain: $env:USERDOMAIN" "yellow"
WriteLog 1 " UserName: $env:USERNAME" "yellow"
WriteLog 0 ""
WriteLog 0 $global:LogStarLine
WriteLog 0 ""
WriteLog 0 ""
WriteLog 1 "Script execution starts here"
WriteLog 0 ""
}
function WriteLogFooter()
{
$EndTime = Get-Date
$ElapsedTimeSpan = New-TimeSpan -Start $global:StartTime -End $EndTime #"The timespan is {0:G}" -f $nts
$ElapsedTimeSpanString = "{0:g}" -f $ElapsedTimeSpan
$TotalMilliseconds = $ElapsedTimeSpan.TotalMilliseconds;
WriteLog 0 ""
WriteLog 1 "Script execution ends here"
WriteLog 0 ""
WriteLog 0 ""
WriteLog 0 $global:LogStarLine
WriteLogCentered "EXECUTION DETAILS"
WriteLog 1 "EXECUTION"
WriteLog 1 " Start time: $global:StartTime" "yellow"
WriteLog 1 " End time: $EndTime" "yellow"
WriteLog 1 " Total elapsed time: $ElapsedTimeSpanString" "yellow"
WriteLog 0 ""
WriteLog 0 $global:LogStarLine
WriteLog 0 ""
}
function WriteLog([Int]$Tabs, [String]$Message, [String]$TextColor="white", [Boolean]$ResizeLog=$true)
{
$LogText = "";
for ($i=1;$i -le $Tabs; $i++)
{
$LogText = $LogText + "`t"
}
$LogText = $LogText + $Message
$ResizedLogText = "$LogText"
if($ResizeLog -eq $true)
{
$ResizedLogText = ResizeLog $LogText
}
OutputLog $LogText $ResizedLogText $TextColor
}
function WriteLogCentered([String]$Message, [String]$TextColor="white")
{
$LogText = "";
if($global:WindowWidth -lt $Message.Length)
{
OutputLog "Shell screen width is smaller then text to output!" "red"
return
}
$StartText = ($global:WindowWidth - $Message.Length) / 2
for ($i=1;$i -le $StartText; $i++)
{
$LogText = $LogText + " "
}
$LogText = $LogText + $Message
$ResizedLogText = ResizeLog $LogText
OutputLog $LogText $ResizedLogText $TextColor
}
function ResizeLog([String]$Message)
{
$Continue = "..."
$SingleTabLength = 8
$UpdatedText = $Message;
$TabCount = [regex]::matches($UpdatedText,"\t").count;
$TabLength = $TabCount * $SingleTabLength;
$TextLength = ($UpdatedText.Length - $TabCount + $TabLength);
if ($TextLength -ge $global:WindowWidth)
{
while($TextLength + $Continue.Length -ge $global:WindowWidth)
{
$UpdatedText = $UpdatedText.Substring(0,$UpdatedText.Length -1)
$TabCount = [regex]::matches($UpdatedText,"\t").count
$TabLength = $TabCount * $SingleTabLength;
$TextLength = ($UpdatedText.Length - $TabCount + $TabLength);
}
$UpdatedText = $UpdatedText + $Continue;
}
return $UpdatedText
}
function WriteLogError([String]$ErrorText, [String]$CustomText, [Int]$Tabs, [Boolean]$Exit)
{
$ErrorDetails = "$ErrorText"
if(!($ErrorDetails))
{
$ErrorDetails = "$CustomText"
$CustomText = "Failed with error"
}
WriteLog $Tabs "--> $CustomText!" "red"
if($ErrorDetails)
{
WriteLog $Tabs "--> Details: $ErrorDetails" "red" $false
}
if ($Exit -eq $true)
{
WriteLog 1 "Quit installation" "red"
Exit 3
}
}
function OutputLog([String]$Message, [String]$ResizedMessage, [String]$TextColor="white")
{
if ($global:logOnVideo -and $global:logOnFile)
{
Write-Host $ResizedMessage -foregroundcolor $TextColor
Out-File -FilePath $global:LogFileFullName -InputObject $Message -Append
}
else
{
if ($global:logOnVideo)
{
Write-Host $ResizedMessage -foregroundcolor $TextColor
}
else
{
if ($global:logOnFile)
{
Out-File -FilePath $global:LogFileFullName -InputObject $Message -Append
}
}
}
}
#endregion UF-Log
#region File System Functions --> This region contains all the "File System" used by the script
function CopyFile([String]$SourceFileFullName, [String]$DestinationFolderFullName)
{
$global:TaskCounter++
WriteLog $global:CurrentTabs "Task $global:TaskCounter) Copying file: '$SourceFileFullName' ..."
WriteLog $global:CurrentTabs " to folder '$DestinationFolderFullName' ..."
try
{
copy-item -path $SourceFileFullName -destination $DestinationFolderFullName
WriteLog $global:CurrentTabs "--> The file was copied successfully!!" "green"
}
catch
{
$CustomText = "The process failed!"
WriteLogError $_ $CustomText $global:CurrentTabs $true
Exit
}
}
function CheckThatFolderExist([String]$FolderFullName, [Boolean]$Exit)
{
$global:CurrentTabs = 3
$global:TaskCounter++
WriteLog $global:CurrentTabs "Task $global:TaskCounter) Checking folder '$FolderFullName' ..."
$global:CurrentTabs = 4
try
{
if (!(Test-Path $FolderFullName))
{
$CustomText = "The folder was not found"
WriteLogError $_ $CustomText $global:CurrentTabs $Exit
return $false
}
else
{
WriteLog $global:CurrentTabs "--> The folder was found!" "green"
return $true
}
}
catch
{
$CustomText = "The process failed!"
WriteLogError $_ $CustomText $global:CurrentTabs $true
Exit
}
}
function CheckSourceFolders()
{
$global:CurrentTabs = 2
$global:StepCounter++;
$global:TaskCounter = 0
WriteLog 0 ""
WriteLog 0 ""
WriteLog $global:CurrentTabs "BEGIN step $global:StepCounter) Check that source folder exist"
if(!(CheckThatFolderExist $global:ASQA_InstallFolder_Bin $false))
{
WriteLog $global:CurrentTabs "--> Quit installation" "red"
$global:CurrentTabs = 2
WriteLog $global:CurrentTabs "END step $global:StepCounter) Check that source folder exist"
Exit
}
$global:CurrentTabs = 2
WriteLog $global:CurrentTabs "END step $global:StepCounter) Check that source folder exist"
}
function CreateFolder([String]$FolderFullName, [Boolean]$Exit)
{
$global:CurrentTabs = 4
$global:TaskCounter++
WriteLog $global:CurrentTabs "Task $global:TaskCounter) Creating folder '$FolderFullName' ..."
try
{
if (Test-Path $FolderFullName)
{
$global:CurrentTabs = 5
$CustomText = "The folder already exists"
WriteLogError $_ $CustomText $global:CurrentTabs $Exit
return $false
}
else
{
$global:CurrentTabs = 5
mkdir -Force $FolderFullName | out-null
WriteLog $global:CurrentTabs "--> The folder was created successfully!" "green"
return $true
}
$global:CurrentTabs--
}
catch
{
$CustomText = "The process failed!"
WriteLogError $_ $CustomText $global:CurrentTabs $true
Exit
}
}
#endregion File System Functions
#endregion Utility Functions
#region Main function --> This region contains the Main function of the Powershell script
function Main()
{
$ErrorActionPreference = "Stop"
$VerbosePreference = "SilentlyContinue"
$WarningPreference = "SilentlyContinue"
$global:StartTime = Get-Date
CheckPSVersion
$global:logOnFile = $false
$global:logOnVideo = $true
WriteLogHeader
try
{
$global:CurrentTabs = 2
$global:StepCounter = 1
$global:TaskCounter = 0
# STEP 1
CreateLogFile
# STEP 2
CheckSourceFolders
# STEP 3
CheckInstalledSSMS
# STEP 4
InstallVSPackages
# STEP 5
UpdateRegistry
}
finally
{
WriteLogFooter
}
}
#endregion
#region Global Variables --> This region contains all the "Global Variable" used by the script
#region GV-Environment --> Global Variables related to the Environment
$global:PSVersion = $null
$pshost = Get-Host
$pswindow = $pshost.UI.RawUI
$global:WindowWidth = $pswindow.windowsize.width
if($global:WindowWidth -lt 100)
{
$global:WindowWidth = 100
}
#endregion GV-Environment
#region GV-Script --> Global Variables related to the Powershell script
$global:CurrentScriptName = $MyInvocation.MyCommand.Name
$global:CurrentScriptPath = $PSScriptRoot
$global:StepCounter = 0
$global:TaskCounter = 0
#endregion GV-Script
#region GV-Execution --> Global Variables related to the Execution of the script
$global:CurrentDateTime = Get-Date -format "yyyyMMddHHmmss"
$global:StartTime = $null
#endregion GV-Execution
#region GV-Log --> Global Variables related to the management of the Log
$global:CurrentTabs = 0
$global:logOnVideo = $true
$global:logOnFile = $false #--> Cannot be true here
$global:LogStarLine = ""
for ($i=1;$i -lt $global:WindowWidth; $i++)
{
$global:LogStarLine = $global:LogStarLine + "*"
}
$global:LogFileExtension = ".log"
$global:LogFilePath = "$(Get-ScriptDirectory)"
$global:LogFileName = "ASQAInstall_$global:CurrentDateTime$global:LogFileExtension"
$global:LogFileFullName = "$global:LogFilePath\$global:LogFileName"
#endregion GV-Log
#region GV-ASQA --> Global Variables related to ASQA SSMS VSPackage
$global:SSMS2012IsInstalled = $false
$global:SSMS2014IsInstalled = $false
$global:SSMS2016IsInstalled = $false
$global:SSMS2017IsInstalled = $false
$global:CounterSSMSFounded = 0
$global:ASQAPackageGUID = "{28672cc0-c8a7-4203-b4ce-d9c3e6216812}"
$global:ASQASSMSVSPackage2012IsInstalled = $false
$global:ASQASSMSVSPackage2014IsInstalled = $false
$global:ASQASSMSVSPackage2016IsInstalled = $false
$global:ASQASSMSVSPackage2017IsInstalled = $false
$global:ASQA_InstallFolder = "$global:CurrentScriptPath"
$global:ASQA_InstallFolder_Bin = "$global:ASQA_InstallFolder\Bin\"
$global:SSMS2012_RegistryConfigKey = "Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER\Software\Microsoft\SQL Server Management Studio\11.0_Config\"
$global:SSMS2012_InstallationFolder = ""
$global:SSMS2012_ExtensionsFolder = ""
$global:SSMS2014_RegistryConfigKey = "Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER\Software\Microsoft\SQL Server Management Studio\12.0_Config\"
$global:SSMS2014_InstallationFolder = ""
$global:SSMS2014_ExtensionsFolder = ""
$global:SSMS2016_RegistryConfigKey = "Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER\Software\Microsoft\SQL Server Management Studio\13.0_Config\"
$global:SSMS2016_InstallationFolder = ""
$global:SSMS2016_ExtensionsFolder = ""
$global:SSMS2017_RegistryConfigKey = "Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER\Software\Microsoft\SQL Server Management Studio\14.0_Config\"
$global:SSMS2017_InstallationFolder = ""
$global:SSMS2017_ExtensionsFolder = ""
$global:SSMSExtensionsFolder = "\Extensions\SSASQueryAnalyzer.Client.SSMS\"
#endregion GV-ASQA
#endregion Global Variables
# Script starts here
Main #--> call to the Main function
# Script ends here