Solved

How do I call powershell scripts from a config file

Posted on 2013-11-20
12
285 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
This article will help you understand what HashTables are and how to use them in PowerShell.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

758 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

19 Experts available now in Live!

Get 1:1 Help Now