Skip to content

Using .Databases with a misspelled database name changes database name in SMO #115

@andreasjordan

Description

@andreasjordan

This bug was found using dbatools: dataplat/dbatools#8598

There are two ways to select a single database from the list of databases from a Microsoft.SqlServer.Management.Smo.Server object: $server.Databases | Where-Object Name -EQ <dbname> and $server['<dbname>'].

The first one works without problems. But the second one changes the casing of the name from the original one to the one used to query the database.

Here is the code to show that:

Import-Module -Name dbatools

# correct:
$server = [Microsoft.SqlServer.Management.Smo.Server]::new('sql01')
$server.Databases | Where-Object Name -EQ Master | Select-Object -ExpandProperty Name
$server.Databases['Master'] | Select-Object -ExpandProperty Name

# wrong:
$server = [Microsoft.SqlServer.Management.Smo.Server]::new('sql01')
$server.Databases['Master'] | Select-Object -ExpandProperty Name
$server.Databases | Where-Object Name -EQ Master | Select-Object -ExpandProperty Name

And here is a screenshot:

image

Yes, I'm using not the current version - but I don't have the current version at hand. But I think it will take you only minutes to test this in your test environment. If it is fixed with the current version: Sorry to bother you.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions