Search code examples
powershellazure-sql-databasessas

How do i return compatibility level of just one database?


I referenced this page: https://technet.microsoft.com/en-us/hh213141(v=sql.100)

Import-Module SqlServer

$as = New-Object Microsoft.AnalysisServices.Server  
$as.connect("$Server")  

$as.databases

Write-Host "Compatibility level ="$as.DefaultCompatibilityLevel

but this returns ALL databases back...

I want to specify just one database to get the compatibility level of...

I tried this,

$as.databases["$Database"]

the DB i am interested in has lvl 1103, but it seems not to return the proper level, because its returning 1200...


Solution

  • I am not familiar with this API, but I see that the $as.Databases property is of type DatabaseCollection. The DatabaseCollection class does have indexers that take either an Int32 or a String, like you're attempting to use with $as.databases["$Database"].

    Note that the documentation for the String indexer says the parameter is an "identifier" for the database to be returned. Also note that the Database class has separate properties for ID and Name, so there is a distinction between the two. So, my suggestion is, make sure you are passing the ID, not the name, when you try to retrieve a Database instance that way.

    Alternatively, if you do want to search by name you could use the FindByName method...

    $as.Databases.FindByName($Database)
    

    ...or GetByName method...

    $as.Databases.GetByName($Database)
    

    ...with the difference being that the former returns $null if no such database exists and the latter throws an exception.

    If all else fails you could retrieve the desired database like this...

    $as.Databases | Where-Object { $_.ID -eq $Database }
    

    ...or like this...

    $as.Databases | Where-Object { $_.Name -eq $Database }
    

    ...depending on which property corresponds to the value in $Database.

    Finally, in your code you are attempting to access a DefaultCompatibilityLevel property, which I don't see defined in the Database class. There is, however, a CompatibilityLevel property.