PowerShell - List all SQL instances on my system?
Is there a Powershell command to list all SQL开发者_如何学编程 instances on my system? (MS SQL 2008)
Just another way of doing it...can be a little quicker than SQLPS to get a quick answer.
(get-itemproperty 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server').InstalledInstances
Import powershell sql server extensions:
Import-Module SqlServer
Then do these commands
Set-Location SQLSERVER:\SQL\localhost
Get-ChildItem
I found that (for me at least) none of the above returned my SQL Express instance. I have 5 named instances, 4 full-fat SQL Server, 1 SQL Express. The 4 full-fat are included in the answers above, the SQL Express isn't. SO, I did a little digging around the internet and came across this article by James Kehr, which lists information about all SQL Server instances on a machine. I used this code as a basis for writing the function below.
# get all sql instances, defaults to local machine, '.'
Function Get-SqlInstances {
Param($ServerName = '.')
$localInstances = @()
[array]$captions = gwmi win32_service -computerName $ServerName | ?{$_.Name -match "mssql*" -and $_.PathName -match "sqlservr.exe"} | %{$_.Caption}
foreach ($caption in $captions) {
if ($caption -eq "MSSQLSERVER") {
$localInstances += "MSSQLSERVER"
} else {
$temp = $caption | %{$_.split(" ")[-1]} | %{$_.trimStart("(")} | %{$_.trimEnd(")")}
$localInstances += "$ServerName\$temp"
}
}
$localInstances
}
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SqlWmiManagement") | out-null
$mach = '.'
$m = New-Object ('Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer') $mach
$m.ServerInstances
$a = "MyComputerName"
[System.Data.Sql.SqlDataSourceEnumerator]::Instance.GetDataSources() | ? { $_.servername -eq $a}
Aaron method return a more sure response. Read Here about Instance.GetDataSources()
The System.Data.Sql namespace contains classes that support SQL Server-specific functionality.
By using the System.Data.Sql
namespace you can get all MSSQL instances on a machine using this command in windows power shell:
[System.Data.Sql.SqlDataSourceEnumerator]::Instance.GetDataSources()
This function it gonna return all the installed instances with the version details in a object list:
function ListSQLInstances {
$listinstances = New-Object System.Collections.ArrayList
$installedInstances = (get-itemproperty 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server').InstalledInstances
foreach ($i in $installedInstances) {
$instancefullname = (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL').$i
$productversion = (Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\$instancefullname\Setup").Version
$majorversion = switch -Regex ($productversion) {
'8' { 'SQL2000' }
'9' { 'SQL2005' }
'10.0' { 'SQL2008' }
'10.5' { 'SQL2008 R2' }
'11' { 'SQL2012' }
'12' { 'SQL2014' }
'13' { 'SQL2016' }
'14' { 'SQL2017' }
'15' { 'SQL2019' }
default { "Unknown" }
}
$instance = [PSCustomObject]@{
Instance = $i
InstanceNameFullName = $instancefullname;
Edition = (Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\$instancefullname\Setup").Edition;
ProductVersion = $productversion;
MajorVersion = $majorversion;
}
$listinstances.Add($instance)
}
Return $listinstances
}
$instances = ListSQLInstances
foreach ($instance in $instances) {
Write-Host $instance.Instance
}
精彩评论