Solved

How do I call powershell scripts from a config file

Posted on 2013-11-20
12
286 Views
Last Modified: 2013-11-30
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
0
Comment
Question by:c7c4c7
  • 4
  • 4
  • 2
  • +1
12 Comments
 
LVL 28

Expert Comment

by:asavener
ID: 39664074
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
 
LVL 39

Expert Comment

by:footech
ID: 39664508
What do you need to customize?  Just variables?

Sorry but I'm not understanding what you're looking to do.
0
 

Author Comment

by:c7c4c7
ID: 39665105
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
 
LVL 68

Expert Comment

by:Qlemo
ID: 39667345
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
 

Author Comment

by:c7c4c7
ID: 39673724
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
 
LVL 39

Assisted Solution

by:footech
footech earned 250 total points
ID: 39673757
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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 68

Expert Comment

by:Qlemo
ID: 39673982
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
 
LVL 68

Expert Comment

by:Qlemo
ID: 39673984
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
 
LVL 28

Expert Comment

by:asavener
ID: 39674586
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
 

Author Comment

by:c7c4c7
ID: 39676668
Qlemo - I like your addition of the Boolean please give an example building on what footech has all ready submitted
0
 
LVL 68

Accepted Solution

by:
Qlemo earned 250 total points
ID: 39677263
$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
 

Author Closing Comment

by:c7c4c7
ID: 39687493
Thanks for the help.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
A brief introduction to what I consider to be the best editor for PowerShell.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now