I have a bunch of PowerShell scripts that are kicked off via scheduled tasks at various times.
The script are run multiple times with different arguments. Also administrators unfamiliar with scripting needed to be able to change the parameters without have to edit the code.
Due to this requirement, I passed the parameters in via arguments to powershell.exe in scheduled tasks. But this immediately became unwieldy, because now to change a script's parameters you have to go into task scheduler and edit the args to powershell.exe which now look like this (actually even longer):
-command "& 'C:\some\file\path\' -param1 'C:\some\file\path\' -param2 'C:\soawme\fawdile\pawawasth\' -param3 'C:\some\fisdfle\pasdfth\' -param4 'some arg'"
So now what I want to do is have each script just take in one editable configuration file where the parameters can be changed by the administrators. I could also organize the parameters more easily - have a "global" parameters configuration file all scripts use and then script-specific configuration files.
I thought I would use JSON in the configuration files and was thinking about doing something like this:
{
"folder1": [
"string",
"C:\\sldks\\dsf\\sdf\\sdf\\sd\\fsdf\\"
],
"folder2": [
"string",
"C:\\jiji\\sfef\\igig\\igg\\"
],
"CSSFile": [
"string",
"\\\\some\\netqwork\\path\\"
],
"DBServer": [
"string",
"myserver"
],
"DB": [
"string",
"DB"
],
"SqlQuery": [
"string",
"SELECT * FROM myTable"
],
"UID": [
"string",
"root"
],
"PWD": [
"string",
"123456"
]
}
$jsonObject = ConvertFrom-Json (cat $PathToMyExternalJsonFilePassedInFromTaskShedualer)
function Set-ParamType ($jsonNode) {
switch ($jsonNode[0])
{
'string' {return [string]$jsonNode[1]}
'int' {return [int]$jsonNode[1]}
'switch' {return [switch]$jsonNode[1]}
default {"Debug: Unknown type"}
}
}
$folder1 = Set-ParamType($jsonObject.folder1)
$folder2 = Set-ParamType($jsonObject.folder2)
$CSSFile = Set-ParamType($jsonObject.CSSFile)
$DBServer = Set-ParamType($jsonObject.DBServer)
$DB = Set-ParamType($jsonObject.DB)
$SqlQuery = Set-ParamType($jsonObject.SqlQuery)
$UID = Set-ParamType($jsonObject.UID)
$PWD = Set-ParamType($jsonObject.PWD)
This way, in the scheduled task I have only one argument to pass in (the path of the configuration file). This seems to work, but I wanted to ask if there was a better and saner way to accomplish my goals. Is there something foolish about this approach I'm not seeing?
Is there something foolish about this approach I'm not seeing?
Having admins "unfamiliar with scripting" change your script parameters seems pretty foolish to me, yes.