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?

How to view a government petition breakdown using Powershell

In the UK we have a petition system where anyone can set up a petition and if it reaches 100K names, the subject gets debated in Parliament. Its an idea that gets misused by various lobby groups and there are often news stories where some political idea gets forced into debate by a group who can’t accept the status quo.

Anyway, enough politics, the interesting part is getting to the numbers.  Take the last big petition – all about Brexit. Each petition has a JSON page where all those lovely numbers are easy to see – but while JSON is easier to read than XML, its no Infographic!

https://petition.parliament.uk/petitions/131215.json

So how are we going to read this? Powershell!

Its only two lines, but its very good.

$datapetition = Invoke-WebRequest -uri “https://petition.parliament.uk/petitions/131215.json” | ConvertFrom-Json
$datapetition.data.attributes.signatures_by_constituency

The first line reads the data, and after the pipe, the variable becomes a Powershell object. The second line grabs the attribute we need. All you need to do is to read it.

$datapetition.data.attributes – this is where the magic is!

Unlike other site that will spoon feed you, I am not adding any more – just experiment and see all the data show up!

 

 

Proof of the past is under your feet

People like to travel. It has always been true, and is the reason we no longer live in Africa as a species. The easiest way from one place to another is via a path. If enough people use that path and it becomes a road. Eventually, the people will spend their own time and effort to make their road easier to use.  It gets straighter and flatter and more damage resistant over time. This is the life of a road.

So when did we first start using roads?  In the UK, we were always taught it was all due to the Romans.  They brought the roads and built them nice and straight from one town to another. As these roads wore out, we rebuilt them and made them better. A great example is the A5 from London to Holyhead in Anglesey.  A real Roman road that still exists and is full of traffic every day. The problem is that Roman road isn’t actually Roman.

There is a story that the road was built by King Belinus, who was a King of the Britons around 400 BC. His brother, Brennus was said to have sacked Rome with the Gauls in 390 BC. However, this was practically 2500 years ago, so any evidence has gone through a number of hands so reliability is hard to gauge.

This map is a good example of how Roman roads looked

Click on the picture to visit site for more details

Notice how those black lines follow the same paths as modern roads. The A5 is obvious, but so is the A1 and the A4.  You can almost see the A49.

Hopefully this is a good introduction into a subject that interests me a great deal.

If the Romans came to Britain and found a bunch of roads ready for them, how far back do we need to look into history before there were no roads?  Secondly, who did build the first British roads?

Welcome 2017

This is the first post of my new blog. I’ve hosted blogs before.  I like to share ideas but have never given the publishing element enough effort.  That changes this time.

I have two ideas that keep my attention.

One is how old is civilisation in the west?  When did we really start to live how we do today?  Where is the evidence of how we used to live? How much of what we think we know about the past is true?  How can we prove the past without physical evidence or written documents?

My other idea is cutting edge technology. Just how will the future pan out and what is the cutting edge?  I love concepts like virtual services, robots, big data and other technologies that people talk about, but not many people actually do.

I aim to post on both these ideas under the banner of Old and New.

I hope you enjoy my ideas and look forward to reading your comments