Show-UI popup with variables

Jul 28, 2014 at 2:49 PM
Hi,

I've been using Powershell for a little while now and I've been making good progress.

Since starting to mess around with Show-UI I've started to feel really stupid again!

I'm currently trying to incorporate Show-UI functions into a script I've already written.

The script only takes 1 Input ( Username ) and returns 7 variables. I want these variables to be displayed in a popup but I cannot for the life of me figure out how to do this.

I've adapted my script to output the variables in a hashtable, in the hope that this may be easier to read into a popup function within Show-UI, but as I said, I can't quite figure it out.

so, long and short :

Hashtable output from my script > popup window displaying variables in hashtable

Could someone give me a very brief example of how this works with passing in variables?

Hopefully I haven't been massively vague with this post :(

Thanks in advance for your help, I'm fed up of banging my head against my screen!
Coordinator
Jul 29, 2014 at 3:00 AM
I think you want Get-Input
Get-Input -Field @{
  FirstName = [String]
  LastName = [String]
  Birthdate = [DateTime]
} -show
Jul 29, 2014 at 8:55 AM
Hi Jaykul,

I'm not sure this is what I'm looking for, as I want to create a popup that DISPLAYS information that has been returned from a function, instead of creating a popup to enter information into.

For example :

I've already used Get-Input to provide a popup for the user to provide a username into. This is then passed to a function I've written, that then returns 7 items in a hashtable. The returned items will always be of the same type, and in the same order. I just can't figure out how to put together a window with Show-UI that displays this information.

Looking at the examples, they mostly show how to use Show-UI to input information, instead of display information that has been returned, or running a command within the function written with the Show-UI module.

I hope that makes a bit more sense.
Coordinator
Jul 30, 2014 at 4:44 AM
Ah, yes -- you clearly want the opposite of Get-Input: maybe Select-ViaUI
New-Object PSObject -Property @{
  FirstName = "Joel"
  LastName = "Bennett"
  BirthDate = [DateTime]"1974-5-22"
} | Select-ViaUI -Show
But since you don't need to select, it would be more fun to write something custom:
function ConvertTo-UniformGrid {
    [CmdletBinding()]
    param(
        [Parameter(ValueFromPipeline=$True, ParameterSetName="Dictionary", Position=0, Mandatory=$True)]
        [Collections.IDictionary]$Dictionary,

        [Parameter(ValueFromPipeline=$True, ParameterSetName="InputObject", Position=0, Mandatory=$True)]
        $InputObject
    )
    process {
        UniformGrid -Columns 2 {
            if($InputObject) {
                $InputObject = Select-Object * -InputObject $InputObject
                foreach($member in Get-Member -InputObject $InputObject -Type NoteProperty) {
                    Label $member.Name
                    Label $InputObject.($Member.Name)
                }
            } else {
                foreach($kv in $Dictionary.GetEnumerator()) {
                    Label $kv.Key
                    Label $kv.Value
                }
            }
        } -Show
    }
}


function ConvertTo-DataGrid {
    [CmdletBinding()]
    param(
        [Parameter(ValueFromPipeline=$True, ParameterSetName="Dictionary", Position=0, Mandatory=$True)]
        [Collections.IDictionary]$Dictionary,

        [Parameter(ValueFromPipeline=$True, ParameterSetName="InputObject", Position=0, Mandatory=$True)]
        $InputObject,

        [Switch]$IsReadOnly,

        [Switch]$IsDisabled
    )
    process {
        # UniformGrid -Columns 2 {
        DataGrid -IsReadOnly:$IsReadOnly -IsEnabled:(!$IsDisabled) -HeadersVisibility "None" -ItemsSource @(
            if($InputObject) {
                $InputObject = Select-Object * -InputObject $InputObject
                foreach($member in Get-Member -InputObject $InputObject -Type NoteProperty) {
                    New-Object PSObject -Property ([ordered]@{
                                            Field = $member.Name
                                            Value = $InputObject.($Member.Name)
                                        })
                }
            } else {
                foreach($kv in $Dictionary.GetEnumerator()) {
                    New-Object PSObject -Property ([ordered]@{
                                            Field = $kv.Key
                                            Value = $kv.Value
                                        })
                }
            }
        ) -Show
    }
}
The second one probably requires PowerShell 4
They'll both probably make it into a future release, with a bit more polish ;-)

In case it's not obvious, the "Field" and "Value" in the second would become column headers, which seemed silly, so I set the -HeadersVisibility to "None" to hide it.

They should both take either an object or a hashtable and display it in a sort-of grid.

How's that?
Jul 30, 2014 at 9:57 AM
Thanks Jaykul,

The first example helped me get my head around the issue I was having. After a bit of hacking about with your example I was able to produce exactly what I wanted, and gained some valuable knowledge with regards to Show-UI which I will be able to implement more in the future.

Thanks again for your help.