-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAdvanced.ps1
More file actions
96 lines (82 loc) · 3.33 KB
/
Advanced.ps1
File metadata and controls
96 lines (82 loc) · 3.33 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
function Get-SqlVersion {
<#
.SYNOPSIS
Sample function showing off advanced function features in PowerShell.
This function could be used in a pipeline to filter a list of SQL Server instances by version and OS.
#>
[CmdletBinding(SupportsShouldProcess=$True,ConfirmImpact='High')]
param(
[Parameter(Mandatory=$True,ValueFromPipeline=$True)]
[Alias('Instance')]
[string]$SqlInstance,
[Parameter(Mandatory=$True)]
[ValidateSet(2012,2014,2016,2017,2018)]
[int]$CurrentMajorVersion
)
DynamicParam {
if ( $CurrentMajorVersion ) {
if ($CurrentMajorVersion -ge 2017) {
$ValSet = @('Windows','Linux')
} else {
$ValSet = @('Windows')
}
}
# Create an attribute collection for our parameter
$AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
#Create a new ParameterAttribute Object
$OSAttribute = New-Object System.Management.Automation.ParameterAttribute
$OSAttribute.HelpMessage = "Please specify an OS to filter by:"
# Create a validation set and add it to the collection
if ( $CurrentMajorVersion ) {
$ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($ValSet)
$AttributeCollection.Add($ValidateSetAttribute)
}
# Add attributes to collection
$AttributeCollection.Add($OSAttribute)
# Create the parameter
$OSParam = New-Object System.Management.Automation.RuntimeDefinedParameter('OS', [string], $AttributeCollection)
# Create and return the parameter dictionary
$ParamDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
$ParamDictionary.Add('OS', $OSParam)
return $ParamDictionary
}
Begin {
# bind the dynamic parameter
$OS = $PsBoundParameters['OS']
Write-Host "Getting SQL Server Version..."
# Set the numeric version major
if ( $CurrentMajorVersion ) {
switch ( $CurrentMajorVersion) {
2012 { $CurrentMajor = 11 }
2014 { $CurrentMajor = 12 }
2016 { $CurrentMajor = 13 }
2017 { $CurrentMajor = 14 }
2018 { $CurrentMajor = 15 }
}
}
# Set a query up
[string]$VersionQuery = @"
SELECT @@SERVERNAME As Server,
SERVERPROPERTY('productversion') AS Version,
CASE
WHEN @@VERSION LIKE '%Windows%' THEN 'Windows'
ELSE 'Linux'
END AS Platform
WHERE CAST(SERVERPROPERTY('productmajorversion') AS VARCHAR(2)) LIKE '$($CurrentMajor)%'
AND @@VERSION LIKE '%$($OS)%'
"@
Write-Verbose "Query:`n$($VersionQuery)"
Write-Debug -Message "Query:`n$($VersionQuery)"
}
Process {
try {
$Version = Invoke-SqlCmd -Query $VersionQuery -ServerInstance $SqlInstance -Username sa -Password myStrongPassword01 -ErrorAction Stop
$Version
}
catch {
Write-Host "$($SqlInstance): failed - $($_.Exception.Message)" -ForegroundColor Red
}
}
End {
}
}