2011-09-28 25 views

Odpowiedz

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

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

+5

wymaga, aby zalogować się do instancji. nie do końca natywna PS –

+0

Działa to dla mnie, ale wynikowy ciąg jest obcięty. W jaki sposób uzyskać cały wynik (wielowierszowy) z powrotem do PowerShell jako jeden długi (pełny/kompletny/nieobcinany) ciąg? – Mark

1

Wszystko, co potrzebne jest, aby połączyć się z SQL Server i uruchomić tę kwerendę:

select @@version 

To, oczywiście, będzie pracować dla każdego narzędzia roboczego.

Dodatkowo, jest również dostępna:

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

więcej sposobów, aby określić wersję SQL Server tutaj: "" 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 

Oczywiście, należy wymienić z nazwą instancji. Jeśli chcesz zobaczyć wszystkie dostępne metody, wejdź na here.

+0

To jest świetne, ponieważ pozwala na łatwe użycie numeru wersji (lub cokolwiek chcesz) w pozostałej części skryptu. –

15

Tylko opcja korzystania z rejestru, znalazłem to może być szybciej na niektóre z moich systemów:


$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 się informacje z tego wątku (i kilka innych), to poszło w moim 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

Może bardziej szczegółowo opisać, co robi twój kod? – cereallarceny

+0

Mam nadzieję, że edycja pomogła – brendan62269

-1

Cóż, tutaj jest to stary sposób szkoła, to proste:

sqlcmd -Q "select @@version;" 

A oto jak go używać z 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

Aby dodać do kodu Brendana .. to się nie powiedzie, jeśli urządzenie jest 64-bitowy, więc trzeba odpowiednio przetestować.

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 
} 
Powiązane problemy