How do I call powershell scripts from a config file

I have several sites that I maintain and use powershell scripts to  keep me up to date with what is going on at the different machines.  Problem is that none of them are configured exactly the same so I have to customize the main scripts at each site to fit their need.

I would like to change that by having a config file with only the names of the scripts that are needed for that site and just have one script that calls that config file to call all of the required scripts

I've looked all over and don't see anything that points me in the right direction.  Can you either show me how to do it or point me to a site that details the implementation for me.


Thanks
c7c4c7Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

asavenerCommented:
Create a folder that contains the scripts you want to run (Called "plugins" here):

$ScriptPath = <somepath>
$PluginsFolder = $ScriptPath + "\Plugins\"
$Plugins = Get-ChildItem -Path $PluginsFolder -filter "*.ps1" | Sort Name

Foreach ($plugin in $Plugins) {
            Invoke-Settings -Filename $plugin.Fullname
      }
0
footechCommented:
What do you need to customize?  Just variables?

Sorry but I'm not understanding what you're looking to do.
0
c7c4c7Author Commented:
footech - As an example not all machine have the same backup software, so rather than modify a machines script to check either for all backup software or just a specific software I  want to be able to create a single script that will call different scripts based on that machines needs.

 asavener - I'll check that out and get back to you

Thanks
0
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Using a folder only containing the necessary scripts for each machine is a good idea. Of course you could also put all scripts including the caller script into the same folder, and exclude the own name from being executed again. The current script (with full path) is stored in $myInvocation.get_MyCommand().Definition. so checking the file name against that allows to skip the own script.

Or you use the same naming convention for the "satellite" scripts, like all starting with 'maint-'.
0
c7c4c7Author Commented:
asavener- As I understand your implementation you will put all scripts for a site in a folder and then execute all scripts that are in that folder.

That's not what I had in mind when I asked the question.  I don't want to have to worry about what script goes where, I load all scripts on every machine.  That way I don't have to keep track if the right script is on the correct machine.

Currently I have a directory of current scripts when I make changes to a script, or add one, I just copy the whole directory to every machine and I know all machines are up to date.

I currently have a driver script that blindly call all of the other scripts, but because not all machines are configured the same the results of some scripts can make it look like there is a problem when there isn't.  

With all scripts loaded on all machines I want the driver script to read a config file, that contains just the names of the scripts I want to run on that machine and driver script will only run those scripts.  When I run the daily scripts if I don't need one anymore I remove its name from the file and also if I need to start running a script I can just add the name to the file.  

No more erratic results
0
footechCommented:
OK, I think I'm finally seeing what you want to do.
If your config file is just a list of which files you want to run like
run1.ps1
run2.ps1
run3.ps1

Open in new window

The following will parse the config file and execute each line.
Get-Content configfile.txt | % {Invoke-Expression .\$_}

Open in new window

0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
IMHO there isn't much of a difference between the "execute all scripts in a customized folder" and "execute all scripts I have enumerated" in regard of making mistakes. If you introduce a new script, you will now push it to all machines (ok), but then need to remember to include it in all or the relevant machines' config files.
Further, it doesn't make sense to me to have a copy of all scripts on all machines. Use a central repository server instead, and let the caller script do the work! I even can imagine to have a single, central config file including either a machine-script or script-machine relation.
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
And if you want to maintain your idea of one config file per machine, no issue with that, but I definitely would use config files including ALL scripts with a boolean to mark those to execute. That way you can always check for added scripts you might have not configured to run yet.
0
asavenerCommented:
Another option would be to create an array with the names/paths of the scripts you want to run.  Create a list of all scripts, and then comment out the ones you don't want to run.

You could use a variable for the path, if you don't always put the scripts in the same path.


Another option is a CSV file with the scripts you want to run.  Just import the list into an array.
0
c7c4c7Author Commented:
Qlemo - I like your addition of the Boolean please give an example building on what footech has all ready submitted
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
$base = 'C:\Scripts\Admin\'
@"
run,script
x,diskhealth
-,cleanup
"@ | convertfrom-csv | # this is for test only
  ? { $_.run -eq 'x' } |
  % { & $(join-path $base ($_.script+'.ps1')) }

Open in new window

Instead of the here-string and convertfrom-csv you would use a file of same format as the string contents and import-csv «configfile», of course.
$base = 'C:\Scripts\Admin\'
@"
script,     machines
diskhealth, "pc1,pc2"
cleanup,    "pc2"
"@ | convertfrom-csv | # this is for test only
  ? { $_.machines -split ',' -contains $env:COMPUTERNAME } |
  % { & $(join-path $base ($_.script+'.ps1')) }

Open in new window

works with enumerating the machine names the script should run on.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
c7c4c7Author Commented:
Thanks for the help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.