Answer the question
In order to leave comments, you need to log in
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
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)
in practice, it will be more convenient to use the fact that the powershell can parse natively.
json, clixml, csv and xml partially.
An example of storing a configuration in xml.
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
<?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 questionAsk a Question
731 491 924 answers to any question