Powershell drop down list for parameter values.

  • Hello,

    I am looking to right a function that will add a sql login to a role. Thing is, when the parameter is entered, I don't want anyone typing anything is because typos are a pain. I have done some research, but they are all form-based.

    At this stage I'm just wondering if this can be done outside forms?

    I did write some code, and although it did not raise any errors, I have no idea how to use it, that's if it actually works!!

    [Array]$AddToDBRole = "db_datareader","db_datawriter", "db_owner"

    Function Return-DropDown {

    $Script:Choice = $DropDown.SelectedItem.ToString()

    $Form.Close()

    }

    I don't think I'm calling it correctly to get it to actually do anything, or the code is just rubbish. I'd appreciate any pointers. Thank you.

    Regards,

    D.

  • Unless you just did not copy/paste all of your code you are missing about 45 lines of code to just build out the form. The example you can use from TechNet shows you all the code you need, but they do leave off how to properly capture the selected item.

    A full example for your situation

    Add-Type -AssemblyName System.Windows.Forms

    Add-Type -AssemblyName System.Drawing

    $objForm = New-Object System.Windows.Forms.Form

    $objForm.Text = "Select a Computer"

    $objForm.Size = New-Object System.Drawing.Size(300,200)

    $objForm.StartPosition = "CenterScreen"

    $objForm.KeyPreview = $True

    $objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")

    {$x=$objListBox.SelectedItem;$objForm.Close()}})

    $objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")

    {$objForm.Close()}})

    $OKButton = New-Object System.Windows.Forms.Button

    $OKButton.Location = New-Object System.Drawing.Size(75,120)

    $OKButton.Size = New-Object System.Drawing.Size(75,23)

    $OKButton.Text = "OK"

    $OKButton.Add_Click({$x=$objListBox.SelectedItem;$objForm.Close()})

    $objForm.Controls.Add($OKButton)

    $CancelButton = New-Object System.Windows.Forms.Button

    $CancelButton.Location = New-Object System.Drawing.Size(150,120)

    $CancelButton.Size = New-Object System.Drawing.Size(75,23)

    $CancelButton.Text = "Cancel"

    $CancelButton.Add_Click({$objForm.Close()})

    $objForm.Controls.Add($CancelButton)

    $objLabel = New-Object System.Windows.Forms.Label

    $objLabel.Location = New-Object System.Drawing.Size(10,20)

    $objLabel.Size = New-Object System.Drawing.Size(280,20)

    $objLabel.Text = "Please select a computer:"

    $objForm.Controls.Add($objLabel)

    $objListBox = New-Object System.Windows.Forms.ListBox

    $objListBox.Location = New-Object System.Drawing.Size(10,40)

    $objListBox.Size = New-Object System.Drawing.Size(260,20)

    $objListBox.Height = 80

    [void] $objListBox.Items.Add("db_datareader")

    [void] $objListBox.Items.Add("db_datawriter")

    [void] $objListBox.Items.Add("db_owner")

    $objForm.Controls.Add($objListBox)

    $objForm.Topmost = $True

    $objForm.Add_Shown({$objForm.Activate()})

    [void] $objForm.ShowDialog()

    $x = $objListBox.SelectedItem.ToString();

    $x

    Which I came across a post on a PS forum that shows a nice function for the above that you might work out using.

    Shawn Melton
    Twitter: @wsmelton
    Blog: wsmelton.github.com
    Github: wsmelton

  • Hi Shawn,

    Thank you for taking the time to answer. Obviously I've completely misread what I was looking at! However, it has opened my eyes to possibly using a form for what I am doing, though time may be against me!

    Regards,

    Paul.

  • Hi Shawn,

    Ok, I've worked out what it was I'm actually after, and I'm almost there. What I was actually after was Argument Completion (unfortunately I didn't know it was called that! :hehe:. So this is what I have so far....

    Function select-DBRole

    {

    param

    (

    [ValidateSet('db_datareader','db_datawriter','db_ddladmin')]

    $ChooseDBRole

    )

    $DBRole = select-DBRole

    }

    Now this works a treat. The last part ('$DBRole = select-DBRole'), is where I'd like to dynamically assign the result of the function to the $DBRole variable. I've tried a number of things, none of which worked.

    How can I assign the result of the function so the variable value will become whatever database role was chosen?

    Thank you for any help.

    Regards,

    D.

  • If the purpose is to use the value being passed out of this function there is no reason to add a variable in the function itself, just let it output the value for you. If you need to use the value within the function, just reference it using the parameter name "chooseDBRole", that will contain the value entered by the user.

    function select-dbrole {

    param(

    [ValidateSet('db_datareader','db_datawriter','db_ddladmin')]

    $chooseDBRole

    )

    $chooseDBRole;

    }

    select-dbrole -chooseDBRole db_datareader

    Shawn Melton
    Twitter: @wsmelton
    Blog: wsmelton.github.com
    Github: wsmelton

  • Hi Shawn,

    Thank you for getting back, the thing I am not getting here is, after I have ran the code and type in $ChooseDBRole [enter], there is no value assigned to it. I just get the PS prompt. I have added $ChooseDBrole as an mandatory parameter at the top of my code.

    Regards,

    D.

  • Duran (5/18/2016)


    Hi Shawn,

    Thank you for getting back, the thing I am not getting here is, after I have ran the code and type in $ChooseDBRole [enter], there is no value assigned to it. I just get the PS prompt. I have added $ChooseDBrole as an mandatory parameter at the top of my code.

    Regards,

    D.

    You are referring to the scope of the variable, take a look at the man for about_scopes (get-help about_scopes).

    Shawn Melton
    Twitter: @wsmelton
    Blog: wsmelton.github.com
    Github: wsmelton

  • Hi Shawn,

    Thank you for getting back, I had a read on Scopes, quite a bit to take in for a newbie, but I did try to apply some of the concepts, specifically the small section on making the variable global with the function. I tried a few things out but it either broke the function, or made no difference. I'll read a few more articles on Scoping and see if something else points me in the right direction.

    Regards,

    D.

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply