尝试使用 powershell 从 Windows 服务器获取本地 SQL 服务器详细信息

问题描述 投票:0回答:0

尝试使用以下格式的 PowerShell 获取本地 SQL Server 详细信息。

Name                           Value                                                    
----                           -----                                                    
Instance                       MSSQLServer                                              
InstanceNameFullName           MSSQL11.MSSQLServer                                          
Version                        11.0.3000.0                                              
Edition                        Standard Edition                                         
MajorVersion                   SQL Server 2012  

我正在尝试使用查询获取 MajorVersion。但它无法在一个 sql server 上获取多个命名实例的 sql 详细信息。

下面的代码会帮助你。

function ListSQLInstances {
    $listinstances = New-Object System.Collections.ArrayList

    #registry
    $installedInstances = (get-itemproperty 'HKLM:\SOFTWARE\Microsoft\Microsoft SQLServer').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


    # Getting the SQL SERVER 20XX value
    $trimmedVersion  = Invoke-SqlCmd -query "select @@version" -ServerInstance "MSSQL11.MAHESH"
    $fullVersion = $trimmedVersion.Column1
    $MajorVersion = $fullVersion.Substring(10, 16)

    # Adding SQL server data to a custom object___
    $instance = [PSCustomObject]@{
        Instance             = $i
        InstanceNameFullName = $instancefullname;
        Edition              = (Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\$instancefullname\Setup").Edition;
        Version              = $productversion;
        MajorVersion         = $MajorVersion;
    }
    $listinstances.Add($instance)
}
sql-server powershell registry
© www.soinside.com 2019 - 2024. All rights reserved.