PowerShell - 列出我系统上的所有 SQL 实例?

发布于 2024-12-05 20:17:49 字数 58 浏览 2 评论 0原文

是否有 Powershell 命令可以列出我的系统上的所有 SQL 实例? (微软 SQL 2008)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

自控 2024-12-12 20:17:49

只是另一种方法...可以比 SQLPS 更快一点来获得快速答案。


(get-itemproperty 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server').InstalledInstances

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
む无字情书 2024-12-12 20:17:49

导入 powershell sql server 扩展:

 Import-Module SqlServer 

然后执行以下命令

Set-Location SQLSERVER:\SQL\localhost
Get-ChildItem

Import powershell sql server extensions:

 Import-Module SqlServer 

Then do these commands

Set-Location SQLSERVER:\SQL\localhost
Get-ChildItem
分開簡單 2024-12-12 20:17:49

我发现(至少对我来说)以上都没有返回我的 SQL Express 实例。我有 5 个命名实例、4 个全功能 SQL Server、1 个 SQL Express。上面的答案中包含了 4 个全脂,而 SQL Express 则没有。所以,我在互联网上做了一些挖掘,发现了 这篇文章,作者:James Kehr,其中列出了有关计算机上所有 SQL Server 实例的信息。我使用这段代码作为编写下面函数的基础。

# get all sql instances, defaults to local machine, '.'

Function Get-SqlInstances {

  Param($ServerName = '.')

  $LocalInstances = @()
  [array]$Captions = Get-WmiObject win32_service -ComputerName $ServerName |
    where {
      $_.Name -match "mssql*" -and 
      $_.PathName -match "sqlservr.exe"
    } |
      foreach {$_.Caption}

  foreach ($Caption in $Captions) {
    if ($Caption -eq "MSSQLSERVER") {
      $LocalInstances += "MSSQLSERVER"
    } else {
      $Temp = $Caption |
        foreach {$_.split(" ")[-1]} |
          foreach {$_.trimStart("(")} |
            foreach {$_.trimEnd(")")}

      $LocalInstances += "$ServerName\$Temp"
    }

  }

  $localInstances
}

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 = Get-WmiObject win32_service -ComputerName $ServerName |
    where {
      $_.Name -match "mssql*" -and 
      $_.PathName -match "sqlservr.exe"
    } |
      foreach {$_.Caption}

  foreach ($Caption in $Captions) {
    if ($Caption -eq "MSSQLSERVER") {
      $LocalInstances += "MSSQLSERVER"
    } else {
      $Temp = $Caption |
        foreach {$_.split(" ")[-1]} |
          foreach {$_.trimStart("(")} |
            foreach {$_.trimEnd(")")}

      $LocalInstances += "$ServerName\$Temp"
    }

  }

  $localInstances
}
月依秋水 2024-12-12 20:17:49
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SqlWmiManagement") | out-null
$mach = '.'
$m = New-Object ('Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer') $mach
$m.ServerInstances
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SqlWmiManagement") | out-null
$mach = '.'
$m = New-Object ('Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer') $mach
$m.ServerInstances
书间行客 2024-12-12 20:17:49

System.Data.Sql 命名空间包含支持 SQL Server 特定功能的类。

通过使用 System.Data.Sql 命名空间,您可以在 Windows Power shell 中使用以下命令获取计算机上的所有 MSSQL 实例:
[System.Data.Sql.SqlDataSourceEnumerator]::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()

世界等同你 2024-12-12 20:17:49
$a = "MyComputerName"

[System.Data.Sql.SqlDataSourceEnumerator]::Instance.GetDataSources() |
  where { $_.servername -eq $a}

Aaron 方法返回更确定的响应。
请阅读此处了解 Instance.GetDataSources ()

$a = "MyComputerName"

[System.Data.Sql.SqlDataSourceEnumerator]::Instance.GetDataSources() |
  where { $_.servername -eq $a}

Aaron method return a more sure response.
Read Here about Instance.GetDataSources()

红颜悴 2024-12-12 20:17:49

此函数将返回所有已安装的实例,并在对象列表中包含版本详细信息:

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
}

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
}
明月夜 2024-12-12 20:17:49

如果您希望列表包含实例使用的附带网络端口,您可以尝试此...

$MsSqlSvc = Get-CimInstance Win32_Service -Filter 'Name like "mssql*"'

$PortList = Get-NetTcpConnection | select -Unique LocalPort, OwningProcess

$SqlPorts = $MsSqlSvc | foreach {
  $ThisSvc = $_
  $PortList | where { $_.OwningProcess -in $ThisSvc.ProcessId }
}

$SqlPorts | select OwningProcess,
  @{
    l = 'SqlInstance';
    e = {
      $ThisPort = $_
      $MsSqlSvc | where { $_.ProcessId -eq $ThisPort.OwningProcess } | 
        select -ExpandProperty Name
    }
  },
  LocalPort

If you like the list to contain accompanying network ports used by the instances, you can try this...

$MsSqlSvc = Get-CimInstance Win32_Service -Filter 'Name like "mssql*"'

$PortList = Get-NetTcpConnection | select -Unique LocalPort, OwningProcess

$SqlPorts = $MsSqlSvc | foreach {
  $ThisSvc = $_
  $PortList | where { $_.OwningProcess -in $ThisSvc.ProcessId }
}

$SqlPorts | select OwningProcess,
  @{
    l = 'SqlInstance';
    e = {
      $ThisPort = $_
      $MsSqlSvc | where { $_.ProcessId -eq $ThisPort.OwningProcess } | 
        select -ExpandProperty Name
    }
  },
  LocalPort
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文