- <#
- .SYNOPSIS
- This script displays the versions of SQL Server running on a system
- .DESCRIPTION
- This script uses WMI to get the SQLServiceAdvancedProperty class from
- the ComputerManagement namespace to print out the versions. This
- script is an adaptation of the VBS script on MSDN.
- .NOTES
- File Name : Get-SQLServerVersion.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell V2 CTP3
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/ms186353.aspx
- .EXAMPLE
- PSH [C:\foo]: .\Get-SQLVersion.ps1'
- You are running the following versions of SQL:
- Service Name Version
- ------------ -------
- MSSQL$MICROSOFT##SSEE 9.3.4035.00
- MSSQL$SQLEXPRESS 9.3.4035.00
- #>
- ##
- # Start of script
- ##
- # Get the versions of SQL from WMI
- $Versions = Get-WmiObject -Namespace root\Microsoft\SQLServer\computerManagement -Class SqlServiceAdvancedProperty | where {$_.SqlServiceType -eq 1 -and $_.PropertyName -eq "VERSION"}
- # Now display results
- "You are running the following versions of SQL:"
- "Service Name Version"
- "------------ -------"
- foreach ($version in $versions) {
- "{0} `t{1}" -f $version.servicename,$version.propertystrvalue
- }
- #End of script
This blog contains PowerShell scripts, more PowerShell scripts and still more PowerShell scripts. Occasionally you may see some organisational posts.
The WMI provider for SQL Server only works against SQL 2005 or higher. Another option that will support SQL Server 2000 is to use SMO
ReplyDeleteparam ($sqlserver)
[reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo") | out-null
$server = new-object ("Microsoft.SqlServer.Management.Smo.Server") $sqlserver | select name, @{name='Version';Expression={$_.information.version}}
Thanks for stopping by Martha and thanks for your kind words. Suggestions etc always welcome. I hope you enjoy the ride!
ReplyDeleteHi Thomas, nice blog! You've got a lot of useful scripts here.
ReplyDeleteHow do you format your script code so it looks so nice? I post some Powershell scripts on my Wordpress blog (http://www.sharepointnomad.com), but the text wraps around and it's difficult to copy & paste...
Andre
Andre,
ReplyDeleteThank you for your kind words. Regarding the syntax highlighting, I use the page: http://www.thecomplex.plus.com/highlighter.html, It works OK, although the syntax colouring is based on early Monad, and does not support the V2 features, Longer scripts sometimes hangs (in which case I break the script up and cut/paste each part manually into Live Writer.
Hope this helps
Thomas