|
| 1 | +<# |
| 2 | + .SYNOPSIS |
| 3 | + Returns installed Microsoft SQL Server component. |
| 4 | +
|
| 5 | + .DESCRIPTION |
| 6 | + Returns installed Microsoft SQL Server component. |
| 7 | +
|
| 8 | + .PARAMETER ServerName |
| 9 | + Specifies the server name to return the components from. |
| 10 | +
|
| 11 | + .PARAMETER InstanceName |
| 12 | + Specifies the instance name for which to return installed components. |
| 13 | +
|
| 14 | + .PARAMETER Version |
| 15 | + Specifies the version for which to return installed components. If the |
| 16 | + parameter InstanceName is not provided then all instances with the version |
| 17 | + will be returned. |
| 18 | +
|
| 19 | + .EXAMPLE |
| 20 | + Get-SqlDscInstalledComponents |
| 21 | +
|
| 22 | + Returns all the installed components. |
| 23 | +
|
| 24 | + .OUTPUTS |
| 25 | + `[System.Management.Automation.PSCustomObject]` |
| 26 | +#> |
| 27 | +function Get-SqlDscInstalledComponent |
| 28 | +{ |
| 29 | + [OutputType([System.Management.Automation.PSCustomObject])] |
| 30 | + [CmdletBinding()] |
| 31 | + param |
| 32 | + ( |
| 33 | + [Parameter()] |
| 34 | + [ValidateNotNullOrEmpty()] |
| 35 | + [System.String] |
| 36 | + $ServerName = (Get-ComputerName), |
| 37 | + |
| 38 | + [Parameter()] |
| 39 | + [System.String] |
| 40 | + $InstanceName, |
| 41 | + |
| 42 | + [Parameter()] |
| 43 | + [System.Version] |
| 44 | + $Version |
| 45 | + ) |
| 46 | + |
| 47 | + Assert-ElevatedUser -ErrorAction 'Stop' |
| 48 | + |
| 49 | + $installedComponents = @() |
| 50 | + |
| 51 | + $currentInstalledServices = Get-SqlDscManagedComputerService -ServerName $ServerName -ErrorAction 'Stop' |
| 52 | + |
| 53 | + foreach ($installedService in $currentInstalledServices) |
| 54 | + { |
| 55 | + $serviceType = $installedService.Type | ConvertFrom-ManagedServiceType -ErrorAction 'SilentlyContinue' |
| 56 | + |
| 57 | + if (-not $serviceType) |
| 58 | + { |
| 59 | + <# |
| 60 | + This is a workaround because [Microsoft.SqlServer.Management.Smo.Wmi.ManagedServiceType] |
| 61 | + does not support all service types yet. |
| 62 | + #> |
| 63 | + $serviceType = $installedService.Type |
| 64 | + } |
| 65 | + |
| 66 | + $fileProductVersion = $null |
| 67 | + |
| 68 | + $serviceExecutablePath = (($installedService.PathName -replace '"') -split ' -')[0] |
| 69 | + |
| 70 | + if ((Test-Path -Path $serviceExecutablePath)) |
| 71 | + { |
| 72 | + $fileProductVersion = [System.Version] (Get-FileVersionInformation -FilePath $serviceExecutablePath).ProductVersion |
| 73 | + } |
| 74 | + |
| 75 | + # Get InstanceName from the service name if it exist. |
| 76 | + $serviceInstanceName = if ($installedService.Name -match '\$(.*)$') |
| 77 | + { |
| 78 | + $Matches[1] |
| 79 | + } |
| 80 | + |
| 81 | + $serviceStartMode = $installedService.StartMode | ConvertFrom-ServiceStartMode |
| 82 | + |
| 83 | + <# |
| 84 | + There are more properties that can be fetch from advanced properties, |
| 85 | + for example InstanceId, Clustered, and Version (for some), but it takes |
| 86 | + about 6 seconds for it to return a value. Because of the slowness this |
| 87 | + command does not use advanced properties, e.g: |
| 88 | + ($installedService.AdvancedProperties | ? Name -eq 'InstanceId').Value |
| 89 | + #> |
| 90 | + $serviceComponent = [PSCustomObject] @{ |
| 91 | + ServiceName = $installedService.Name |
| 92 | + ServiceType = $serviceType |
| 93 | + ServiceDisplayName = $installedService.DisplayName |
| 94 | + ServiceAccountName = $installedService.ServiceAccount |
| 95 | + ServiceStartMode = $serviceStartMode |
| 96 | + InstanceName = $serviceInstanceName |
| 97 | + ServiceExecutableVersion = $fileProductVersion |
| 98 | + |
| 99 | + # Properties that should be on all objects, but set later |
| 100 | + InstanceId = $null |
| 101 | + Feature = @() |
| 102 | + } |
| 103 | + |
| 104 | + $installedComponents += $serviceComponent |
| 105 | + } |
| 106 | + |
| 107 | + # Loop to set InstanceId. |
| 108 | + foreach ($component in $installedComponents.Where({ $_.ServiceType -in ('DatabaseEngine', 'AnalysisServices', 'ReportingServices') })) |
| 109 | + { |
| 110 | + $component.InstanceId = $component.ServiceType | Get-InstanceId -InstanceName $component.InstanceName |
| 111 | + } |
| 112 | + |
| 113 | + # Loop to get features. |
| 114 | + foreach ($component in $installedComponents) |
| 115 | + { |
| 116 | + switch ($component.ServiceType) |
| 117 | + { |
| 118 | + 'DatabaseEngine' |
| 119 | + { |
| 120 | + $component.Feature += 'SQLEngine' |
| 121 | + |
| 122 | + #$isReplicationInstalled = Test-IsReplicationFeatureInstalled -InstanceName $InstanceName |
| 123 | + |
| 124 | + if ($isReplicationInstalled) |
| 125 | + { |
| 126 | + $component.Feature += 'Replication' |
| 127 | + } |
| 128 | + |
| 129 | + #$isDQInstalled = Test-IsDQComponentInstalled -InstanceName $InstanceName -SqlServerMajorVersion $sqlVersion |
| 130 | + |
| 131 | + if ($isDQInstalled) |
| 132 | + { |
| 133 | + $component.Feature += 'DQ' |
| 134 | + } |
| 135 | + |
| 136 | + #TODO: This has nothing to do with DatabaseEngine. |
| 137 | + $isDQInstalled = Test-IsDataQualityClientInstalled -Version $component.Version |
| 138 | + |
| 139 | + if ($isDQInstalled) |
| 140 | + { |
| 141 | + $component.Feature += 'DQC' |
| 142 | + } |
| 143 | + |
| 144 | + #$isSsmsInstalled = Test-IsSsmsInstalled -SqlServerMajorVersion $sqlVersion |
| 145 | + |
| 146 | + if ($isSsmsInstalled) |
| 147 | + { |
| 148 | + $component.Feature += 'SSMS' |
| 149 | + } |
| 150 | + |
| 151 | + #$isSsmsAdvancedInstalled = Test-IsSsmsAdvancedInstalled -SqlServerMajorVersion $sqlVersion |
| 152 | + |
| 153 | + if ($isSsmsAdvancedInstalled) |
| 154 | + { |
| 155 | + $component.Feature += 'ADV_SSMS' |
| 156 | + } |
| 157 | + |
| 158 | + break |
| 159 | + } |
| 160 | + |
| 161 | + '9' |
| 162 | + { |
| 163 | + $component.Feature += 'FullText' |
| 164 | + |
| 165 | + break |
| 166 | + } |
| 167 | + |
| 168 | + '12' |
| 169 | + { |
| 170 | + $component.Feature += 'AdvancedAnalytics' |
| 171 | + |
| 172 | + break |
| 173 | + } |
| 174 | + |
| 175 | + 'AnalysisServices' |
| 176 | + { |
| 177 | + $component.Feature += 'AS' |
| 178 | + |
| 179 | + break |
| 180 | + } |
| 181 | + |
| 182 | + 'IntegrationServices' |
| 183 | + { |
| 184 | + $component.Feature += 'IS' |
| 185 | + |
| 186 | + break |
| 187 | + } |
| 188 | + |
| 189 | + 'ReportingServices' |
| 190 | + { |
| 191 | + $component.Feature += 'RS' |
| 192 | + |
| 193 | + break |
| 194 | + } |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + |
| 199 | + # Filter result |
| 200 | + |
| 201 | + $componentsToReturn = @() |
| 202 | + |
| 203 | + if ($PSBoundParameters.ContainsKey('Version')) |
| 204 | + { |
| 205 | + if ($PSBoundParameters.ContainsKey('InstanceName')) |
| 206 | + { |
| 207 | + $componentsToReturn += $installedComponents | |
| 208 | + Where-Object -FilterScript { |
| 209 | + -not $_.InstanceName -and $_.FileProductVersion.Major -eq $Version.Major |
| 210 | + } |
| 211 | + } |
| 212 | + else |
| 213 | + { |
| 214 | + $componentsToReturn += $installedComponents | |
| 215 | + Where-Object -FilterScript { |
| 216 | + $_.FileProductVersion.Major -eq $Version.Major |
| 217 | + } |
| 218 | + } |
| 219 | + } |
| 220 | + |
| 221 | + if ($PSBoundParameters.ContainsKey('InstanceName')) |
| 222 | + { |
| 223 | + $componentsToReturn += $installedComponents | |
| 224 | + Where-Object -FilterScript { |
| 225 | + $_.InstanceName -eq $InstanceName |
| 226 | + } |
| 227 | + } |
| 228 | + |
| 229 | + if (-not $componentsToReturn) |
| 230 | + { |
| 231 | + $componentsToReturn = $installedComponents |
| 232 | + } |
| 233 | + |
| 234 | + return $componentsToReturn |
| 235 | +} |
0 commit comments