2011-09-28 26 views

Trả lời

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

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

+5

đòi hỏi bạn phải đăng nhập vào ví dụ. không chính xác bản địa PS –

+0

Điều này làm việc cho tôi nhưng chuỗi kết quả bị cắt ngắn. Làm thế nào để có được toàn bộ (multiline) kết quả trở lại vào PowerShell như là một chuỗi dài (đầy đủ/hoàn thành/không cắt ngắn)? – Mark

1

Tất cả bạn cần là để kết nối với SQL Server và chạy truy vấn này:

select @@version 

này, tất nhiên, sẽ làm việc cho bất kỳ công cụ khách hàng.

Ngoài ra, điều này cũng có sẵn:

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

Nhiều cách để xác định phiên bản SQL Server ở đây: "" 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 

Rõ ràng, thay thế với tên của trường hợp của bạn. Nếu bạn muốn xem tất cả các phương pháp có sẵn, hãy truy cập here.

+0

Điều này thật tuyệt vì nó cho phép bạn dễ dàng sử dụng số phiên bản (hoặc bất kỳ thứ gì bạn muốn) trong phần còn lại của tập lệnh. –

15

Chỉ cần một tùy chọn sử dụng registry, tôi đã tìm thấy nó có thể được nhanh hơn trên một số hệ thống của tôi:


$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

Hacked lên lời khuyên từ chủ đề này (và một số người khác), điều này đã đi trong psprofile tôi:

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

Có thể xây dựng thêm một chút về mã của bạn làm gì? – cereallarceny

+0

hy vọng chỉnh sửa sẽ giúp – brendan62269

-1

Vâng, đây là cách học cũ, đó là dễ dàng:

sqlcmd -Q "select @@version;" 

Và dưới đây là cách tôi sử dụng nó từ 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

Để thêm mã Brendan của .. này không thành công nếu máy của bạn là 64-bit, vì vậy bạn cần phải kiểm tra một cách thích hợp.

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 
} 
Các vấn đề liên quan