Getting started with PowerShell TUIs (Creating Console GUI PowerShell Tools) - Jeffery Hicks - …

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign [Music] this session is called getting started with Powershell twoes now one thing I want to mention here I've been speaking at events like this for a long time one thing you should keep in mind when you come to an event like this don't expect to learn everything that there is in 45 minutes that's just not going to happen you should come to sessions hoping to say hey is it something that I want to learn more about or maybe by the time I'm gonna go yeah that was interesting but that's not on my agenda and that's perfectly fine so you should just come to see what is this thing all about get some notes about where to go to follow up and learn more but don't expect that these conference sessions are going to teach you anything in 45 minutes other than oh I need to start doing something else I already have a list of a dozen things that I need to learn more about when I get back home so that's what you should be focusing on uh my name is Jeff Hicks I am just an old Powershell guy um although actually I need to take a moment and think about back to the very first pre-powershell event at Red Rocks in Las Vegas I don't think anyone here was that at was that that event it's amazing for me to think if I could go back to talk to my old self that in I don't remember what year that was it's like 13 years later that there'd be powerful events literally all around the world so that's really amazing for me and I'm very happy to be uh a part of that and which if you want to know more about me the geta bio link we'll help you with that all right so let's get right to it so terminal user interface or two e's some of you may have seen some of these things uh this is now kind of like my new hot Obsession in Powershell instead of gooey bass stuff like WPF we're going to be creating forms that will run in the console and this worked great if you're using Windows terminal although it will work in the Legacy console but you should all be terminal users by now now there are some requirements and this is where it gets a little tricky there are a couple assemblies that you will need at least as of today and I'll show you where to get them and install them terminal that GUI that's the main open source project that we're going to be using and there's another assembly called end stack I'm not a developer so I but I know enough to cluge my way through the nice thing about all of this stuff I'm going to show you is that it does run cross-platform so you can build a Tui based tool that will work in Linux Mac and windows it does run across platform although I'm just focusing all on Powershell seven primarily because I want the cross platform support I'm using the latest version one dot whatever it currently is of the turmeleg.gui stack there is version two that is in development and that will take away the requirement for endstack so everything I'm going to show you now is good as of today but maybe in six months it might have changed now you can experiment we're not even sorry you can use these terminal tools right now there is a Microsoft module that you can install microsoft.powershell.console GUI tools Powershell 7 only this will give you a single command called out console grid view just like upgrade view except it runs interterminal interface as far as multiple selection Powershell seven only the downside is that the version that's in the module that's in the gallery now is using an outdated version of the terminal jet GUI assembly some of you may already be able to see where I'm going with this you can use the command the same way you would you use upgrade view we're in get process piped out grid View get a nice little display like that you can select items then the items get returned back to the pipeline you can't really customize it too much other than using the filter in there but this is a 2E a terminal user interface that Microsoft built you can go and install it now and use it right away but the real fun is hey I want to make my own things and that's what I want to show you here so you may want to create and I actually have an example later I'll show you I recreated this screenshot that I pulled from the term like gooey GitHub repo I want to build a cross-platform app it will support mouse and keyboard input you can create little tab paths you can tab around so you can keep your hands you know on the keyboard you don't have to use a mouse but it can you can click things you can again this is kind of like WPF and when created WPF space scripts all right so if you've done that conceptually this will be no different and we'll get to that when I get to the demos uh there's clipboard support uh some things that we're not going to get into I'm just going to kind of give you an overview for Loops timers event handlers lots of things that that we can do with this again conceptually is very similar to using WPF so that's enough of the intro self let's just get right to the meat of the demo so I'll leave this up for a second all the demos I'm going to show you are up on GitHub everyone got that now we'll see if this will no it won't you have to stop the slideshow all right so some setup stuff first off let's show you um up console grid View so I already have the module installed and I'm going to uh let's run it here so this is a plain Powershell seven no profile loaded all right so this will be nicer than the screenshot so you can come through you can select what you want hit enter and it writes those objects in the pipeline just like upgrade view does so that's kind of Handy now if we want to do the chewy stuff that I want to show you some requirements now you may have to adjust your package source and I'm just going to get some of the stuff that I want to show you doesn't necessarily work very well in BS code so just to get in a habit I'm just going to be copying and pasting between vs code and here all right so I've already set up the nuget Source you'll need that if you don't have that I think I have yeah I have the line here that you can register that if you don't have that source so I'm not I'm not going to run through every line you've got this code you can go through and try yourself you want to look for the terminal dot GUI package now it's a nuget package so you can use install package and I should have splatted all of this be sure to skip the dependencies otherwise you'll end up in a loop and you'll eventually get an error message that it can't find something because we don't need that once you've installed it that will install it in your system so I've already installed it so let's grab get package so right so they're under C program files yeah I don't P dot sorts so when I installed the package it just created download this file that has a new package extension that is nothing more than a zip file because what you want to do is you just want to expand that archive and I'm just going to copy that to a temp directory in this case I don't know I need to change this but I don't have dtemp on this computer so my favorite vs code think that will work so terminal GUI so underneath here are going to be the packages and we don't want the services sorry the library so this has the different versions of.net and within here it's just a recurse are the different assemblies that we eventually will need to load okay so what it depending what you're doing like for example I'm building a new module that's going to package a lot of my demonstrations and so I'm going to load in the module I'm going to take the appropriate.net 7 or whatever whatever one works or dotnet7 version here which I think is a net standard 2.0 and I'm going to copy that into my module and then load that in the module so we'll see here and I've got some code here that copies all of that so that's the terminal GUI piece you also need the end stack piece now if you have the Powershell console GUI stuff installed you kind of have that but you can also follow the same process install the and stack.core package expand it grab the dll that you need and put in the same directory where your terminal at GUI assembly is and then you can just load them I'll show you all that when we get to that all right so that's real quickly on the requirements well let's do the ubiquitous hello world example I have some links here to the GitHub repo for terminal I got GUI where you can learn all the read all the documentation and everything on this let's look at hello world Now by default don't run this in vs code because you'll run into a key binding conflict unless you follow the notes here I'm going to run all my Demos in the console so what I need to do is in a virgin system I need to load the assemblies okay so I have them in a directory that is under PS script root you need to then initialize the terminal.gui.application this is the first line you're going to have in any terminal GUI script after you load the assemblies okay because you need to do this before you can access any of the classes now in my examples I've have several techniques of creating the objects this is just using a very simple programmatic way creating new object terminal you've got gui.window again the documentation will go through and show you all of the classes like WPF everything runs in kind of a I hate to say the word container but a layer but let's call it layer at the very top you have a window then within that window we're going to add our other controls like you do in WPF you know buttons and check boxes and text boxes and labels and all of that these objects obviously have properties so you can once you create the object you can just pipe to get member and see what the properties are in addition to go and look at the documentation so I'm just creating a new window giving it a title some text and telling it to Auto size and then adding that top layer window to the application I then run it and this is this will be a blocking call by default control Q is how you get out of the application I'll show you in a bit how you can modify that if you want to and then very importantly when you are finished run the shutdown command to clean up everything so this is all in a Powershell script file and let's run this here in Powershell seven all right there we go so that's a very basic what it looks like now one thing to point out the color schemes that you get might vary depending upon the color scheme you're using in Windows terminal so this is a different Windows terminal has a slightly different color scheme you can kind of see no not too bad it is pretty much similar there some of the other things I do it will be different not then again as I said control Q to break out of that there is a bug right now something going on I don't know if it's with the assembly or it's Windows terminal but you notice you get a little artifact left over at the bottom from when the terminal was presented I think it's a term lagoo we think I think I saw an issue on GitHub it's just a cosmetic thing you know clear screen and gone all right so let's get a little more here let's go to hello world 2. and I'm not going to walk through every highlight the whole line foreign so here again I because I I wrote all these demos to be Standalone but I also wrote them in such a way that I might run them consecutively so I need to so low to make sure the assemblies are loaded but I don't want to get an error message saying that the assembly is already loaded the other thing you're going to run into is that if you have loaded the older version of terminal dot GUI like from out console grid view you'll get an error message you get basically you have a conflict right now I'm very happy to find out there's gonna be a session on how to avoid that so I need to figure out how to avoid that but that is one thing to keep in mind so I'm I'm basically loading or trying to load some dlls and if they already exist I'm just going to ignore the error and just keep going now the one thing I've done different in this demo and what I do want to point out is I want to change the quit key I spent a lot of time trying to figure out how to get this to work the right way because the key has to be basically the ASCII key value so the Escape key is has code 27. there are term lagui codes key chords that are supposed to work and they probably work in compiled C sharp code but I could not get them to work consistently in Powershell in in vs code so I just went to the lowest common denominations give me the raw value so 27 I typically just use 27 for the escape code everything else here is pretty much the same different syntax of creating the object so here I am implicitly creating a GUI window and then I have uh basically splatting if you will all the parameter I'm sorry all the properties for that object so instead of creating it with new object and then object.title dot text I'm doing it all here in one thing I'm also creating a button positioning the button just like in WPF I'm going to create an error Handler so I'm going to call the add underscore clicked method and I'm going to call the request stop which will tell tell the journal you got GUI hey you want I want to shut down I then have to add that button to the window and then at the very end add my window to the top main application run it it will run and then once the that request stop happens then shutdown gets called now I also put in show you this will also work with Mouse alt Q or that's one more time uh Escape okay so I I gave myself some options there so I didn't get totally uh screwed up and again the unfortunate artifact all right so that's the hello world that's kind of boring the 2E will write to the pipeline if you want it to so this example I'm finding basically a simple window and a title called hello Jeff and I have a text box here and I believe yeah I escape was still my Escape key so here's a here's a tricky thing white and this is why I always document in my two week definition what I want the Escape key to beat because whatever you define as the Escape key or whatever it detects is the Escape key it will always use because the Escape key is a field in the object and it's not something that you can change on the Fly unless you change like I do in the script so I always document the uh the key and then here's what that code looks like very similar so here I'm initializing the Escape key using the actual numeric value of control which is control Q instead of trying to get because I should be able to use this control math stuff it doesn't I can't get the word consistently so I figured out what the exact numeric value is at its returns and just use that numeric value and you see of the same type of code that I'm using before so I basically create the window because to review here create the window create the button button Handler add the button to the window I add my quit button add the button to the window add the window to the application run it and that's as simple as that and to show you that this also works cross-platform um so there's hello world that looks a little bit different this is an Ubuntu instance in WSL yeah let's tell it works too so I change the Escape key and then let's do my sample now here you can see the color scheme kind of can kind of get in the way so it may take a little trial and error changing the colors in these terminal guides is not necessarily A Simple Thing you can create little message boxes as well let's do a quick and then it wrote the response back to the pipeline let me do a time here all right so let's look a little closer at some of the elements that you might add first up frames this is a piece of a demo that I repurpose or borrowed from Andrew pla and there's another way that you might create code it's probably the cleaner way to do this at the top of the script I have the using namespace directive so I'm saying using the terminal got GUI namespace that way down in my code it can just be application this I could probably change and I should change to that way I don't have to include all of that and I let's change this one too I'm tempting Fate by changing my demo live Midway through I even saved it so you can create different frames so I can have my window and I can build a frame and then I can add elements to the frame I'll just show you what that looks like so you can have multiple frames you can size the frames the way that you want you can give titles to the frames this is just all this is doing just showing frames I haven't added any buttons or drop downs or anything else to it just want you to see the the frames I have so much you can create lists no way why this won't stay down I keep telling it stay down all right so the beginning of this script I'm basically running Powershell to get all the processes here on line 17. and creating a hash table and then creating a list object line 19 and then adding each of the processes to the list object I initialize my application line 23 to find my Escape key creating my window creating a frame creating a list View I'm specifying the location the X and Y as to where I want and this is like WPF this is the tricky part there'll be some trial and error to figure out okay where exactly do I want want to position this I can then set my source which the reason I made this a list object like a 19 line 19 is because that then works very nicely as the source on line 44. okay it doesn't necessarily recognize an array it needs to be a technically a list object that you get in.net and then I've added a list Handler that if I change a selected item in the list and as soon as I get to the demo it's going to change it's going to get some information from the hash table that I created earlier and display all that and I have second frame that has process details add the frames to the window window to the application run the application let's just look at this so that ran really quickly it got all the processes populated the list that's what's here in this Frame and then as I click it's running the event handler to display the properties from the object that's in my basically internal hash table and displays it here in the other list on in the other frame and I'm just hitting because I've selected this Frame I'm just using the down arrow key or up Arrow key I don't have any actions associated with selecting anything come on yeah I may have to fix that I don't know why that doesn't uh I've not had much luck getting scroll bars to work nicely with um terminal gui's menus I'm basically showing you all the things that you can do and then we'll put it all together here so menu's a little trickier because there is the menu object and then there are the items that go into the memory object I'm sorry not memory menu object this is where you need to read the documentation or kind of look at my examples here so I'm creating a menu item called close and I've got the underscore C for the type for the command accelerator key accelerator it's going to invoke if I click it the request stop method and then I add that menu bar item I'm sorry I have that menu item to the menu bar I'm sorry take that back I'm adding a new item called quit also to the menu bar and I have an about string here I'm adding another menu item called about that will display a message box that will show that here string and another menu item called documentation that's going to invoke start process and open up the terminal GUI GitHub page and then create my oh that's right it's just where it gets confusing you know let's run this then we can go back and talk about it all right so here are my menus so I have a menu called quit and a menu called Help under quit is a menu bar item for closing which should be here okay so that's close and that is under quit and then I have about and documentation which I put under new so when I click the about it invokes the message box and displays my little help string and Escape because out of that and then documentation takes me to GitHub we don't need to see that so menus are kind of fun you if you're building a full-blown application you kind of probably want to have menus do an untimely around 14 minutes this is more of a proof of concept as opposed to an actual you know anything totally useful I have plans to flesh us out more you can create progress bars so I've got a couple different versions here and you get the code will go through and show that oh where there is you can either put it in a frame like I have down in this one you can color code as to what value you want or if you want to have mine my mail was running and I've also created over here a radio box or yeah radio button selections that will go through and show you the different styles that you can create for the progress bar so you can kind of see what they look like and all of that code is listed here I'm just basically flying through trusting that you're going to go if this interests you you're going to grab all the code and try it yourself try to make it self-explanatory and the last thing I want to show you in terms of just raw elements there are ways that you can work with file dialog boxes they're kind of built into the library so there's an open dialogue class that you can create that will allow you to open up basically a file picker or in this case a directory picker so if I run [Music] oh actually I don't think I've I didn't program this to do anything I think if I quit oh I'm supposed to actually pick a file that's why there we go so I picked a file and opened it up in notepad and don't worry about being able to read that because that's not really the point so you can include things like that in your script so let's put this all together and let's look at a few more practical examples more complete examples you know let's just run this and move with the code so here's my two week credential prompt so again this is a blocking prompt you don't get your Powershell prompt back this is just like get credential you put in the password I even added I configured the password box to mask it and I configured this button to toggle between making it matched and unmasked so I can view and double check the password and then new creates the object so obviously I would have created assign that to a variable and then I could use that so that code now well there's another mistake I don't need that so I will be updating the GitHub repo here when I'm done so I'm creating the text fields so here's the terminal GUI text field for the password and this secret property is what I'm toggling with the button to to be whether it's masked or not so I create the buttons add the buttons to the form or the window add the Windows application and run it and then my last example well let's run it first and then we'll take a look at it now let me okay let me check my vm's running all right all right so this is kind of a complete to me example this is a functional Powershell tool that basically as title says that the service report it defaults to the local computer and the code creates the text field and then Auto popular in the basically provides the default value with my computer name I have a box over here for credentials now I can already tell that the formatting and spacing is a little off and that's because I have increased the size of my journal window so it displays better for you so the formatting is a little off there so let's get all running processes do you get info and now this displays it all in yeah and this is there we go all in a text file or a table that auto sizes now if I want to let's do something and let's do oh it did this right if it actually changed come on oh well it's cool I think I locked up well the demo gods do not like me it's fine we will go here I think I'll just stick to the local machine because I wanted to show you here are my menus so I have an option to you know clear the form oh yeah I got time let's screw this up again I should add my little button to show the password it may not like my VM setup here oh I didn't even talk about the status bar at the bottom no one asked about that so I guess my thought be that important there are ways there's a not so totally another different object so you can then add elements to that and I've got code so I'm failing to get to srv1 so no it's down there it says ready and the code in the script see it's changing the status bar giving me some feedback so at least I know it's trying to do something and I don't think it's going to work it worked at home it worked on ah okay so I think I have code in there that displays and error message and again because I have zoomed in my size I don't get to see the message displayed completely well but clear for one more thing here well yeah there is error hand so the question is when I put in the wrong password um I do have in here there is some error handling and just displays the air um to that I don't really have because I'm calling get some instance so I don't have too much integration other than detecting if there's an error and if there is just playing the message box that's I'm sorry right if there is an error I'm trying to display it unfortunately because I think I've zoomed in you can't see all the error message it's not displaying as nicely as it normally does but I also have options here so once you could get that uh so then say hey I want to export it to a CSV or Json file and then that would prompt up the file dialog to have me put in the location where I want to save it and all that and when I'm all done I have my little reminder down here this is using the Escape key to take me right back so let me go back to the slides real quickly because I do have a few other things I guess I should show all right some resources here so again these are just refreshing uh your memory about the GitHub site for the terminal you got GUI application or the assembly and from there you can get to all the other documentation uh Adam Driscoll has a 2E designer it's a partial module that you can install that allows you to use and it's based on a Tui that allows you to basically create the form and drag and drop the control that you want and then generate the code that you want um you can also take a look at that I included the link and the this UPC code I think takes you to the GitHub place not the repo place with a examples but we've got less than two minutes any other questions does anyone look like something you want to try or you want to run away from all right questions yeah you yes so the questions are so many features available in 5-0 so in Windows Powershell five one yes you can use the term look that GUI you just need to load the appropriate version of the assembly for the version.net that's in that which I think is the dot net four seven two so I'm using the version of the assembly that I know runs in Powershell seven I couldn't tell you off the top of my head which version runs a Windows Powershell but if you download the the package one of those libraries will work in Windows Powershell and then so then you can do it so if you want to make something truly cross-platform across version in your module you'll just need to have some code that would detect what version of Powershell you're running and then locally appropriate assembly that would be the way to get around that but yes it should work yeah I yeah I don't know why things change color wise I think it that may be a Windows terminal thing because there are ways the windows terminal displays things based upon the terminal color scheme that you use plus PS Redline colors and so it's kind of a and then terminal GUI has its own way of interpreting you know what is red for example you know so that's gonna it's gonna pull that from the underlying hosting application so I I try not to do too much with color because that's just going to raise all sorts of issues but that it's definitely a challenge other yeah does it work in a PS session over win or M well let's find out so let's uh here this ought to work because this is oh I got the wrong sir yeah thank you and I've got a minute I don't think it does because yeah in fact I'm almost certain it doesn't I'm going to I'll test it um later because Adam briscoll also has another module called PS edit which is a two-way editor that you can run now it's so really you know Alpha stage where you can solve that from the partial Gallery called PS edit you need to change a name um but there's some questions about getting that to work over terminal session because then you could have an a GUI or Tui editor editing a file on remote machine right and that's why I don't think this stuff works yet now it's possible that the new stuff in version 2 will I I don't know we'll have to see um I'm here through the end of the day Wednesday I mean I'm flying home on Thursday so I'm happy to talk with you guys about anything that you want and I think I'm over my time so thank you very much [Music]
Info
Channel: PowerShell Conference EU
Views: 4,421
Rating: undefined out of 5
Keywords: Monad, PSConfEU, PowerShell
Id: FOuw3mSZFnU
Channel Id: undefined
Length: 47min 20sec (2840 seconds)
Published: Sat Jul 15 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.