User Input with a function (new to me)....how to specify order

This topic contains 7 replies, has 4 voices, and was last updated by  postanote 2 weeks, 5 days ago.

  • Author
    Posts
  • #97995

    JasonL
    Participant

    Hi Guys am in need of a little help – currently creating a script that requires user input, which then is set as variables. Found the below and amended slightly:

    cls
    $H = Hostname
    $Date = ((Get-Date).ToString('dd-MM-yyyy'))
    
    function SiteDetails {
        param (
            [string]$SiteName,
    	[string]$Filename      
        )
        [pscustomobject]@{
    	SiteName = $SiteName
    	Filename = $Filename
             }
    }
    
    $result = Invoke-Expression (Show-Command SiteDetails -PassThru)
    $Site = $result.SiteName
    $File = $result.Filename
    
    $Site
    $File
    

    but whichever order I put :

     param (
            [string]$SiteName,
    	[string]$Filename      
        )
    

    in, it's ignored and the input box stays in the same list format, doesn't change if I put [string]$Filename before [string]$Sitename
    obviously it's some formatting I must be missing, but not sure how / where....any help greatly appreciated. This is my first foray into functions, and with input boxes

    thanks

  • #97998

    Don Jones
    Keymaster

    the input box stays in the same list format

    What input box?

    • #98001

      JasonL
      Participant

      when I run the script, it creates a text input box called Site Details, with a Parameters: and then text inputs for Site details and filename with OK, copy, cancel at the bottom

  • #97999

    Don Jones
    Keymaster

    Oh wait, I see. You're using Show-Command. That's not technically an input box, but I get you now.

    I'd probably advise against that. You don't really get any control over how Show-Command works. If you need to have something pretty, you'd need to spin your own UI. Bear in mind that "GUI" is kind of the opposite of what PowerShell stands for.

    Also, "Get-SiteDetails" would be a better function name ;).

    • #98002

      JasonL
      Participant

      Yeah, I got you, thanks...not really up on creating GUi's so thought this would do enough of a job for the stuff to get done (under the hood).....all it is is for a format of site name and then the filename, so they are specific, but nothing too grand 🙂 Was more interested in the process!

      Thanks

    • #98004

      JasonL
      Participant

      my main script pulls O365 stuff for different customers etc, and was trying to get a format for the files to reflect this – I tried read-host but when the script ran, it ignored that, hence finding this 'function'

  • #98005

    Joel Sallow
    Participant

    It looks like Show-Command simply orders them alphabetically... but you may be able to coerce it if you explicitly specify the order:

    function SiteDetails {
        [CmdletBinding()]
        param (
            [Parameter(Position = 0)]
            [string]$SiteName,
            
            [Parameter(Position = 1)]
    	[string]$Filename      
        )
        [pscustomobject]@{
    	SiteName = $SiteName
    	Filename = $Filename
             }
    }

    EDIT: Yeah, not even this changes Show-Command's behaviour. Strictly alphabetical, I'm afraid.

  • #98020

    postanote
    Participant

    Ditto to what DonJ says here:

    For Q&D (quick and dirty) stuff I do this kind of thing, but knew the default is always alphabetical.
    You can see that by doing the Show-Command for any function of cmdlet.

    So, yep, write your own form. If you show Show-Command because you wanted to avoid writing a form (the same reason I'd do this, well sometimes, and using Out-Gridview as well for stuff), you can jump out to poshgui.com, click the GUI Editor, and use that site to quickly use drag and drop to crate your form, download that code and go from there.

    Sure you can hand code this, but a form GUI editor is a more prudent option and of course that site is free.

    Otherwise, you need to consider getting Sapien's PowerShell Studio (I've been using it since 2012 – good PoSH dev tool – but not free) or master hand coding WinForms, or XMAL forms, or master the msgbox approaches using the .Net libraries or use this module for message box efforts. 'donaldmellenbruch.com/post/anybox-v0.3'

You must be logged in to reply to this topic.