PowerShell - List all SQL instances on my system?

后端 未结 7 2035
一生所求
一生所求 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:51

    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
    }
    

提交回复
热议问题