Building a Robot for the future
Is there a God?
Britain in the past
Reality is there if you agree or not!
Why its time to get off the property ladder
How do you think?
Wage Slave – its not a smart soundbite, its actually true
Theory of the age of civilisation
Powershell – changing parameters at the file name
How to view a government petition breakdown using Powershell

Powershell – changing parameters at the file name

Powershell is for scripting, but that doesn’t mean you can get away with sloppy coding.  You should never write more than you need to and you need to be able to reuse as much as possible. This means you write small little modules that do a few things, rather than one huge script that only works for one task.

A great example is something like today’s task. I needed to change some permissions in a folder. The problem was, to expand on the task, I would need to feed in the rest of the folders I needed to work on, but this would not allow me to run things in parallel.

Ideally, I needed to have a task schedule where the same script worked on 3 different folders at once. The only way to do that is to trigger the Task Scheduler 3 times. But using that process, I would need 3 different scripts – which breaks the first rule, never write more than you need.

The answer is quite easy – feed the name of the new folder into the script at launch.  So, at the top line of the script, add:

param(
[string]$unique
)

This will create a variable you can place where you need it in the real code.

write-host "This is the magic " $unique

The way you add the details is like this:

./MyScript.ps1 "Beans"

This is very flexible now, and I can create 3 task scheduled processes that all triggered the same time, but deal with 3 unique folders, using the same code.
How easy was that?

Leave a Reply

Your email address will not be published. Required fields are marked *