Freigeben über


I have just run the MAP tool to inventory SQL and the report shows some SQL Servers with Unknown Edition

You have just inventoried your SQL Server Environment with the MAP Tool https://blogs.msdn.com/b/markm/archive/2015/02/17/inventorying-sql-servers-with-the-microsoft-assessment-and-planning-toolkit-map.aspx and the MAP tool reports several SQL Servers have an "Unknown" edition.

The MAP tool uses WMI to collect the information about SQL and there are times especially on older Operating Systems when WMI does not return the SQL Server Edition.

You could manually connect via SQL Server Management Studio to each of the machines the MAP tool reported as having an Unknown edition and do a select @@Version or select serverproperty('edition'). Depending on the number of machines you have with Unknown edition this could be a very tedious and time consuming task.

You could write something to programmatically connect to each of those machines and query for Edition information which is the route I have gone.

The following PowerShell script will parse an input file containing the machine name and instance name if the machine has a named instance and query that machine for Product Name, SP level and Edition:

The script will also take an output file and return results in the file. If no output file is specified the results are displayed on your screen.

To prepare you input file open the SQLServerAsessment Report from the MAP tool and go to the DatabaseInstances tab. Filter on Unknown Editions so that the sheet only contains Machines with Unknown Editions. Copy the Server Name and Instance Name columns and paste them into notepad. For machines with MSSQLSERVER as the instance name, remove the instance name. For machines with MSSQL$ and the instance name, remove the MSSQL$ so that the line in notepad is servername\instancename. You can run the script with parameters or uncomment out the declaration of $infile and $ outfile and hardcode the file names in the script. If you omit the output file name the results are displayed on your screen.

The script will login using your windows credentials so you will need to run the script using credentials that have the ability to query SQL for edition information.

$infile = $args[0]
$outfile = $args[1]
 
#$infile = ".\servers2.txt"
#$outfile = ".\outfile.csv"
 
if(!$infile)
    {
    write-host
        ""
        "INVALID ARGUMENT"
        ""
        "GetSQLProperties has two arguments"
        ""
        "The first argument is for a file containing a list of SQL Servers to be Queried"
        "The second argument if for an output file containing the results of the query in a comma delimited format"
        "If the second argument is ommitted the results will be displayed in your powershell window"
        ""
        "The format of the input file is as follows:"
        ""
        "Server1"
        "Server2"
        "Server3"
        "Server3\namedinstance"
        "Server4"
        "Server5"
        ""
        "Usage:"
        ""
        "GetSQLProperties c:\filepath\infile.txt"
        "GetSQLProperties c:\filepath\infile.txt c:\filepath\outfile.txt"
        ""
        ""
    }
Else
    {
    $Servers = Get-Content $infile
 
    Foreach ($value in $Servers)
 
    {
        $pversion = ""
        $plevel = ""
        $pedition = ""
 
         try
            {
            $con = new-object System.Data.SqlClient.SqlConnection "Server=$value;Pooling=false;Integrated Security=true"
            $con.Open()
 
            $cmd = $con.CreateCommand()
            $cmd.CommandText = "
                     SELECT SERVERPROPERTY('productversion') productversion,
                            SERVERPROPERTY('productlevel') productlevel,
                            SERVERPROPERTY('edition') edition
                     "
           
            $rdr = $cmd.ExecuteReader()
            $rdr.Read()
                $pversion = $rdr[0]
                $plevel =     $rdr[1]
                $pedition =  $rdr[2]
            $rdr.Close();
 
       
            $con.Close()
       
            if(!$outfile)
                {write-host $value, $pversion, $plevel, $pedition}
            Else
                {"{0}","`t","{1}","`t","{2}","`t","{3}" -f $value,$pversion,$plevel,$pedition | out-file $outfile -Append}
            }
         Catch
            {
                if(!$outfile)
                    {Write-host -f red $value, " Could not connect"}
                Else
                    {"{0}","`t","Could Not Connect" -f $value | out-file $outfile -Append}
            }
    }
}