Sqlserver
 sql >> Database >  >> RDS >> Sqlserver

PowerShell - Elencare tutte le istanze SQL sul mio sistema?

Ho scoperto che (almeno per me) nessuno dei precedenti ha restituito la mia istanza SQL Express. Ho 5 istanze denominate, 4 SQL Server full-fat, 1 SQL Express. I 4 full-fat sono inclusi nelle risposte sopra, SQL Express no. COSÌ, ho fatto un po' di ricerche su Internet e mi sono imbattuto in questo articolo di James Kehr, che elenca le informazioni su tutte le istanze di SQL Server su una macchina. Ho usato questo codice come base per scrivere la funzione di seguito.

# 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
}