Friday, 19 September 2014

Powershell: $host.ui.PromptForChoic


$host.ui.PromptForChoic

PromptForChoice method (which belongs to the UI property of the $host object). When we call PromptForChoice we need to pass four parameters, in order:
  • $title, the title of our menu.
  • $message, the message displayed to the user.
  • $options, the menu options.





$title = "Delete Files"
$message = "Do you want to delete the remaining files in the folder?"

$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", `
    "Deletes all the files in the folder."

$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", `
    "Retains all the files in the folder."

$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)

$result = $host.ui.PromptForChoice($title, $message, $options, 0)

switch ($result)
    {
        0 {"You selected Yes."}
        1 {"You selected No."}
    }


----------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------

$t = "Location"
$msg = "What location?"
$CFT = New-Object System.Management.Automation.Host.ChoiceDescription "&A choice 1", "1"
$CON = New-Object System.Management.Automation.Host.ChoiceDescription "&B choice 2", "2"
$ELP = New-Object System.Management.Automation.Host.ChoiceDescription "&C choice 3", "3"
$options = [System.Management.Automation.Host.ChoiceDescription[]]($CFT, $CON, $ELP)
$result = $host.ui.PromptForChoice($t, $msg, $options, 1)


----------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------

No comments:

Post a Comment