Solved

How do I call powershell scripts from a config file

Posted on 2013-11-20
12
291 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 40

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
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

 
LVL 70

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 40

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
 
LVL 70

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 70

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 70

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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

A procedure for exporting installed hotfix details of remote computers using powershell
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

632 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