2

I have asp.net mvc application, for which database connection string is placed in Environment variable DbConn as

Min Pool Size=20;Application Name=***;**initial catalog=*****;server=***;Integrated Security=True;

Now my question is how to read this connection string using Powershell script. I know how to read the entire connection string Get-ChildItem Env:DBConn,but I need a portion of it like the initial catalog,server etc.

Please guide me in resolving this.

1
  • Deleting after getting valid anwer? What was that? Commented Oct 27, 2015 at 17:13

2 Answers 2

2

You could parse and tokenize the connection string with -split, Trim() and a hashtable:

$DBConnValues = @{}
$env:DBConn.Trim(";") -split ";" |ForEach-Object { 
    $key,$value = $_ -split "="
    $DBConnValues[$key] = $value
}

Now you can access each property by name:

PS C:\> $DBConnValues["Server"]
ServerName
Sign up to request clarification or add additional context in comments.

Comments

1

This would be a perfect case for ConvertFrom-StringData so that you can make it into a custom object.

$props = $env:DBConn -replace ";","`r`n" | ConvertFrom-StringData
$dbConn = New-Object -TypeName psobject -Property $props

ConvertFrom-StringData wants a string where each line is a key value pair. We get just that by replacing the semicolons with newlines. It returns a hashtable which we feed to New-object to get a custom PowerShell object. Then you can use the new object like you would any in PowerShell.

$dbConn."Min Pool Size"

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.