Parsing @@Version output question

Hello,

I am using Powershell 2.0 with SQL snapin loaded and trying to get the SQL server Version, version number and Edition.

$ver = Invoke-Sqlcmd  -ServerInstance 'ServerName\InstanceName' -Query 'select @@version'
$ver

Result:
--------------------------------
Column1 : Microsoft SQL Server 2008 R2 (RTM) - 10.50.1600.1 (X64)
            Apr  2 2010 15:48:46
            Copyright (c) Microsoft Corporation
            Standard Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: ) (Hypervisor)
---------------------------------- 

My question is, how do I parse this single string output of four lines into

$SQLVersion = Microsoft SQL Server 2008 R2
$SQLVerNum = 10.50.1600.1
$SQLEdittion = Standard Edition (64-bit)
$Buil = Build 9200

I am assuming that i should be using RegEx, but not really getting anywhere (especially because I cant figure out how to look at a separate line

 

 

 

 

 

 

 

 

 

Parents
No Data
Reply
  • Try this variant:

     001 
     002 
     003 
     004 
     005 
     006 
     007 
     008 
     009 
     010 
     011 
     012 
     013 
     014 
     015 
     016 
     017 
     018 
     019 
    
    $ver = 
    @"
    --------------------------------
    Column1 : Microsoft SQL Server 2008 R2 (RTM) - 10.50.1600.1 (X64)
                Apr  2 2010 15:48:46
                Copyright (c) Microsoft Corporation
                Standard Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: ) (Hypervisor)
    ----------------------------------
    "@
     
    $SQLEdittion = [regex]::match($ver,'\n\s+(.+)\son').groups[1].value
    $SQLVersion = [regex]::match($ver,':\s([^(-]+)').groups[1].value
    $SQLVerNum = [regex]::match($ver,'\d+(\.\d+){3}').value
    $Build = [regex]::match($ver,'Build \d+').value
     
    $SQLEdittion
    $SQLVersion
    $SQLVerNum
    $Build
    

     

Children
No Data