Last active
August 29, 2015 13:56
-
-
Save dotps1/9050441 to your computer and use it in GitHub Desktop.
Gets SQL Edition Type for each installed SQL Instance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
Collects each SQL Edition Type by Instance Name. | |
.DESCRIPTION | |
Enumerates the registry for installed instances of SQL, then foreach installed instance, it enumerates the Edition Type. | |
.EXAMPLE | |
Get-SqlEdition -ComputerName MySQLServer.mydomain.org | |
.EXAMPLE | |
@("MyComputer","MyServer","MyDomainController") | %{ Get-SqlEdition -ComputerName $_ } | |
.NOTES | |
Proper rights are needed to remote read registry values. | |
http://technet.microsoft.com/en-us/library/cc732388.aspx | |
.LINK | |
http://dotps1.github.io | |
#> | |
function Get-SqlEdition | |
{ | |
[CmdletBinding()] | |
[OutputType([PSObject])] | |
param | |
( | |
# ComputerName, Type String, System to enumerate SQL Edition aginst. | |
[Alias("Server")] | |
[ValidateScript({ if (Test-Connection $_ -Count 2){ return $true } else { throw "Failed to ping $_. Please ensure the system is available." }})] | |
[Parameter(Mandatory = $true, | |
ValueFromPipeline = $true, | |
Position = 0)] | |
[String[]] | |
$ComputerName = $env:COMPUTERNAME | |
) | |
try | |
{ | |
$hive=[Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', "$ComputerName") | |
$regKeys=$hive.OpenSubKey("SOFTWARE\\Microsoft\\Microsoft SQL Server") | |
foreach ($instanceNames in $regKeys.OpenSubKey("Instance Names")) | |
{ | |
foreach ($instance in $instanceNames.GetSubKeyNames()) | |
{ | |
$instanceName=$instanceNames.OpenSubKey($instance) | |
foreach ($name in $instanceName.GetValueNames()) | |
{ | |
foreach ($server in $regKeys.OpenSubKey($instanceName.GetValue($name)+"\\Setup")) | |
{ | |
$sqlServer=[PSObject] @{ | |
'ComputerName'=$ComputerName | |
'InstanceName'=$instanceName.GetValue($name) | |
'EditionName'=$server.GetValue("Edition") | |
} | |
return $sqlServer | |
} | |
} | |
} | |
} | |
} | |
catch | |
{ | |
Write-Output "Unable to enmerate the SQL registry hive. Either SQL is not installed on $ComputerName, or remote access is denied to the registry on $ComputerName." | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment