2

When combining multiple Powershell scripts together as indicated by @mklement0 in his answer on Converting Multiple Powershell Scripts to EXE, is there a way to take into account nested script calls?

For example, I have "base" powershell scripts that handle REST API calls, and build request headers for more complex API calls. Then I have my main script that takes all of that work and builds out a procedural script. Similar to include/using statements, I call the needed scripts from the top of my main script and pass in base variables that are needed.

Example:

MainScript.ps1

. .\Base\WebRequests.ps1 -BaseUri $BaseUri -Token $Token   
Invoke-BaseWebRequest -Variable1 $Variable1 -Variable2 $Variable2

\Base\WebRequests.ps1

param(
[String] $BaseUri,
[String] $Token
)

function Get-CustomRequest{
param(
[String] $Variable1,
[String] $Variable2
)
process{
script code
}

When performing the combining, is there a non-convoluted way to take into account the "include" statements and remove them so that it will bundle the scripts in proper procedural order in order that the functions can be used from the main script?

2
  • Such a feature would be nice, but it's a nontrivial problem to solve, and also support calls to functions from non-built-in modules it is probably infeasible. For nested script-file calls only, you may be able to build a solution via PowerShell's language parser, System.Management.Automation.Language.Parser, where you create a single script by (recursively) replacing script-file calls with & { <script-file-content> } args..., though note that this may have side effects. Commented May 30, 2021 at 14:12
  • Another - equally cumbersome - option is to package the contents of the nested scripts as entries of a DATA section, which you then extract to files at runtime - but creating aux. script files on the target machine may be undesirable (conceivably, the main script could switch to a temporary location and clean up after itself). Commented May 30, 2021 at 14:23

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.