PowerShell PromptForChoice - powershell

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!

Related

Powershell PowerPoint Header

I have a simple script that creates a Word Document and a PowerPoint document for staff that are sitting an exam where the files needed are pre loaded to a user area on a file share. The Word Document opens an creates the document header with the line of code
$Header = $Section.Headers.Item(1);
$Header.Range.Text = "$FirstName $SecondName $ID Activity 1";
PowerPoint is a bit more funnier and when you add a header it asks for dates. Is there a way to do this with powershell? We are printing them afterwards so would be a lot more benificial to have the variables that are in the CSV printed in the header of the document.
If this isn't doable is there a way to edit the first slide to have the variables in the first slide of the PowerPoint.
If anyone is able to look at the code or any ways of simplifying it, it would be massively appreiciated
The CSV is listed below and the full script is at the bottom :)
ID Four First Second
219999 9999 Tech Support
Have a good day
$Exams = Import-Csv "C:\\techtest.csv"
$fileserver = "C:\\ExamHomes\"
foreach ($User in $Exams)
{
$FirstName = $User.First
$SecondName = $User.Second
$ID = $User.ID
$FourID = $User.Four
$Time = "am"
$Date = "1511"
$Word = New-Object -ComObject Word.Application;
$Word.Visible = $false;
$Doc = $Word.Documents.Add();
$Section = $Doc.Sections.Item(1);
$Header = $Section.Headers.Item(1);
$Header.Range.Text = "$FirstName $SecondName $ID Activity 1";
$Doc.SaveAs("$fileserver\${date}${time}-${FourID}\Desktop\activity 1_${ID}_${FirstName}_${SecondName}.docx");
$Word.Quit()
Write-Host "File 'activity 1_${ID}_${FirstName}_${SecondName}.docx' for $FirstName $SecondName has been created and sent to the folder ${date}${time}-${FourID}" -BackgroundColor Black -ForegroundColor Cyan
add-type -assembly microsoft.office.interop.powerpoint
$Application = New-Object -ComObject powerpoint.application
$slideType = "microsoft.office.interop.powerpoint.ppSlideLayout" -as [type]
$presentation = $application.Presentations.add()
$presentation.SaveAs("$fileserver\${date}${time}-${FourID}\Desktop\activity 1_${ID}_${FirstName}_${SecondName}.pptx")
$presentation.close()
Stop-Process -name POWERPNT -Force
Write-Host "File 'activity 1_${ID}_${FirstName}_${SecondName}.pptx' for $FirstName $SecondName has been created and sent to the folder ${date}${time}-${FourID}" -BackgroundColor Black -ForegroundColor Cyan
}
Write-Host "All Staff have successfully had their document deployed for the exam" -BackgroundColor Black -ForegroundColor Red
Headers are only available for Notes Master and Handout Master objects
But you can add a Footer on a slide.
Add-Type -AssemblyName microsoft.office.interop.powerpoint
$Application = New-Object -ComObject powerpoint.application
$presentation = $application.Presentations.add()
$slide = $presentation.Slides.Add(1, [Microsoft.Office.Interop.PowerPoint.PpSlideLayout]::ppLayoutTitle)
$slide.Shapes.Title.TextFrame.TextRange.Text = "My Title"
$slide.Shapes(2).TextFrame.TextRange.Text = "My SubTitle"
$slide.HeadersFooters.Footer.Visible = $true
$slide.HeadersFooters.Footer.Text = "My Name"
You can use some Text on the DateAndTime HeaderFooter object like this:
$slide.HeadersFooters.DateAndTime.Visible = $true
$slide.HeadersFooters.DateAndTime.UseFormat = $true
$slide.HeadersFooters.DateAndTime.Format = [Microsoft.Office.Interop.PowerPoint.PpDateTimeFormat]::ppDateTimeFigureOut
$slide.HeadersFooters.DateAndTime.Text = "Coucou"
EDIT: SLIDE MASTER
In fact this can be defined on the SlideMaster so that every Slide inherit the values from the footer, pagenumber or datetime.
This is defined at the presentation level. So depending what you want do it on the SlideMaster or on any Slide you create. Probably a Slide may override the values from the SlideMaster.
$presentation.SlideMaster.HeadersFooters.SlideNumber.Visible = $true
$presentation.SlideMaster.HeadersFooters.Footer.Visible = $true
$presentation.SlideMaster.HeadersFooters.Footer.Text = "My Name"
$presentation.SlideMaster.HeadersFooters.DateAndTime.Visible = $true
$presentation.SlideMaster.HeadersFooters.DateAndTime.UseFormat = $true
$presentation.SlideMaster.HeadersFooters.DateAndTime.Format = [Microsoft.Office.Interop.PowerPoint.PpDateTimeFormat]::ppDateTimeFigureOut
$presentation.SlideMaster.HeadersFooters.DateAndTime.Text = "Something"

How do I ask for confirmation? [duplicate]

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)")

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

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

Console Window closes

I have a application with arguments that I do not have source code for that I am trying run from Powershell.
I can get it to run but it closes quickly and nothing happens. I assume it it erroring because the file path I have is bogus. How do I keep it open to see the error?
$*folder= ''
$title = "Organization"
$message = "Which Organization are you source controlling?"
$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Donaldson", "Donaldson"
$no = New-Object System.Management.Automation.Host.ChoiceDescription "&US", "US."
$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
$result = $host.ui.PromptForChoice($title, $message, $options, 0)
switch ($result)
{
0 {$args = $action -and "/zipfile:`"\\nasprod\prod$\dciprod\mscrm\customizations\Release\Global.zip`" /folder:`"C:\Users\mchaffee\Documents\Code\CRM\Global`"" }
1 {$args = $action -and "/zipfile:`"\\nasprod\prod$\dciprod\mscrm\customizations\Release\US.zip`" /folder:`"C:\Users\mchaffee\Documents\Code\CRM\US`"" }
}
Start-Process "C:\Users\mchaffee\Documents\sdk\bin\solutionpackager.exe" $args*
Check out this help about start-process - http://technet.microsoft.com/library/hh849848.aspx. It has -wait switch. Try it if that works out for you.
PS C:\> start-process notepad -wait