2011-09-28 62 views

回答

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

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

+5

需要您登錄到該實例。不完全是本機PS –

+0

這適用於我,但生成的字符串被截斷。如何將整個(多行)結果作爲一個長(完整/完整/非截斷)字符串返回到PowerShell? – Mark

1

所有你需要的是連接到SQL Server和運行此查詢:

select @@version 

這當然會爲任何客戶端工具的工作。

此外,這也是可供選擇:

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

更多方式在這裏確定SQL Server版本:http://support.microsoft.com/kb/321185

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

顯然,代替 「」與您的實例的名稱。如果您想查看所有可用的方法,請轉至here

+0

這很好,因爲它允許您在腳本的其餘部分輕鬆使用版本號(或任何您想要的)。 –

15

只需使用註冊表選項,我發現它可以更快地在我的一些系統:


$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

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 
} 
+2

也許你的代碼做的更多一些? – cereallarceny

+0

希望編輯有幫助 – brendan62269

-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 
1

爲了增加布倫丹代碼..如果你的機器是64位的失敗,所以你需要適當的測試。

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) { 
    $SqlVer = New-Object PSObject 
    $SqlVer | Add-Member -MemberType NoteProperty -Name ServerName -Value $ComputerName 
    $base = "SOFTWARE\" 
    $key = "$($base)\Microsoft\Microsoft SQL Server\Instance Names\SQL" 
    $type = [Microsoft.Win32.RegistryHive]::LocalMachine 
    $regKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($type, $ComputerName) 
    $SqlKey = $regKey.OpenSubKey($key) 
    try { 
     $SQLKey.GetValueNames() 
    } catch { # if this failed, it's wrong node 
     $base = "SOFTWARE\WOW6432Node\" 
     $key = "$($base)\Microsoft\Microsoft SQL Server\Instance Names\SQL" 
     $regKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($type, $ComputerName) 
     $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("$($base)\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 
}