Single Unifying Script

I started learning PowerShell a couple years ago and during that time I had a mentor who taught a specific method for scripting that basically wrapped all functions into a single script…(C# junky) Now it’s been a couple years and I have enjoyed using it a lot instead of having a million scripts sitting around. However, I’m curious about possible draw backs and possibly to see if there is a better way to go about it. Maybe i’m just breaking all types of rules here. Or it might not matter at all. Generally the code format is as follows :

param
( [string]$cmd, [string]$p1, [string]$p2, [string]$p3, [switch]$p4, .....)

function testfunction1 ($inParam1, $inParam2) {}

function testfunction2 ($inParam, $inParam2, $inParam3) {}

try
{
   Invoke-Expression $cmd $p1 $p2 $p3
}
catch
{
   Write-Error "Sorry no such function exists"
}

Then outside of the XXXX.ps1, I can go ahead and call any of the functions as such : xxxx.ps1 testfunction1 “some Value” “Some value again”. This provides the ability to call script functions, and then those functions can call other functions etc… Without doing any type of Module Importing / Dotsourcing.

Any thoughts?

Just my opinion: What’s wrong with importing modules? Since Powershell 3 you don’t even have to think about how and which one. They load aoutomaticly if you placed them to the right folder.

The main concerns I have seen about modules are as follows :

  • Modules would seem to add a lot of extra overhead with manifests, and the need to transport the extra items.
  • How do modules work when running various scripts in parallel. With the removal/importing of modules and making sure that one script doesn’t affect the next scripts version of the module.

So you have not formed your own opinion? Why don’t you try it with a small test module? I would expect modules to be easier maintainable than a giant single script with everything you need. I even used to have single script files for each single function I use in a module. And the script files have the name of the function they contain. Usually, my actual module file looks like this:

Get-ChildItem -Path $psscriptroot*.ps1 | ForEach-Object { . $_.FullName }

I don’t know about any side effect when you run functions of modules in parallel. And if you don’t use Powershell before version 2.0 you don’t need to import or remove them explicitly.