用Powershell来检测当前SQL Server的版本

本篇将介绍如何使用Powershell来检测当前SQL Server的版本。

检测SQL Server版本主要是利用注册表中获取的值来判定。

然后通过之前设置好的SQL Server想对应的版本,来输出当前SQL Server的版本。

本篇仅仅用SQL Server 2012来做例子,之前的版本号,可以再微软的官网查询。

if(Test-Path 'HKLM:SOFTWAREMicrosoftMicrosoft SQL Server'){
"This server has SQL Server installed, The following are the service pack"
$sqlPath = join-path (resolve-Path -Path 'HKLM:SOFTWAREMicrosoftMicrosoft SQL Server*.MSSQLSERVER')

MSSQLSERVERCurrentVersion
$sqlVersion = (Get-ItemProperty -Path $sqlPath -Name CurrentVersion).currentversion
switch($sqlVersion){
"11.0.5058.0" {"SQL Server 2012 Service Pack 2"}
"11.00.3000.00" {"SQL Server 2012 Service Pack 1"}
"11.00.2100.60" {"SQL Server 2012 RTM"}
default {"The version cannot be determinated"}
}
}else{
"SQL Server is not installed on the server"
}