Link to home
Start Free TrialLog in
Avatar of Darrell Porter
Darrell PorterFlag for United States of America

asked on

Powershell: More elegant method of creating a visual interface

I have the following code block:
$tspan = @(
    New-Object -TypeName PSObject -Property @{'id'='01';'name'='Previous hour';'seconds'='3600'}
    New-Object -TypeName PSObject -Property @{'id'='02';'name'='Previous 2 hours';'seconds'='7200'}
    New-Object -TypeName PSObject -Property @{'id'='03';'name'='Previous 4 hours';'seconds'='14400'}
    New-Object -TypeName PSObject -Property @{'id'='04';'name'='Previous 8 hours';'seconds'='28800'}
    New-Object -TypeName PSObject -Property @{'id'='05';'name'='Previous day';'seconds'='86400'}
    New-Object -TypeName PSObject -Property @{'id'='06';'name'='Previous week';'seconds'='604800'}
    New-Object -TypeName PSObject -Property @{'id'='07';'name'='Previous 2 weeks';'seconds'='1209600'}
    New-Object -TypeName PSObject -Property @{'id'='08';'name'='Previous month';'seconds'='2419200'}
)
$tmspan = ($tspan | Select-Object -Property name, seconds | Sort-object -Property id | out-gridview -OutputMode Single -Title "Select a timeframe").seconds

Open in new window

Is there a more elegant / best-practice method of creating this interface?
I am trying to write a more complete Powershell-based interface to Cisco MEraki's API.

Thank you in advance!
Avatar of Sam Jacobs
Sam Jacobs
Flag of United States of America image

Pretty much the same:
$menu = @"
    'id', Name, Seconds
    '01','Previous hour','3600'
    '02','Previous 2 hours','7200'
    '03','Previous 4 hours','14400'
    '04','Previous 8 hours','28800'
    '05','Previous day','86400'
    '06','Previous week','604800'
    '07','Previous 2 weeks','1209600'
    '08','Previous month','2419200'
"@ | ConvertFrom-Csv
Do {
	If ($selection = $menu | Select Name, Seconds | Out-GridView -Title "Select a timeframe" -OutputMode Single) {
		& $selection.Script
	}
} Until (-not $selection)

Open in new window

		& $selection.Script

Open in new window

should be
		selection.Seconds

Open in new window

But I think it doesn't really improve the interface, only how to feed it. A "proper" interface IMO requires .NET (XAML) or something similar, a GUI form designed for the particular purpose.
Yep … I had a copy/paste error, thanks. However, I believe it should be:

      $selection.Seconds

Open in new window

I totally agree about it only improving the feed. Not sure why the ID field is needed, since it comes out in the same order it was entered.
You believe correct.
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Darrell Porter

ASKER

I will try out the GUI tomorrow.  Thank you for that.
I appreciate all of your responses.
Thank you, Gentlemen.
I had not considered using a "genuine" GUI as I am often asked for Quick & Dirty solutions and the picker was the easiest thing to implement.
I do have to tweak oBda's code because I need to reference a specific, limited set of times (those specified in my original code snippet) due to the API being used (Cisco-Meraki) and the defined business needs.