使用powershell檢查SQL Server版本和版本的最簡單方法是什麼?如何使用Powershell檢查SQL Server版本?
回答
Invoke-Sqlcmd -Query "SELECT @@VERSION;" -QueryTimeout 3
所有你需要的是連接到SQL Server和運行此查詢:
select @@version
這當然會爲任何客戶端工具的工作。
此外,這也是可供選擇:
SELECT SERVERPROPERTY('productversion'),
SERVERPROPERTY ('productlevel'),
SERVERPROPERTY ('edition')
更多方式在這裏確定SQL Server版本:http://support.microsoft.com/kb/321185
[reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo") | out-null
$srv = New-Object "Microsoft.SqlServer.Management.Smo.Server" "."
$srv.Version
$srv.EngineEdition
顯然,代替 「」與您的實例的名稱。如果您想查看所有可用的方法,請轉至here。
這很好,因爲它允許您在腳本的其餘部分輕鬆使用版本號(或任何您想要的)。 –
只需使用註冊表選項,我發現它可以更快地在我的一些系統:
$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
}
從這個線程(以及其他一些)砍死了意見,這又在我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
}
也許你的代碼做的更多一些? – cereallarceny
希望編輯有幫助 – brendan62269
嗯,這裏是老派的方式,這很簡單:
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
爲了增加布倫丹代碼..如果你的機器是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
}
試試這個
Invoke-SqlCmd -query "select @@version" -ServerInstance "localhost"
當你想分享代碼或命令時,你有沒有聽說過粘貼TEXT?這是l33t。 –
- 1. PowerShell:使用Powershell檢查Firefox版本
- 2. 如何使用TSQL檢查數據庫的SQL Server版本?
- 3. 如何檢查SQL Server的版本是什麼?
- 4. 如何在wix中檢查sql版本
- 5. Azure/Powershell:檢查SQL Server是否存在
- 6. 如何使用Autoconf檢查Linux版本?
- 7. 如何使用C++檢查python版本?
- 8. 如何配置SQL Server 2008使用PowerShell?
- 9. 檢查是否安裝了SQL Server(任何版本)?
- 10. 檢索當前的SQL Server版本
- 11. sql server檢索表的版本
- 12. 使用powershell查找IIS版本
- 13. 如何檢查LS版本
- 14. 如何檢查版本號?
- 15. 如何檢查libpng版本
- 16. 如何檢查Spark版本
- 17. 如何檢查NHibernate版本?
- 18. 如何檢查enyo版本?
- 19. 如何檢查和比較Powershell中的Python版本
- 20. SQL Server版本號
- 21. SQL Server版本612,665?
- 22. 你如何檢查現有使用PowerShell
- 23. 使用SQL Server解決行版本化
- 24. 使用JDBC識別SQL Server版本
- 25. 要使用哪個SQL Server 2008版本
- 26. 如何版本SQL Server數據庫?
- 27. 如何定位多的SQL Server版本
- 28. 查找但SQL Server數據庫版本
- 29. 如何使用PowerShell查找MSI產品版本號?
- 30. 檢查SQL Server數據庫上的託管程序集版本
需要您登錄到該實例。不完全是本機PS –
這適用於我,但生成的字符串被截斷。如何將整個(多行)結果作爲一個長(完整/完整/非截斷)字符串返回到PowerShell? – Mark