C
C
Chronic 862018-07-22 16:28:54
PowerShell
Chronic 86, 2018-07-22 16:28:54

powershell script configuration?

Hello.
There is a script written in powershell, what are the options for storing its configuration?
What is more convenient in practice?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
ApeCoder, 2018-07-22
@ApeCoder

Environment variables, registry, config next to the file or in the profile (as a config, you can simply put a powershell script with assignments and variables and execute it)

A
azarij, 2018-07-22
@azarij

in practice, it will be more convenient to use the fact that the powershell can parse natively.
json, clixml, csv and xml partially.

S
strangerror, 2018-07-26
@strangerror

An example of storing a configuration in xml.

Script
function getConfig ($configFile){
    if (Test-Path ((Get-Location).Path + "\$configFile")){
        $config = ([xml]$(Get-Content $configFile)).configuration
        if ($? -and $config){
            return $config
        }
        else{
            Write-Host 'Config file is incorrect'
        }
    }
    else {
        Write-Host 'Config file not found'
    } 
}

function init {
    $files = $configuration.logFiles.File
    foreach ($file in $files){
        New-Item -Path ($(Get-Location).Path + "\Logs\$(Get-Date -Format 'yyyy')\$(Get-Date -Format 'MM')\$(Get-Date -Format 'dd')") -Name $file.value -ItemType File -Force | Out-Null
    }
}

$ErrorActionPreference = 'SilentlyContinue'
$configFile = 'config.xml'
$configuration = getConfig ($configFile)
init

config
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<configuration>
  <logFiles>
    <File key="Full" value="logFull.log"/>
    <File key="Warning" value="logWarning.log"/>
    <File key="Error" value="logError.log"/>
  </logFiles>
</configuration>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question