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?

MattB
  • 585
  • 4
  • 17
  • 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`](https://learn.microsoft.com/en-US/dotnet/api/System.Management.Automation.Language.Parser), where you create a single script by (recursively) replacing script-file calls with `& { } args...`, though note that this may have side effects. – mklement0 May 30 '21 at 14:12
  • Another - equally cumbersome - option is to package the contents of the nested scripts as entries of a [`DATA` section](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Data_Sections), 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). – mklement0 May 30 '21 at 14:23

0 Answers0