Classes/BcAppSettings.ps1
class BcAppSettings{ hidden [System.Object] $_ServerInstance # Parent object [BCServerInstance] BcAppSettings( [System.Object] $ServerInstance ){ $this._ServerInstance = $ServerInstance } hidden [hashtable] GetBcAppSettings(){ [scriptblock] $scriptBlock = { param( [string] $ServerInstance, [string] $ServicePathName ) # Get the Business Central Server Instance config file path from the service. $regex = 'config\s"(?<Config>.*?)"' $match = $ServicePathName | Select-String -Pattern $regex $bcsConfigPath = $match.Matches[0].Groups['Config'].Value if($bcsConfigPath){ if(Test-Path $bcsConfigPath){ # Get the ServerInstance appSettings config file path from the BCS config [XML] $bcsConfig = Get-Content -Path $bcsConfigPath $bcsAppSettingsConfigPath = $bcsConfig.configuration.appSettings.file if($bcsAppSettingsConfigPath -eq 'CustomSettings.config'){ $bcsAppSettingsConfigPath = Join-Path ($bcsConfigPath | Split-Path -Parent) $bcsAppSettingsConfigPath } } } if($bcsAppSettingsConfigPath){ if(Test-Path $bcsAppSettingsConfigPath){ # Read Business Central appSettings and convert the keys and values to hashtable [XML] $bcsAppSettingsConfig = Get-Content -Path $bcsAppSettingsConfigPath $bcsAppSettings = @{} foreach($key in $bcsAppSettingsConfig.appSettings.add){ $bcsAppSettings += @{($key.key.ToString()) = $key.value} } } } if(-not $bcsAppSettings){ 'Could not find the AppSettings for ServerInstance {0}' -f $serverInstance | Write-Warning return } return $bcsAppSettings } $additionParams = @{} if(-not $this._ServerInstance._isLocalService -or $this._ServerInstance._remotePsEnabled -eq $true){ $additionParams = @{'ComputerName' = $this._ServerInstance._ComputerName} } $result = Invoke-Command @additionParams -ScriptBlock $scriptBlock -ArgumentList ` @($this._ServerInstance.ServerInstance, $this._ServerInstance._BcService.PathName) return $result } [System.Object] Search ( [string] $filter ){ # Add wildcards if not pressent if($filter -notmatch '\*'){ $filter = '*' + $filter + '*' } return $this._ServerInstance.Appsettings | Select-Object -Property ('{0}' -f $filter) } [void] Help ( [string] $keyName ){ $regex = '<!--[\r\n]+(?<helpText>([^>])*)-->[\r\n]\s+(?:<add key=")(?:{0}")' -f $keyName $customSettingsPath = Join-Path ($this._ServerInstance.GetInstallationFolder()) 'CustomSettings.config' $helpTextMatch = Get-Content -Path $customSettingsPath -Raw | Select-String -Pattern $regex | Select-Object -ExpandProperty Matches if($helpTextMatch){ $helpTextMatch.Groups['helpText'].Value.Replace(' ','') | Write-Host return } 'Help text for Business Central configuration key ''{0}'' not found.' -f $keyName | Write-Warning } hidden [void] SaveAppSetting ( [string] $Key, [string] $Value ){ [scriptblock] $scriptBlock = { param( [string[]] $BcModulesPath, [string] $ServerInstance, [string] $Key, [string] $Value, [string] $PreviousValue ) $BcModulesPath | Import-Module -Force "Updating setting '{0}' from '{1}' to '{2}' on ServerInstance {3}." -f $Key, $PreviousValue, $Value, $ServerInstance | Write-Host Set-NAVServerConfiguration -ServerInstance $ServerInstance -KeyName $Key -KeyValue $Value -Force } $additionParams = @{} if(-not $this._ServerInstance._isLocalService -or $this._ServerInstance._remotePsEnabled -eq $true){ $additionParams = @{'ComputerName' = $this._ServerInstance._ComputerName} } Invoke-Command @additionParams -ScriptBlock $scriptBlock -ArgumentList ` @($this._ServerInstance.GetBcPsModules(), $this._ServerInstance.ServerInstance, $Key, $Value, $this._ServerInstance._AppSettings.$Key) } } |