PowerShell, where can I set global settings?

We are creating a larger project using PowerShell (a set of scripts / functions that helps us set up SharePoint environments or tenants).

Many functions must reuse settings that are stored in one central location.

I could not find the “best practice” of how such a file / location of settings is best created and structured.

My idea is to store global settings in a separate file (module file), for example Settings.psm1 with content like this:

# Set vars
$global:scriptEnvironment = "SP2016HOSTINGDEV"
$global:logFileName = "z_Migration_to_SP2016.log"
$global:languageMapping = @{
    "en-US" = 1;
    "de-DE" = 2;
}
$global:oldWsps = @(
    [WspFile]@{ Filename = "comapany.solution.wsp"; IsDeployable = $true; IsGloballyDeployable = $false; FullTrustBinDeployment = $false },
    [WspFile]@{ Filename = "company.solution2.server.wsp"; IsDeployable = $true; IsGloballyDeployable = $false; FullTrustBinDeployment = $false }
)
...

And in other modules / scripts, I could always include settings like this:

# Set vars
$scriptDirectory = Split-Path -parent $PSCommandPath

# Module import
Import-Module (Join-Path $scriptDirectory Settings.psm1) -Force -ErrorAction Stop
Import-Module (Join-Path $scriptDirectory Functions.psm1) -Force -ErrorAction Stop

# Functions
...

Thus, this will allow me to use global settings like this in functions inside other script files:

Function WriteLog
{
    param
    (
        [System.String]$LogString = "",
        [System.String]$LogLevel = ""
    )
    WriteLogToPath -LogFileName $global:logFileName -LogString $LogString -LogLevel $LogLevel
}

? , , ?

+4
3

, , / . , .

- :

$modulename = Split-Path $PSScriptRoot -Leaf

$default_foo = 'something'
$default_bar = 'or other'
...

function Get-RegistrySetting($name) {
    $path = "HKCU:\Software\${script:modulename}"

    if (-not (Test-Path -LiteralPath $path)) {
        New-Item -Path $path -Force | Out-Null
    }

    try {
        Get-ItemProperty $path -Name $name -ErrorAction Stop |
            Select-Object -Expand $name
    } catch {
        $val = Get-Variable -Scope script -Name "default_${name}" -ValueOnly -ErrorAction Stop
        Set-ItemProperty $path -Name $name -Value $val
        $val
    }
}

$global:foo = Get-RegistrySetting 'foo'
$global:bar = Get-RegistrySetting 'bar'
...

, , script global.

+1

. . (.. .psd1, - , ) "root" RootModule.

RootModule, .

"" ( ), PowerShell , .

, .

0

Get-Configuration Powershell. , , ( JSON) .

(dir env:PSGetConfiguration).Value
{
    "Mode":  "Xml",
    "XmlPath":  "C:\\Managers\\PSGetConfiguration.xml"
}

cat $(Get-ConfigurationSource).XmlPath
<Configuration>
  <conf key="ExampleKey" value="ExampleValue" />
  <conf key="key" value="Value123" />
  <conf key="remove" value="remove" />
</Configuration>

Get-Configuration Set-Configuration

Set-Configuration -Key k1 -Value v1
Get-Configuration -Key k1
v1

Powershel XML Configuration

When the module starts, XML is saved in the module directory, it can be changed by manually changing the environment variable or using the Set-XmlConfigurationSource command

SQL configuration

By default, the module uses XML to store data, but the second option is to store data in SQL. Setting up the configuration is quite simple:

Set-SqlConfigurationSource -SqlServerInstance ".\sql2017" -SqlServerDatabase "ConfigDatabase" -SqlServerSchema "config" -SqlServerTable "Configuration"

After that, our config will be saved in the SQL table.

SQL configuration

Code is also available on github .

0
source

Source: https://habr.com/ru/post/1690686/


All Articles