如何使用Powershell检查SQL Server版本?

时间:2011-09-28 17:21:05

标签: sql sql-server powershell version

使用PowerShell检查SQL Server Edition和Version的最简单方法是什么?

9 个答案:

答案 0 :(得分:22)

只是使用注册表的一个选项,我发现它可以在我的某些系统上更快:


$inst = (get-itemproperty 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server').InstalledInstances
foreach ($i in $inst)
{
   $p = (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL').$i
   (Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\$p\Setup").Edition
   (Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\$p\Setup").Version
}

enter image description here

答案 1 :(得分:21)

Invoke-Sqlcmd -Query "SELECT @@VERSION;" -QueryTimeout 3

http://msdn.microsoft.com/en-us/library/cc281847.aspx

答案 2 :(得分:15)

[reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo") | out-null
$srv = New-Object "Microsoft.SqlServer.Management.Smo.Server" "."
$srv.Version
$srv.EngineEdition

显然,替换“。”与您的实例的名称。如果您想查看所有可用方法,请转到here

答案 3 :(得分:3)

从这个帖子(以及其他一些人)中获取建议,这是我的psprofile:

Function Get-SQLSvrVer {
<#
    .SYNOPSIS
        Checks remote registry for SQL Server Edition and Version.

    .DESCRIPTION
        Checks remote registry for SQL Server Edition and Version.

    .PARAMETER  ComputerName
        The remote computer your boss is asking about.

    .EXAMPLE
        PS C:\> Get-SQLSvrVer -ComputerName mymssqlsvr 

    .EXAMPLE
        PS C:\> $list = cat .\sqlsvrs.txt
        PS C:\> $list | % { Get-SQLSvrVer $_ | select ServerName,Edition }

    .INPUTS
        System.String,System.Int32

    .OUTPUTS
        System.Management.Automation.PSCustomObject

    .NOTES
        Only sissies need notes...

    .LINK
        about_functions_advanced

#>
[CmdletBinding()]
param(
    # a computer name
    [Parameter(Position=0, Mandatory=$true)]
    [ValidateNotNullOrEmpty()]
    [System.String]
    $ComputerName
)

# Test to see if the remote is up
if (Test-Connection -ComputerName $ComputerName -Count 1 -Quiet) {
    # create an empty psobject (hashtable)
    $SqlVer = New-Object PSObject
    # add the remote server name to the psobj
    $SqlVer | Add-Member -MemberType NoteProperty -Name ServerName -Value $ComputerName
    # set key path for reg data
    $key = "SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL"
    # i have no idea what this does, honestly, i stole it...
    $type = [Microsoft.Win32.RegistryHive]::LocalMachine
    # set up a .net call, uses the .net thingy above as a reference, could have just put 
    # 'LocalMachine' here instead of the $type var (but this looks fancier :D )
    $regKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($type, $ComputerName)

    # make the call 
    $SqlKey = $regKey.OpenSubKey($key)
        # parse each value in the reg_multi InstalledInstances 
        Foreach($instance in $SqlKey.GetValueNames()){
        $instName = $SqlKey.GetValue("$instance") # read the instance name
        $instKey = $regKey.OpenSubkey("SOFTWARE\Microsoft\Microsoft SQL Server\$instName\Setup") # sub in instance name
        # add stuff to the psobj
        $SqlVer | Add-Member -MemberType NoteProperty -Name Edition -Value $instKey.GetValue("Edition") -Force # read Ed value
        $SqlVer | Add-Member -MemberType NoteProperty -Name Version -Value $instKey.GetValue("Version") -Force # read Ver value
        # return an object, useful for many things
        $SqlVer
    }
} else { Write-Host "Server $ComputerName unavailable..." } # if the connection test fails
}

答案 4 :(得分:3)

试试这个

Invoke-SqlCmd -query "select @@version" -ServerInstance "localhost"

screen

检查所有可用的方法Get the build number of the latest Cumulative Update / Service Pack that has been installed in SQL Server

答案 5 :(得分:1)

您只需连接到SQL Server并运行此查询:

select @@version

当然,这适用于任何客户端工具。

此外,这也是可用的:

SELECT SERVERPROPERTY('productversion'), 
       SERVERPROPERTY ('productlevel'), 
       SERVERPROPERTY ('edition')

在此处确定SQL Server版本的更多方法:http://support.microsoft.com/kb/321185

答案 6 :(得分:1)

要添加到Brendan的代码中......如果您的计算机是64位,则会失败,因此您需要进行适当的测试。

vars := hello world
something-all:
    $(foreach VAR,$(vars),something)

答案 7 :(得分:1)

这是我从这里和那里*的一些来源拼凑而成的版本。

此版本不会在注册表中运行,也不会在SQL中运行,甚至不需要实例在运行。它确实要求您知道实例名称。如果您不知道实例名称,则应该可以通过此代码轻松地将其计算出来。

要使其正常工作,请用实例名称替换“ YourInstanceNameHere”。如果这样做不起作用,请勿触摸$

$ErrorActionPreference = "Stop"
$instanceName = "MSSQL`$YourInstanceNameHere"

$sqlService = Get-Service -Name $instanceName

$WMISQLservices = Get-WmiObject -Class Win32_Product -Filter "Name LIKE 'SQL Server % Database Engine Services'" | Select-Object -Property Name,Vendor,Version,Caption | Get-Unique

foreach ($sqlService in $WMISQLservices)
{
    $SQLVersion = $sqlService.Version
    $SQLVersionNow =  $SQLVersion.Split("{.}")
    $SQLvNow = $SQLVersionNow[0]
    $thisInstance = Get-WmiObject -Namespace "root\Microsoft\SqlServer\ComputerManagement$SQLvNow"  -Class SqlServiceAdvancedProperty | Where-Object {$_.ServiceName -like "*$instanceName*"}  | Where-Object {$_.PropertyName -like "VERSION"}
}

$sqlServerInstanceVersion = $thisInstance.PropertyStrValue

if ($sqlServerInstanceVersion)
{
    $majorVersion = $thisInstance.PropertyStrValue.Split(".")[0]
    $versionFormatted = "MSSQL$($majorVersion)"
}
else
{
    throw "ERROR: An error occured while attempting to find the SQL Server version for instance '$($instanceName)'."
}

$versionFormatted

**我也得到了我的这个https://stackoverflow.com/users/1518277/mqutub朋友的帮助,也得到了我的帮助,我不希望它被取消信用。

答案 8 :(得分:-1)

嗯,这是旧学校的方式,这很简单:

sqlcmd -Q "select @@version;"

以下是我在Serverspec中使用它的方式:

require 'windows_spec_helper'

describe 'MS SQL Server Express' do
  describe service('MSSQLSERVER') do
    it { should be_enabled }
    it { should be_running }
  end
  describe port(1433) do
    it { should be_listening }
  end
  describe command('sqlcmd -Q "select @@version;"') do
    its(:stdout) { should match /Microsoft SQL Server 2008 R2 (SP2) - 10.50.4000.0 (X64)/ }
  end
end