deadlydog
deadlydog

Reputation: 24404

Best practice for writing equivalent powershell scripts and functions

I have a script that takes in multiple parameters, and that I've documented with proper help comments (e.g. .SYNOPSIS, .DESCRIPTION, .PARAMETER). Several different users in my organization are going to use this powershell script, some who know powershell and will call it from powershell with specific parameter values, and some who don't know powershell and will simply right-click on the script file in Windows Explorer and choose Run with PowerShell (so the parameters will use their default values).

My conundrum is what is the best way to do this in powershell without a bunch of duplicate code. The way I see it, these are my options:

1 - Just write a DoStuff.ps1 script that provides default values for all parameters. This allows it to be ran directly from Windows Explorer, but feels clunky for the powershell users that want to use it as a function from their own scripts, since instead of writing:

Do-Stuff param1 param1

they will be doing:

.\DoStuff.ps1 param1 param2

2 - Within DoStuff.ps1, move the operations it performs into a DoStuff function, and after the function declaration call the DoStuff function with the parameters passed into the script. This would still allow the script to be ran from Windows Explorer, as well as developers to dot source the script into their own scripts so they can access the function. The downside is that when the developers dot source the script, the script is going to call the function with the default parameters (unless I allow them to provide an optional Switch parameter to the script that triggers the function to not be called). Even with this though, it means that I would have to duplicate all of the scripts help text so that it shows for both the script and the function (description, parameter descriptions, etc.).

I can't think of any other options. Ideally I would just be able to write functions in .ps1 file and tag a function with a "default" keyword so that if the script is called, that function is ran by default; but I don't think PowerShell provides anything like this.

What do you think is the best approach in this situation. Is there something I'm overlooking or don't know about? Thanks.

Upvotes: 1

Views: 686

Answers (1)

Richard
Richard

Reputation: 109070

but feels clunky for the powershell users that want to use it as a function from their own scripts

Default parameters would seem, based on your description, to be the best (or, at least, least-worse) approach.

But rather than naming your script DoStuff.ps1 name it and call it so it can be called more like an internal function:

  • Name it with the dash: Do-Stuff.ps1
  • Remember you don't need to specify the ps1
  • If the script is in a folder in $env:Path then you don't need to specify a path.

Also consider a script can load a module from a relative path: you could put most of the code in a script module which the front end (right click on it) script loads and calls into it. Script authors load the module themselves.

Upvotes: 3

Related Questions