header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

[August 25th, 2008] Check the home page regarding PowerShell related news from a brand new sponsor: Idera

Prompting for a choice
Last Post 15 Dec 2008 03:46 PM by Nibo. 3 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages Informative
NiboUser is Offline
New Member
New Member
Posts:2
Avatar

--
28 Nov 2008 03:08 PM  
Hi,

I have been using the code below to prompt the user to select from a choice of options. I got this code from http://www.microsoft.com/technet/sc...p0125.mspx. However, I'm having trouble removing the default option. Everything I've read online (although not in this article) says that to not set a default, you can specify the final argument to PromptForChoice as -1. When I do this I get the following exeption:

Exception calling "PromptForChoice" with "4" argument(s): ""defaultChoice" must be a valid index into "choice". Parameter name: defaultChoice Actual value was -1."
<BR>$Caption = "Confirm"<BR>$Message = "Are you sure?"<BR>$Yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes","help"<BR>$No = New-Object System.Management.Automation.Host.ChoiceDescription "&No","help"<BR>$Choices = &#91;System.Management.Automation.Host.ChoiceDescription&#91;&#93;&#93;($Yes,$No)<BR>$Answer = $host.ui.PromptForChoice($Caption,$Message,$Choices,0)<BR>

I've tried various combinations without success. Any suggestions as to where I'm going wrong would be much appreciated!

Thanks,


Nigel
DanBallUser is Offline
New Member
New Member
Posts:26
Avatar

--
28 Nov 2008 04:13 PM  
I tested this out, and it appears that in PowerShell v1, that value is required to be a valid index number, you are "required" to have a default choice.

You'll need to upgrade to v2 in order to use the function that you desire.
glnsizeUser is Offline
Basic Member
Basic Member
Posts:101

--
28 Nov 2008 11:28 PM  
K.I.S.S... I wouldn't bother muckin with Host.UI if your using V1.

$v1Confirm = ""
while ($v1Confirm -eq "") {
    switch (Read-Host "Are you sure(Y/N)") {
        "yes" {$v1Confirm = "yes"}
        "no" {$v1Confirm = "No"}
        "y" {$v1Confirm = "yes"}
        "n" {$v1Confirm = "No"}
        default {Write-Host "Invalid selection, please try again"}
    }
}
if ($v1Confirm -eq "yes") { Write-Host "do something!" }

Also if you did decide to use V2 take advantage of the ShouldProcess support in script cmdlets.  Wrap your destructive action in a script cmdlet and set the default confirm impact... ala

Cmdlet Write-Message -SupportsShouldProcess -ConfirmImpact high {
    param ([string]$message)
    process {
        if (($pscmdlet.ShouldProcess($env:username, "Send message $message"))){
            Write-Host $message
        }
    }
}

Write-Message "do Something!"

Hope that helps
~Glenn
NiboUser is Offline
New Member
New Member
Posts:2
Avatar

--
15 Dec 2008 03:46 PM  

Thanks both for your replies.

I'm not in a position to upgrade to V2 as these are production servers. However, Glenn's suggested V1 solution works well, so I will use this when I want to avoid being forced into having a default choice.

Nigel

You are not authorized to post a reply.

Active Forums 4.1
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • ShellTools, LLC • Microsoft Windows Server 2008 footer
footer