0

I have an Azure SQL database in one resource group and a key vault in a separate resource group. Everything is already deployed but I'd like to assign the connection string values of the sql db to a variable and user it to create a secret in my key vault resource. I currently have this set as the connection string value:

$dbsecretvalue = ConvertTo-SecureString -String "Server=tcp:$sqlServerfqdn,1433;Initial Catalog=$database;Persist Security Info=False;User ID=$adminlogin;Password=$password;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30" -AsPlainText -Force

The problem is in key vault this additional text is being added to the secret value instead of just the server name: Server=tcp:@{FullyQualifiedDomainName=servername.azure.net......

How do I go about removing the additional text so only the FQDN of my sql db is returned when I set the variable (ex: databaseserver.azure.net instead of @{FullyQualifiedDomainName=databaserver.azure.net)? I have very limited powershell experience so any help/advice would be appreciated.

Thanks!

asked Dec 8, 2020 at 16:21
2
  • I'm not entirely sure what you are trying to do. Are you trying to remove the partial string from the $sqlServerfqdn variable or are you trying to prevent the variable from getting the ":@{FullyQualifiedDomainName" added in the first place? If the second option, could you please share how you are setting that variable. Commented Dec 9, 2020 at 17:29
  • Here's how I set the variable: $sqlserverfqdn = Get-AzSqlServer -ResourceGroupName $resourceGroup -ServerName $sqlServer | Select FullyQualifiedDomainName. This returns a 'FullyQualifiedDomainName' header with the name of my db listed under it. I only want the name of the database returned. When I create the secret in my vault (see cmd above for how I create secret) '@{FullyQualifiedDomainName=' is also being added which isn't a valid connection string. I need '@{FullyQualifiedDomainName=' removed so only server name host name (fdqn) is listed/returned. Hope this helps. Commented Dec 9, 2020 at 19:15

1 Answer 1

1

There are a couple ways to do this. The simplest is going to be to break this up into two lines:

$server = Get-AzSqlServer -ResourceGroupName $resourceGroup -ServerName $sqlServer
$sqlserverfqdn = $server.FullyQualifiedDomainName
$sqlserverfqdn
#output databaserver.azure.net

This command is fine, and in general Powershell will try to make the JSON into an object by default, but once in awhile piping into ConvertFrom-Json will get the data loaded into an object you can manipulate that way

If you need to clean a string, then the replace method will usually handle it:

$fqdn = '@{FullyQualifiedDomainName=databaserver.azure.net'
$sqlserverfqdn = $fqdn -replace "@{FullyQualifiedDomainName=",""
answered Dec 10, 2020 at 3:28
Sign up to request clarification or add additional context in comments.

1 Comment

This worked perfect, thank you for the help!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.