Powershell – What on earth are you returning?

Powershell – What on earth are you returning?

I’ve finished a ‘minor’ project where I extended my previous Powershell script seen here – https://www.kennethsutherland.com/shell/copying-files-using-powershell-sd-card-to-pc/

What’s new and what did I solve?

I’ve got a Sony RX10-IV and one of its drawbacks is that is doesn’t have a GPS unit inside it. This is a major bummer as far as I’m concerned. So what to do? I searched, read various guides and yet nothing simple jumped out to solve my issue. Ideally I wanted a device to attach to it’s hot-shoe that when I took photos it would write GPS data to the photo. There is nothing, not a single thing that is suitable for the Sony camera.

Before anyone comments, why not use Sony’s phone software to sync up – it’s a huge pile of 💩💩💩 Not getting into why, it just is! PlayMemories and Imaging Edge Mobile are both virtual paperweights. Ready to be binned on my phone now.

So I happen to have an eTrex 30 for when I go hillwalking and as soon as you turn it on, it starts recording it’s location and time. This is stored in a *.gpx file. Sorted! All I need to do is to merge the gpx file with my photos. Sounds simple. Actually nope, took me far to long…

Functions that return way more than you expect!

So you can create functions in Powershell – good.

You create the function flow, and in order to make sure it’s working as expected you put in some echo commands. Great so far.

NO, NO, NO – do not use echo commands inside a method that you are going to return something. It completely MESSES it up!

Some code ->

Look at the below screenshot while debugging some code 👇

If you look at the above image 👆 you can see that the method getSelectedPoint returns the selected point. But it also has a echo.
Inside the debug tooltip you can see that the returned object is more than the point, it’s also got the echo statement!
WTF Powershell! Never seen a language return more than what’s specified to return.

Now check out the same but without the echo

Look at the same debug tooltip when the echo isn’t there. That returned point is just a point.

WARNING – I don’t know Powershell

Must add this warning, as I’m sure those with experience will call out the reasons for the above and no doubt issues with my code. But as a novice in powershell I’m highlighting the issues I stumbled on to get the end result. This took to long to figure out. HTH.

If you’ve got this far and want to know more then head along to this – About Return This is a MS guide about powershell and what it returns. Essentially if you want a standard programming style return, then you can use a class.

2 Replies to “Powershell – What on earth are you returning?”

  1. Hello Kenneth!
    Thanks for this and for the useful material on github.
    Three part comment.
    1) in PowerShell (PS) “echo” is an alias for “Write-object”, which puts things on the output pipeline. For debugging, you need Write-Host or Write-Debug as these put things on the console. I’m using “things” because PS is object orientated and everything in PS is an “object”.
    2) I use Andreas Schneider’s “Geotag” to match photos to GPX data. Because it can show you the image, it makes it easy to set the time correctly if you use your camera to photograph a UTC clock on your phone. I wish I had thought of that 8-{
    3) In case people get the idea that it is difficult to handle GPX in PS, here’s a code example on github https://github.com/CheshireCat44/GPXwithPowershell

    1. Thanks for the comment 🙂
      Yes as I’ve been tweaking my initial PS code as stated I’m very much a newbie with PS.
      One of my todo’s is to move this script into a UI, and as part of that is to allow the picking of a photo that you know where you took it. That can then be used as a reference point against the GPX data. Will I ever get around to doing so… hmmm. Considering it’s took me month and half before opening my blog to see your comment, but hey you never know!

      My code is also on github, and if you’ve looked and run it, it is not the most robust and I have a rather long list of tweaks for just the basics. What this space.

Leave a Reply

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


This site uses Akismet to reduce spam. Learn how your comment data is processed.