Search code examples
powershellpowershell-module

How can I have a parent script but maintain separation of concerns with Powershell Scripts?


So I am working on some IIS management scripts for a specific IIS Site setup exclusive to a product to complete tasks such as:

- Create Site
- Create App Pool
- Create Virtual directories

The problem, is I would like to keep separate scripts for each concern and reference them in a parent script. The parent script could be ran to do a full deployment/setup. Or you could run the individual scripts for a specific task. The problem is that they are interactive, so they will request for the user information relevant to completing the task.

I am not sure how to approach the problem where each script will have a script body that will acquire information from the user, yet if it is loaded into the parent script avoid that specific's scripts script body from prompting the user.

NOTE: I know I could put them into modules, and fire off the individual "Exported to the environment" functions, but this script is going to be moved around to the environment that needs setup, and having to manually put modules (psm1) files into the proper PowerShell module folders just to run the scripts is a route I am not particularly fond of.

I am new to scripting with Powershell, any thoughts or recommendations?

Possible Answer*

This might be (a) solution: but I found I could Import-Modules from the working directory and from there have access to those exported functions.

I am interested in any other suggestions as well.


Solution

  • They way I would address it would be to implement a param block at the top of each sub script that would collect the information it needs to run. If a sub script is run individually the param block would prompt the user for the data needed to run that individual script. This also allows the parent script to pass the data needed to run the subscripts as the parent script calls the sub script. The data needed can be hard coded in the parent script or prompted for or some mixture thereof. That way you can make the sub scripts run either silently or with user interaction. You get the user interaction for free from Powershell's parameter handling mechanism. In the subscripts add a parameter attribute to indicate that Powershell will request those particular parameter values from the user if they are not already provided by the calling script.

    At the top of your sub scripts, use a parameter block to collected needed data.

    param
    (
        [parameter(Mandatory=$true, HelpMessage="This is required, please enter a value.")]
        [string] $SomeParameter
    )