0

We have some config parms in our BizTalk config file. I'd like to access them in PowerShell using System.ConfigurationManger, rather than writing a custom function to parse the xml and do it.

I'm trying what is described in this blog: https://www.c-sharpcorner.com/blogs/load-and-read-config-files-in-powershell

I might be hitting what this describes: ConfigurationManager.AppSettings getting null?

cls 
$configPath = "d:\Program Files (x86)\Microsoft BizTalk Server 2016\BTSNTSvc.exe.config"
[appdomain]::CurrentDomain.SetData("APP_CONFIG_FILE", $configpath)
$verifyConfig = [appdomain]::CurrentDomain.GetData("APP_CONFIG_FILE")
Write-Host "verifyConfig=$verifyConfig" 

$resultObj = [System.Configuration.ConfigurationManager]::AppSettings["Office365_Token_ClientID"]

$resultObj.GetType

if ($resultObj -eq $null) 
   {
       Write-Host "Result = null" 
   } 
else 
   {
       $result = $resultObj.ToString()
       write-host "Result = $result" 
   }

Trying to pick up this value:

  <add key="Office365_Token_ClientID" value="xxx" /> 

The result is the "Result = null" from the Write-Host.

NealWalters
  • 17,197
  • 42
  • 141
  • 251

1 Answers1

0

This is an entirely different approach, but got it working, and is also a minimal amount of code:

$configPath = "d:\Program Files (x86)\Microsoft BizTalk Server 2016\BTSNTSvc.exe.config"
$xml = [xml] (Get-Content $configPath)
$result = $xml.SelectSingleNode('//add[@key="Office365_Token_ClientID"]').Value
Write-Host "Result=$result"

Later I wrote this function to better handle a missing key/value pair:

function getConfigKey($key) 
{
    Write-Host "function: getConfigKey key=$key" 
    $configPath = "d:\Program Files (x86)\Microsoft BizTalk Server 2016\BTSNTSvc.exe.config"
    $xml = [xml] (Get-Content $configPath)
    $configObj = $xml.SelectSingleNode("//add[@key='$key']")

    if ($configObj -eq $null) 
    {
        # end with an error if any config value is missing 
        Throw "No key found for config key=$key in $configPath" 
    }
    return $configObj.Value 
}

It can be called like this:

$clientID = getConfigKey "Office365_Token_ClientID"
NealWalters
  • 17,197
  • 42
  • 141
  • 251