dataplat / dbatools

🚀 SQL Server automation and instance migrations have never been safer, faster or freer

Home Page:https://dbatools.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Copy-DbaCredential issue when name of credential contains square brackets

ibaxo opened this issue · comments

Verified issue does not already exist?

I have searched and found no existing issue

What error did you receive?

Getting an error:
Failed Exception calling "Query" with "1" argument(s): "Exception calling "ExecuteWithResults" with "1" argument(s): "Execute w...

When trying to use Copy-DbaCredential on credentials created by/for replication, which contains square brackets '[', ']' in their name for example on a credential with name like:
[REPL][XXX][XXXX]

image

Steps to Reproduce

1.) Create a credential on MSSQL which does contain square brackets '[', ']' in it's name, for example:
[REPL][MyDomain\TestAccount][ServerName-ReplicationName]
2) Execute Copy-DbaCredential with source as the server containing mentioned credential.

Please confirm that you are running the most recent version of dbatools

2.1.7

Other details or mentions

Problem seems to be laying in Copy-DbaCredential, Line: 220
$destServer.Query("CREATE CREDENTIAL [$sqlcredentialName] WITH IDENTITY = N'$identity', SECRET = N'$password' $cryptoSQL")

There are used square brackets in front and after $sqlcredentialName instead of using proper QUOTENAME function for escaping.

What PowerShell host was used when producing this error

Windows PowerShell (powershell.exe)

PowerShell Host Version

Name Value


PSVersion 5.1.20348.2110
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.20348.2110
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1

SQL Server Edition and Build number

Microsoft SQL Server 2022 (RTM-CU8) (KB5029666) - 16.0.4075.1 (X64)

.NET Framework Version

PSChildName Version


Client 4.8.04161
Full 4.8.04161
Client 4.0.0.0

Created pull request:
#9243

Not sure if it's correct fix, it's just proposed fix, which works for me.

Because:
$decryptedCredentials items already contain a property .Quotename which is escaped correctly by QUOTENAME function.
One possible fix could be to use the value from .Quotename property instead of $credentialName variable

Referenced commit:
https://github.com/dataplat/dbatools/compare/development...ibaxo:issue/9242?expand=1#diff-7768bb5b72caf8c08848cb0a46f4d9e59e17bc1e41fd35800befda1d57863ffb