[PowerShell][Resource]: Creating a menu from a template

[PowerShell][Resource]: Creating a menu from a template

Ever needed to have a series of tasks passed off to another team, which lets face it, may not be as technical as we'd like? ... Well fear not someone painstakingly [joking of course] created a simple solution for you. I'll leave the use case up to you, I don't want to be the only one apart of the creative process, but please feel free to comment if you do use it and why. Below I have included all the files needed as well as examples.. Please note that the tasks are simple and just to show how things work.

[This is a personal about me section feel free to skip this following paragraph] Since this is my first LinkedIn article I think a quick into is needed. I am a little sarcastic and you might see that in my writing style. I was laid off about 6 months ago, and I have been pondering what to do with myself.... you know when I wasn't looking for a job. I decided what I wanted and missed doing, is helping people, especially in the tech arena. I agonized over the voice and topics, and well I landed on just being myself, using my own voice and whatever random topic I want :). I have a whole life ahead of me for organization and a professional tone.

Alright, so the moment you've been waiting for [drum roll] MenuFromTemplate.zip. Included in this are all the files needed below is a quick description of each.

Files [I feel like they are pretty self explanatory, but just incase]:

  • consoleFormat[.json/.xml] - The example template I have created that will generate a console based menu system.
  • createMenuFromTemplate.ps1 - The meat and gravy, reads the template and creates a menu based on the input. This will need to be in the same directory as the scripts, unless modified.
  • importJson.ps1 - A helper file that will handle the JSON format for import. This will need to be in the same directory as the scripts, unless modified.
  • testConsoleMenuCreate.ps1 - This is the example for the console, run this :) see how it works, you get the idea
  • testWindowMenuCreate.ps1 - Same as above, just using the windows form, also run this see how it works.
  • windowFormat[.json/.xml] - Same as the consoleFormat,.. just for the well.. the window format.

Templates

So I thought I should quickly cover the templates.

The console template is the easiest. The "Template" node is the highest level and is required. Format only had two options; "console" or "window", which will select the style of menu presented. Everything else underneath "Template" is fairly self explanatory with the exception of "ExecuteOnHotKey", this node is for menu snapping. When true it will also automatically run the desired codeblock. Now for each MenuItem [in "MenuItems"]. Please note that this changes a bit based on xml or json, as json does not have the node "MenuItem" they are implied as part of an array under "MenuItems". "HotKey" refers to the key press needed to snap and activate (if set to true). Finally the function is name that you called a function in your script that you want that node to activate.

So now if that wasn't painful enough.. The Window options :)... Since we start talking about a GUI, size and position come into play, and are on pretty much every object. Along side of that you will need two attributes for each "FormItem" again it is implied in the json format as described in the console section. 1. ObjectType - currently supported are the following objects: label, textbox, radiobutton, checkbox, combobox, button. [feel free to add your own, or suggest some and maybe... I'll update]. 2. ObjectIDName - This is needed to access your objects later in code, you know to get values and such. The one thing to note is that buttons will need to be put at the end due to how PowerShell compiles the objects. If put first it will have no context to anything that was created before it. This is also true with the code side.

Usage

Again lets start with the console because its just easier. So you created your template.. cool the menu is mostly done then, all you have to do is dotsource to it like so:

. .\createMenuFromTemplate.ps1

Then add your functions... you know, do your things... Finally, run the following line (which will work if you dotsourced correctly above. Just note and this is SUPER DUPER important... put this after everything... otherwise well they won't work as it will generate the objects and have no clue what those are.

createMenuFromTemplate "$((Get-Item -Path ".\" -Verbose).FullName)\consoleFormat.json"

And just like that, you have a menu system... hit up, down, hotkey, esc they all work :).

So, your want to get fancier and show off a window menu system. Well, if you got your template right, its all the same as above. First dot source:

. ./createMenuFromTemplate.ps1

Then your functions... Well wait... we now have objects... how do I get the state of them, or how to I write an output to a text box... Again simple. I created a function to do just that. "getFormElement". If you've been reading this entire document, then I feel sorry for you. If not, in the template section I mentioned the really important ObjectIDName. Just use the getFormElement with the ObjectIDName as a parameter and you can get and set any of the properties. Here's my example:

function getdate{
    $date = get-date
    $outputLocation = getFormElement "tbRslts"
    $outputLocation.Text = "getdateFcn - $date"
}

So now to create the window menu:

$null = createMenuFromTemplate "$((Get-Item -Path ".\" -Verbose).FullName)\windowFormat.xml"

As long as your template is correct you should be good to go. If not you'll see errors on the console screen needing to be fixed. if events aren't working, check to ensure they functions are created before anything attempting to use them. When all else fails blame me. :) I make no guarantees everything is perfect, but "it worked on my machine".

Anyway, this brings to end my first... winded article.. I just hope that this rambling has been found useful to anyone if not everyone.

-Best Luck, Robert Waltzing

To view or add a comment, sign in

More articles by Robert Waltzing

Explore content categories