PowerShell - List all SQL instances on my system?

后端 未结 7 2072
一生所求
一生所求 2021-02-19 15:04

Is there a Powershell command to list all SQL instances on my system? (MS SQL 2008)

7条回答
  •  遇见更好的自我
    2021-02-19 15:57

    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
    }
    

提交回复
热议问题