How do I ask for confirmation? [duplicate] - powershell

This question already has answers here:
do until user input yes/no
(3 answers)
How to Use -confirm in PowerShell
(11 answers)
Closed 3 years ago.
I want to create a confirmation box. I want to have a popup box that says that it appears that this is the factory key. Do you wish to continue? and if so proceed with the rest of the script if not, exit script.
The following is my script:
if (Test-Path $USB2\sources\install3.swm) {
$caption = "*** IT APPEARS THIS IS A FACTORY KEY ***"
$message = "Are you Sure You Want To Proceed:"
[int]$defaultChoice = 1
$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", "I understand, flash over it."
$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No"
$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
$choiceRTN = $host.UI.PromptForChoice($caption, $message, $options, $defaultChoice)
if ( $choiceRTN -ne 1 ) {
"Your Choice was Yes"
} else {
Stop-Process -Id $PID
}
}
See the bit, $options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no). Where I am stuck is the "Your Choice was yes" I don't need that... I just need it to continue with the script if yes. How do I do that?

Consider using the System.Windows.MessageBox...
Add-Type -AssemblyName PresentationFramework
[System.Windows.MessageBox]::Show('It appears that this is the factory key. Do you wish to continue?', 'Confirmation', 'YesNo');
For your use case,
# You need this to use System.Windows.MessageBox
Add-Type -AssemblyName 'PresentationFramework'
# By default, you're blasting the USB drive
$continue = 'Yes'
# If you see the file, prompt the user
if ( $(Test-Path -Path "${USB2}\sources\install3.swm") ) {
$caption = "*** IT APPEARS THIS IS A FACTORY KEY ***"
$message = "Are you Sure You Want To Proceed:"
$continue = [System.Windows.MessageBox]::Show($message, $caption, 'YesNo');
}
if ($continue -eq 'Yes') {
# The user either (a) said "yes" to the "do you prompt" or
# (b) the file didn't exist.
Write-Output "Flash over..."
# ...do the flashing here...
} else {
# The user said "no" to the flash prompt
Write-Output "Terminating process..."
Start-Sleep 1
}
The message box will return Yes or No, and you can use that in your control flow to determine next steps. If that includes closing the window, call exit.

The below prompts for input, stores the clicked value in a variable, and returns it as a string. Checking to see the value of $var and proceeding from there should be simple enough.
$var = [System.Windows.MessageBox]::Show('It appears this is a factory key. Do you wish to continue?','DialogTitle','YesNoCancel','Warning')
echo("The value of the dialog box is $($var)")

Related

Comparing a String Variable via PowerShell

I'm trying to create an if and else statement within PowerShell that exits my code if the $value variable has a 0 or 1.
The below is my code which is executed remotely:
$MsgBox = {
Add-Type -AssemblyName PresentationFramework
$ButtonType = [System.Windows.MessageBoxButton]::OKCancel
$MesssageIcon = [System.Windows.MessageBoxImage]::Warning
$MessageBody = "Please save anything open in your current Web Browser, this includes Google Chrome, Microsoft Edge, and Mozilla FireFox and close each one respectively. Click Ok when done saving and closing browsers. IT Will be deleting your Cache and Cookies.
-IT"
$MessageTitle = "Read Me"
$Result = [System.Windows.MessageBox]::Show($MessageBody, $MessageTitle,$ButtonType,$MesssageIcon)
Switch ($Result){
'Ok' {
return "1"
}
'Cancel' {
return "0"
}
}
}
$value = invoke-ascurrentuser -scriptblock $MsgBox -CaptureOutput
$exit = "0"
Write-Host $value
if ($value.Equals($exit)){
Write-Host "Script Exiting"
exit
}
else{
Write-Host "Continuing Script"
}
Am I comparing the wrong way in PowerShell?
I tested the code with Write-Host and my scriptblock returns 0 or 1 as planned.
When it gets to the If and Else statement it doesn't seem to compare value and exit, and will instead go straight to the else statement and Write "Continuing Script"
I've also tried with $value -eq 0 and $value -eq "0".
Invoke-AsCurrentUser, from the third-party RunAsUser module, when used with -CaptureOutput invariably outputs text, as it would appear in the console, given that a call to the PowerShell CLI, is performed behind the scenes (powershell.exe, by default).
The captured text includes a trailing newline; to trim (remove) it, call .TrimEnd() before comparing values:
# .TrimEnd() removes the trailing newline.
$value = (Invoke-AsCurrentuser -scriptblock $MsgBox -CaptureOutput).TrimEnd()
if ($value -eq '0') {
Write-Host "Script Exiting"
exit
}
else {
Write-Host "Continuing Script"
}

PowerShell - Passing hashtables to a function

I'm in the process of writing a CLI for a thing I'm working on, and at various points in the process I want to ask the user for input. Each time I ask the user for input, the questions/answers are likely to be different so I started out with something like:
$choices = [Management.Automation.Host.ChoiceDescription[]] #(
New-Object Management.Automation.Host.ChoiceDescription("&Yes","Option Description for Yes")
New-Object Management.Automation.Host.ChoiceDescription("&No","Option Description for No.")
)
$choice = $Host.UI.PromptForChoice("Question Title","Question Text",$choices,1)
This works pretty well, but it's a bit clunky when it comes to re-use especially if the number of choices expands.
I want to wrap it in a function that I can call easier - like:
$options = #{
Yes = "Option Description for Yes"
No = "Option Description for No"
}
askQuestion -title "Question Title" -question "Question Text" -options $options
So far so good. The bit I'm struggling with is accessing the properties of $options:
function askQuestion {
param (
[hashtable]$options,
[string]$title,
[string]$question
)
Write-Host $title -ForegroundColor Cyan
Write-Host $question -ForegroundColor Cyan
foreach($option in $options)
{
# Do stuff HERE
}
}
If I just access $option directly in the foreach, it prints out a table like:
Name Value
---- -----
No Option Description for No
Yes Option Description for Yes
If I try accessing $option.Name or .Value it doesn't seem to do anything at all.
Can someone point out where I'm going wrong with this please?
I think you can use the GetNumerator() method to iterate through the hash table entries. Then create a custom message using the format operator -f. $i here is just something to track a number for each line in the output. This should be fairly dynamic provided your values/descriptions are consistently worded so there are no grammatical/comprehension issues.
$i = 1
foreach ($option in $options.GetEnumerator()) {
"{2}. Enter {0} for {1}" -f $option.key,$option.value,$i++
}
For anyone interested, this is what this finished up looking like:
function askQuestion {
param (
[hashtable]$options,
[string]$title,
[string]$question
)
$choices = [Management.Automation.Host.ChoiceDescription[]] #(
foreach ($option in $options.GetEnumerator()) {
$selection = $option.key
$description = $option.value
New-Object Management.Automation.Host.ChoiceDescription("&$selection",$description)
}
)
$choice = $Host.UI.PromptForChoice($title,$question,$choices,1)
return
}
And it can be called with something like this, where $options is very flexible.
$options = #{
Yes = "Yes Description"
No = "No Description"
Maybe = "Maybe Description"
}
askQuestion -title "Question Title" -question "Question Text" -options $options

Using Read-host with Input validation and TRAP

I'm just a typical admin trying to make a simple script for some IT assistants in remote offices, to make domain joins easier while minimizing potential errors. The script's end game is to run the one-liner command Add-Computer -DomainName $DomainToJoin -OUPath $LocDN -NewName $WS_NewName -Restart.
But the whole script is supposed to include input validation for the computer's new name as well as for the target OU for the two-letter code for the remote office.
Googling for code snippets for days, esp. from sites like yours, was very helpful. But the problem I have now is I couldn't find the right codes to combine Read-Host , input length validation, and TRAP to work together without losing the value for my variables.
Pardon my coding as obviously I'm no real PS scripter, and I know the wrong portions of it are very basic. I would want to spend more time if I had the luxury, but I would really so much appreciate it if you could point me in the right direction.
Thank you so much in advance.
Please see my code below:
# START: Display name and purpose of invoked script
$path = $MyInvocation.MyCommand.Definition
Clear-Host
Write-Host $path
Write-Host " "
Write-Host "This script will allow you to do the following in a single-step process:"
Write-Host "(1) RENAME this computer"
Write-Host "(2) JOIN it to MYDOMAIN"
Write-Host "(3) MOVE it to a target OU"
Write-Host "(4) REBOOT"
Write-Host " "
Pause
# Function: PAUSE
Function Pause ($Message = "Press any key to continue . . . ") {
if ((Test-Path variable:psISE) -and $psISE) {
$Shell = New-Object -ComObject "WScript.Shell"
$Button = $Shell.Popup("Click OK to continue.", 0, "Script Paused", 0)
}
else {
Write-Host -NoNewline $Message
[void][System.Console]::ReadKey($true)
Write-Host
}
Write-Host " "
}
# Function: Define the parameters
Function Define-Parameters {
# Specify new computer name, with validation and TRAP
$WS_NewName = $null
while ($null -eq $WS_NewName) {
[ValidateLength(8,15)]$WS_NewName = [string](Read-Host -Prompt "NEW NAME of computer (8-15 chars.)" )
TRAP {"" ;continue}
}
Write-Host " "
# Domain to join.
$DomainToJoin = 'mydomain.net'
# Specify the target OU, with validation and trap
$baseOU='OU=Offices OU,DC=mydomain,DC=net'
$OU2 = $null
while ($null -eq $OU2) {
[ValidateLength(2,2)]$OU2 = [string](Read-Host -Prompt 'Target OU (TWO-LETTER code for your office)' )
TRAP {"" ;continue}
}
Write-Host " "
$LocDN = "OU=$OU2,$baseOU"
}
# Function: Summary and confirmation screen for defined parameters.
Function Confirm-Parameters {
Write-Host "==========================================================================="
Write-Host "Please confirm that you are joining this computer to
$DomainToJoin (MYDOMAIN)"
Write-Host "with the following parameters:"
Write-Host ""
Write-Host ""
Write-Host "Computer's NEW NAME: $WS_NewName"
# Write-Host "Domain to Join: $DomainToJoin"
Write-Host "TARGET mission OU: $OU2"
}
# Call Define-Parameters Function
Define-Parameters
# Call Confirm-Parameters Function
Confirm-Parameters
<#
Some more code here
#>
# FINAL COMMAND if all else works: Join the computer to the domain, rename it, and restart it.
# Add-Computer -DomainName $DomainToJoin -OUPath $LocDN -NewName $WS_NewName -Restart
In your code, you have a lot of things defined very strangely. Your functions create a new scope and the variables you're trying to define therein will disappear after calling them unless you change the variable scope (in this case, to $script: or $global:). Also, to use functions, you need to define them first (your Pause doesn't actually do anything)
Here's something you can do with your Define-Parameters function (I suggest looking at Get-Verb)
# Domain to join.
$DomainToJoin = 'mydomain.net'
# Function: Define the parameters
Function Get-Parameters {
do {
$global:WS_NewName = Read-Host -Prompt 'NEW NAME of computer (8-15 chars)'
} until ($WS_NewName.Length -gt 7 -and $WS_NewName.Length -lt 16)
''
do {
$global:OU2 = Read-Host -Prompt 'Target OU (TWO-LETTER code for your office)'
} until ($OU2 -match '^[a-z]{2}$')
''
$OU2 = "OU=$global:OU2,OU=Offices OU,DC=mydomain,DC=net"
}
I'd strongly recommend moving away from the ISE to do your testing and test in an actual powershell console.
Perhaps Try/Catch block instead of trap?
Try {
[ValidatePattern('^\w{8,15}$')]$compname=read-host 'enter comp name' -ErrorAction Stop
[ValidatePattern('^\w{2}$')]$OU=read-host 'enter OU name' -ErrorAction Stop
}
Catch {
$ErrorMessage = $_.Exception.Message
$ErrorLineNumber = $_.InvocationInfo.ScriptLineNumber
$ErrorCommandName = $_.InvocationInfo.InvocationName
Write-Error -Message "The error message was: <$ErrorMessage>, script line: <$ErrorLineNumber>, command name: <$ErrorCommandName>"
exit 255
}

PowerShell PromptForChoice

I have the following problem.
I want to offer the user of my powershell script the choice of what Azure Subscription to use. I've been following this example (included because it also shows the part I can't figure out).
Example
$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)
I can get as far as offering the list with the following
My version
After connecting.....
$title = "Azure subscriptions"
$message = "Please pick a subscription"
$all_subscriptions = Get-AzureRmSubscription
$options = [System.Management.Automation.Host.ChoiceDescription[]]($all_subscriptions.name)
$result = $host.ui.PromptForChoice($title, $message, $options, 0)
I'm aware this is missing the part of the code that specifies the choice which in the example is this
New-Object system.Management.Automation.Host.ChoiceDescription "&Yes", `
"Deletes all the files in the folder."`
I've tried a foreach loop using the $all_subscriptions.name but this (at least how I done this) doesn't seem to work. Can anyone point me in the right direction here. Is PromptForChoice even the right way to do this?
TLDR;
How do I build a dynamic list a user can select from using PromptForChoice within powershell
Assuming you have less than 10 choices to pick from, you can prepend &N and generate the choice descriptions on the fly with hot keys then numbered 1 - 9:
$choiceIndex = 1
$options = $all_subscriptions |ForEach-Object {
New-Object System.Management.Automation.Host.ChoiceDescription "&$($choiceIndex++) - $($_.Name)"
}
$chosenIndex = $host.ui.PromptForChoice($title, $message, $options, 0)
$SubscriptionToUse = $all_subscriptions[$chosenIndex]
Looks like you can just pass a string array as choices.
[string[]]$choices = "machine", "another machine", "the third one", "obviously this one!"
$result = $host.ui.PromptForChoice($title, $message, $choices, 0)
Write-Host $choices[$result] -ForegroundColor Green
$result is an integer representing the indices of the choice. We can then refer back to our array and grab the item at that position and hey-presto - you get your result!

How to Use -confirm in PowerShell

I'm trying to take user input and before proceeding I would like get a message on screen and than a confirmation, whether user wants to proceed or not. I'm using the following code but its not working:
write-host "Are you Sure You Want To Proceed:" -Confirm
-Confirm is a switch in most PowerShell cmdlets that forces the cmdlet to ask for user confirmation. What you're actually looking for is the Read-Host cmdlet:
$confirmation = Read-Host "Are you Sure You Want To Proceed:"
if ($confirmation -eq 'y') {
# proceed
}
or the PromptForChoice() method of the host user interface:
$title = 'something'
$question = 'Are you sure you want to proceed?'
$choices = New-Object Collections.ObjectModel.Collection[Management.Automation.Host.ChoiceDescription]
$choices.Add((New-Object Management.Automation.Host.ChoiceDescription -ArgumentList '&Yes'))
$choices.Add((New-Object Management.Automation.Host.ChoiceDescription -ArgumentList '&No'))
$decision = $Host.UI.PromptForChoice($title, $question, $choices, 1)
if ($decision -eq 0) {
Write-Host 'confirmed'
} else {
Write-Host 'cancelled'
}
Edit:
As M-pixel pointed out in the comments the code could be simplified further, because the choices can be passed as a simple string array.
$title = 'something'
$question = 'Are you sure you want to proceed?'
$choices = '&Yes', '&No'
$decision = $Host.UI.PromptForChoice($title, $question, $choices, 1)
if ($decision -eq 0) {
Write-Host 'confirmed'
} else {
Write-Host 'cancelled'
}
This is a simple loop that keeps prompting unless the user selects 'y' or 'n'
$confirmation = Read-Host "Ready? [y/n]"
while($confirmation -ne "y")
{
if ($confirmation -eq 'n') {exit}
$confirmation = Read-Host "Ready? [y/n]"
}
Read-Host is one example of a cmdlet that -Confirm does not have an effect on.-Confirm is one of PowerShell's Common Parameters specifically a Risk-Mitigation Parameter which is used when a cmdlet is about to make a change to the system that is outside of the Windows PowerShell environment. Many but not all cmdlets support the -Confirm risk mitigation parameter.
As an alternative the following would be an example of using the Read-Host cmdlet and a regular expression test to get confirmation from a user:
$reply = Read-Host -Prompt "Continue?[y/n]"
if ( $reply -eq 'y' ) {
# Highway to the danger zone
}
The Remove-Variable cmdlet is one example that illustrates the usage of the -confirm switch.
Remove-Variable 'reply' -Confirm
Additional References: CommonParameters, Write-Host, Read-Host, Comparison Operators, Regular Expressions, Remove-Variable
Here is the documentation from Microsoft on how to request confirmations in a cmdlet. The examples are in C#, but you can do everything shown in PowerShell as well.
First add the CmdletBinding attribute to your function and set SupportsShouldProcess to true. Then you can reference the ShouldProcess and ShouldContinue methods of the $PSCmdlet variable.
Here is an example:
function Start-Work {
<#
.SYNOPSIS Does some work
.PARAMETER Force
Perform the operation without prompting for confirmation
#>
[CmdletBinding(SupportsShouldProcess=$true)]
param(
# This switch allows the user to override the prompt for confirmation
[switch]$Force
)
begin { }
process {
if ($PSCmdlet.ShouldProcess('Target')) {
if (-not ($Force -or $PSCmdlet.ShouldContinue('Do you want to continue?', 'Caption'))) {
return # user replied no
}
# Do work
}
}
end { }
}
write-host does not have a -confirm parameter.
You can do it something like this instead:
$caption = "Please Confirm"
$message = "Are you Sure You Want To Proceed:"
[int]$defaultChoice = 0
$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", "Do the job."
$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", "Do not do the job."
$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
$choiceRTN = $host.ui.PromptForChoice($caption,$message, $options,$defaultChoice)
if ( $choiceRTN -ne 1 )
{
"Your Choice was Yes"
}
else
{
"Your Choice was NO"
}
For when you want a 1-liner
while( -not ( ($choice= (Read-Host "May I continue?")) -match "^(y|n)$")){ "Y or N ?"}
Write-Warning "This is only a test warning." -WarningAction Inquire
from:
https://serverfault.com/a/1015583/584478
Here's a solution I've used, similiar to Ansgar Wiechers' solution;
$title = "Lorem"
$message = "Ipsum"
$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", "This means Yes"
$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", "This means No"
$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
$result = $host.ui.PromptForChoice($title, $message, $Options, 0)
Switch ($result)
{
0 { "You just said Yes" }
1 { "You just said No" }
}
A slightly prettier function based on Ansgar Wiechers's answer. Whether it's actually more useful is a matter of debate.
function Read-Choice(
[Parameter(Mandatory)][string]$Message,
[Parameter(Mandatory)][string[]]$Choices,
[Parameter(Mandatory)][string]$DefaultChoice,
[Parameter()][string]$Question='Are you sure you want to proceed?'
) {
$defaultIndex = $Choices.IndexOf($DefaultChoice)
if ($defaultIndex -lt 0) {
throw "$DefaultChoice not found in choices"
}
$choiceObj = New-Object Collections.ObjectModel.Collection[Management.Automation.Host.ChoiceDescription]
foreach($c in $Choices) {
$choiceObj.Add((New-Object Management.Automation.Host.ChoiceDescription -ArgumentList $c))
}
$decision = $Host.UI.PromptForChoice($Message, $Question, $choiceObj, $defaultIndex)
return $Choices[$decision]
}
Example usage:
PS> $r = Read-Choice 'DANGER!!!!!!' '&apple','&blah','&car' '&blah'
DANGER!!!!!!
Are you sure you want to proceed?
[A] apple [B] blah [C] car [?] Help (default is "B"): c
PS> switch($r) { '&car' { Write-host 'caaaaars!!!!' } '&blah' { Write-Host "It's a blah day" } '&apple' { Write-Host "I'd like to eat some apples!" } }
caaaaars!!!!
This version asks if the user wants to perform an action before continuing with the rest of the script.
DO
{
$confirmation = Read-Host "Do want Action before continue? [Y/N]"
if ($confirmation -eq 'y') {
write-Host "Doing the Action"
}
} While (($confirmation -ne 'y') -and ($confirmation -ne 'n'))
I prefer a popup.
$shell = new-object -comobject "WScript.Shell"
$choice = $shell.popup("Insert question here",0,"Popup window title",4+32)
If $choice equals 6, the answer was Yes
If $choice equals 7, the answer was No