#Finding files based on $PSScriptRoot or taking a path parameter

13 messages · Page 1 of 1 (latest)

tidal geyser
#

I’m building a couple of PS scripts to automate tasks in a repository and was wondering whether I could get some opinions on this:

Should I accept paths to important files as parameters, or derive them based on where I know the script is in the repository?

The former is more resistant to the script being moved and could allow for ‘dry runs’ if writing to a temp directory, but the latter is easier to just ‘double click and run’, albeit requiring the script to be updated if the repo is reorganised.

plain stump
#

you could just make $PSScriptRoot a default value for a parameter if it is not supplied

tidal geyser
#

I guess that would be the best of both worlds

#

Then I wonder if the CI that runs this script should use the parameters or not 😛

#

Maybe not, to ensure the defaults are kept updated

plain stump
#

If it wanted something portable I would define a bunch of defaults with the ability to override. The override might be explicit params, but more likely a config file in a relative path if I wanted to do my CI pipeline just one time.

tidal geyser
#

Is it best to use $PSDefaultParameterValues for this by the way?

plain stump
#

🤷‍♂️ you can... I very rarely do, but don't let me stop you

tidal geyser
#

Haha no I’m genuinely unsure

plain stump
#

if you've a use for it, but except for convenience in the shell it's all a bit too magic when you get to debugging things imo

tidal geyser
#

on my phone so excuse the formatting but

param (

[String]
$SomePath = Join-Path “a” “b”

)

Appears to be a syntax error

plain stump
#

needs parens

#
param ( 
    [String]
    $SomePath = (Join-Path “a” “b”)
)