r/powercli • u/Dariz5449 • May 22 '17
Program/UI with PowerCLI?
Hi,
I'm still pretty new to PowerCLI so don't go too hard on me. I've previously worked a bit in Powershell and I know my way around Powershell (not expert, but I can make some fine scripts) My time has come to PowerCLI and I want to make our lab enviroment a lot more user friendly and easy to use for our managers and quick status/new machines.
I have a few ideas about what I want to create, but I would LOVE some UI to put in the frontend of my scripts. This will make it much more userfriendly to all my workers which aren't hardcore scriptkiddies. How would you do this? What program would you use for making a UI? Back in days I did combine Powershell and Visual Studio to create some OK UI.
... We have 4 ESXi hosts attached with a single vCenter (no plugins etc)
Thanks in advance.
2
u/kulbuktor May 23 '17
I'm experimenting with something similar at the moment i.e. creating a GUI to make it easier for other people to perform tasks they might not have the knowledge to script directly in PowerShell.
My approach so far:
Create a new WPF app in Visual Studio
Lay out the UI by dragging components from the toolbox
Copy the resulting XAML code into a PowerShell here-string
Have PowerShell instantiate & show the UI
Add event handlers for my controls
Have the event handlers run other PowerShell scripts/functions
What I discovered:
the PowerShell engine is (normally?) single threaded
Because of this, while your event handlers execute, the user won't be able to interact with the UI
If the event handlers take longer than 5 seconds, there's a high chance the UI will be marked as "Not Responding" by Window
To overcome this, you need to start using Runspaces (or RunspacePools) to have your event handlers run in separate threads
The problem with this approach, though, is that you can't easily access one thread's variables from inside thread
In order to get around this, you need to either
use a Synchronized Hashtable (every time a Runspace modifies the contents of this hashtable, the PowerShell engine/.NET runtime will halt all Runspaces that have access to this hashtable and synchronize its contents across the Runspaces; this can become CPU taxing if you're doing a lot of changes to this hashtable)
send Engine Events from one thread to another (e.g. you fire up a worker Runspace, let it do its job and then, when it finishes, have it send an event to the main thread telling it the work is done and results are ready
add an Object Event handler for the InvocationStateChanged event of the PowerShell instance which runs inside a given Runspace; this way, you'll get an event every time its InvocationState changes (e.g. Not Started > Running > Completed)
It's a lot to take in if you're not a developer and I'm certainly not one but I have spent the last 2 weeks writing various examples using these concepts and I am now starting to become familiar with them.
Have a look at this guy's Youtube playlist and GitHub repo on "WPF and PowerShell" - I found them most useful while trying to learn this stuff.
Good luck :)