1

I have the following connection string

{"Authentication Kind":"UsernamePassword","Username":"someID1","Password":"Yu#gh456!ts","EncryptConnection":true}

I am trying to show the password with ******* , so I need to convert it to SqlConnectionStringBuilder type as its easy to replace properties with that. I am using it just fine for non-json structured strings

[System.Data.SqlClient.SqlConnectionStringBuilder]::New('{"Authentication Kind":"UsernamePassword","Username":"someID1","Password":"Yu#gh456!ts","EncryptConnection":true}')

i am getting this error:

Cannot convert value to type System.Data.SqlClient.SqlConnectionStringBuilder

How do I convert it to a connection string thats acceptable by the SqlConnectionStringBuilder type?

4
  • Do you ultimately need a SqlConnectionStringBuilder instance, or are you just using it for structured access to / display of the input string's fields? Commented Jul 9, 2019 at 3:19
  • @mklement0 well i am mainly concerned with eventually using the connection string in this function here: stackoverflow.com/a/56944292/8397835 it works for regular strings just fine, such as "Connection Timeout=120;User Id=UID1;Data Source=datasource.com;Password=password12!553;". so i need to somehow convert that JSON structure to something similar so that it would work with the function. the connection string is generated from the Credential class here: docs.microsoft.com/en-us/dotnet/api/… Commented Jul 9, 2019 at 3:34
  • A format transformation to a ;-separated list of key-value pairs is not enough in your case, because the JSON contains property (field) names that SqlConnectionStringBuilder doesn't recognize, such as Username. Commented Jul 9, 2019 at 4:39
  • @mklement0 hmm, is there another stringbuilder for this kind of structure? i would understand if there isnt, as the JSON structure is new to 2017 server databases Commented Jul 9, 2019 at 4:45

1 Answer 1

2

You could convert it to a PSObject first.

$Json = '{"Authentication Kind":"UsernamePassword","Username":"someID1","Password":"Yu#gh456!ts","EncryptConnection":true}'
$Sql = $Json | ConvertFrom-Json
$Sql.Password

From there you can use the properties to create a new string, or convert it back to JSON.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.