Skip to content

Commit 4cc9fd1

Browse files
committed
Add Get-Credential and SecureString examples to PromptExamples.ps1
1 parent 7579770 commit 4cc9fd1

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

examples/PromptExamples.ps1

+34-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,41 @@
1+
<# ------ Input Prompts ------ #>
2+
3+
$fields = @(
4+
New-Object "System.Management.Automation.Host.FieldDescription" "Input"
5+
New-Object "System.Management.Automation.Host.FieldDescription" "Input List"
6+
)
7+
$fields[1].SetParameterType([int[]])
8+
9+
$host.UI.Prompt("Caption", "Message", $fields)
10+
11+
Get-Credential
12+
Get-Credential -Message "Test!"
13+
Get-Credential -UserName "myuser" -Message "Password stealer"
14+
15+
$host.UI.PromptForCredential("Caption", "Message", $null, $null, [System.Management.Automation.PSCredentialTypes]::Default, [System.Management.Automation.PSCredentialUIOptions]::Default)
16+
$host.UI.PromptForCredential("Caption", "Message", "testuser", $null, [System.Management.Automation.PSCredentialTypes]::Default, [System.Management.Automation.PSCredentialUIOptions]::Default)
17+
18+
Read-Host -AsSecureString
19+
Read-Host -Prompt "Enter a secure string" -AsSecureString
20+
21+
$field = New-Object "System.Management.Automation.Host.FieldDescription" "SecureString"
22+
$field.SetParameterType([SecureString])
23+
$host.UI.Prompt("Caption", "Message", $field)
24+
25+
$field = New-Object "System.Management.Automation.Host.FieldDescription" "PSCredential"
26+
$field.SetParameterType([PSCredential])
27+
$host.UI.Prompt("Caption", "Message", $field)
28+
29+
<# ------ Choice Prompts ------ #>
130

2-
# Multi-choice prompt
331
$choices = @(
432
New-Object "System.Management.Automation.Host.ChoiceDescription" "&Apple", "Apple"
533
New-Object "System.Management.Automation.Host.ChoiceDescription" "&Banana", "Banana"
634
New-Object "System.Management.Automation.Host.ChoiceDescription" "&Orange", "Orange"
735
)
836

9-
$defaults = [int[]]@(0, 2)
10-
$host.UI.PromptForChoice("Choose a fruit", "You may choose more than one", $choices, $defaults)
37+
# Single-choice prompt
38+
$host.UI.PromptForChoice("Choose a fruit", "You may choose one", $choices, 1)
39+
40+
# Multi-choice prompt
41+
$host.UI.PromptForChoice("Choose a fruit", "You may choose more than one", $choices, [int[]]@(0, 2))

0 commit comments

Comments
 (0)