พรอมต์ PowerShell สำหรับการป้อนข้อมูล | วิธีการต่างๆ พร้อมตัวอย่างที่เหมาะสม
ข้อมูลเบื้องต้นเกี่ยวกับพรอมต์ PowerShell สำหรับการป้อนข้อมูล
ใน PowerShell ผู้ใช้สามารถดึงข้อมูลอินพุตได้โดยแจ้งด้วย Read-Host Cmdlet มันทำหน้าที่เป็น stdin และอ่านอินพุตที่ผู้ใช้จัดทำจากคอนโซล เนื่องจากอินพุตสามารถจัดเก็บเป็นสตริงที่ปลอดภัยได้ จึงสามารถถามรหัสผ่านโดยใช้ cmdlet นี้ ใน PowerShell หรือ ISE ปกติ เครื่องหมายทวิภาคจะแสดงที่ส่วนท้ายของพร้อมท์เพื่อขออินพุตใน ISE ที่ปรับปรุง GUI มากขึ้น ป๊อปอัปจะปรากฏขึ้นพร้อมกับปุ่มบางปุ่ม บทความนี้จะอธิบายรายละเอียดเกี่ยวกับการป้อนข้อมูลของผู้ใช้ใน PowerShell โดยใช้พร้อมท์
ไวยากรณ์:
Read-Host
NAME
Read-Hostไวยากรณ์:
Read-Host [[-Prompt] <Object>] [-AsSecureString] [<CommonParameters>]
ALIASES
Noneพารามิเตอร์:
-AsSecureString:
สิ่งนี้บ่งชี้ว่าอินพุตที่ผู้ใช้พิมพ์ถูกซ่อนด้วย * เมื่อใช้พารามิเตอร์นี้ ผลลัพธ์จะเป็นวัตถุสตริงที่ระบุ ชนิดข้อมูลของพารามิเตอร์นี้คือสวิตช์ ค่าเริ่มต้นคือไม่มี ไม่ยอมรับทั้งอินพุตไปป์ไลน์และอักขระไวด์การ์ด
-อินพุตมาสก์:
นี่เป็นเหมือนพารามิเตอร์สตริงที่ปลอดภัยในการทำงาน ยกเว้นว่าเอาต์พุตที่ส่งคืนโดยสิ่งนี้จะเป็นสตริงและไม่ใช่สตริงที่ปลอดภัย ชนิดข้อมูลของพารามิเตอร์นี้คือสวิตช์ ค่าเริ่มต้นคือไม่มี ไม่ยอมรับทั้งอินพุตไปป์ไลน์และอักขระไวด์การ์ด
-พร้อมท์:
นี่หมายถึงข้อความพร้อมท์ที่ควรแสดงต่อผู้ใช้ นี่จะต้องเป็นสตริง ในกรณีที่มีการเว้นวรรค ควรอยู่ในเครื่องหมายคำพูด ชนิดข้อมูลของพารามิเตอร์นี้เป็นออบเจ็กต์ ค่าเริ่มต้นคือไม่มี ไม่ยอมรับทั้งอินพุตไปป์ไลน์และอักขระไวด์การ์ด
ตัวอย่างของ PowerShell พร้อมท์ให้ป้อนข้อมูล
ตัวอย่าง #1: พรอมต์ปกติ
อินพุต:
Write-Host "Welcome to demo of powershell prompt input" -ForegroundColor Green
$name= Read-Host -Prompt "Enter your name"
$age= Read-Host -Prompt "Enter your age"
$city= Read-Host -Prompt "Enter your city"
Write-Host "The entered name is" $name -ForegroundColor Green
Write-Host "The entered age is" $age -ForegroundColor Green
Write-Host "The entered city is" $city -ForegroundColor Greenเอาต์พุต:
ตัวอย่าง #2: บันทึกอินพุตเป็นสตริงที่ปลอดภัย
อินพุต:
Write-Host "Welcome to demo of powershell prompt input" -ForegroundColor Green
$s1= Read-Host -Prompt "Enter your subject 1 name" -AsSecureString
$s2= Read-Host -Prompt "Enter your subject 2 name" -AsSecureString
$s3= Read-Host -Prompt "Enter your subject 3 name" -AsSecureString
Write-Host "The entered name is" $s1 -ForegroundColor Green
Write-Host "The entered age is" $s2 -ForegroundColor Green
Write-Host "The entered city is" $s3 -ForegroundColor Green
เอาต์พุต:
ตัวอย่าง #3:แบบฟอร์มที่กำหนดเอง
อินพุต:
Write-Host "Demo of custom prompt using form" -ForegroundColor Green
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$testform = New-Object System.Windows.Forms.Form
$testform.Text = 'Data Entry Form'
$testform.Size = New-Object System.Drawing.Size(400,300)
$testform.StartPosition = 'CenterScreen'
$okb = New-Object System.Windows.Forms.Button
$okb.Location = New-Object System.Drawing.Point(85,130)
$okb.Size = New-Object System.Drawing.Size(75,25)
$okb.Text = 'Add'
$okb.DialogResult = [System.Windows.Forms.DialogResult]::OK
$testform.AcceptButton = $okb
$testform.Controls.Add($okb)
$cb = New-Object System.Windows.Forms.Button
$cb.Location = New-Object System.Drawing.Point(170,130)
$cb.Size = New-Object System.Drawing.Size(75,25)
$cb.Text = 'Remove'
$cb.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$testform.CancelButton = $cb
$testform.Controls.Add($cb)
$test = New-Object System.Windows.Forms.Button
$test.Location = New-Object System.Drawing.Point(270,130)
$test.Size = New-Object System.Drawing.Size(75,25)
$test.Text = 'close'
$test.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$testform.AcceptButton = $test
$testform.Controls.Add($test)
$lb = New-Object System.Windows.Forms.Label
$lb.Location = New-Object System.Drawing.Point(20,40)
$lb.Size = New-Object System.Drawing.Size(240,20)
$lb.Text = 'Please enter the information in text box:'
$testform.Controls.Add($lb)
$tb = New-Object System.Windows.Forms.TextBox
$tb.Location = New-Object System.Drawing.Point(40,80)
$tb.Size = New-Object System.Drawing.Size(240,20)
$testform.Controls.Add($tb)
$testform.Topmost = $true
$testform.Add_Shown({$tb.Select()})
$rs = $testform.ShowDialog()
if ($rs -eq [System.Windows.Forms.DialogResult]::OK)
{
$y = $tb.Text
Write-Host "Entered text is" -ForegroundColor Green
$y
}เอาต์พุต:
ตัวอย่าง #4: การแจ้งให้ผู้ใช้ป้อนข้อมูลโดยใช้กล่องโต้ตอบ
อินพุต:
$yeah = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes","Description."
$nah = New-Object System.Management.Automation.Host.ChoiceDescription "&No","Description."
$abort = New-Object System.Management.Automation.Host.ChoiceDescription "&Cancel","Description."
$options = [System.Management.Automation.Host.ChoiceDescription[]]($yeah, $nah, $abort)
$heading = "Demo"
$mess = "are you sure you want to continue?"
$rslt = $host.ui.PromptForChoice($heading, $mess, $options, 1)
switch ($rslt) {
0{
Write-Host "Yes" -ForegroundColor Green
}1{
Write-Host "No" -ForegroundColor Red
}2{
Write-Host "Cancel" -ForegroundColor Red
}
}
$mess = "are you satisfied with out service?"
$rslt = $host.ui.PromptForChoice($heading, $mess, $options, 1)
switch ($rslt) {
0{
Write-Host "Yes" -ForegroundColor Green
}1{
Write-Host "No" -ForegroundColor Red
}2{
Write-Host "Cancel" -ForegroundColor Red
}
}
$mess = "are you sure you want to exit?"
$rslt = $host.ui.PromptForChoice($heading, $mess, $options, 1)
switch ($rslt) {
0{
Write-Host "Yes" -ForegroundColor Green
}1{
Write-Host "No" -ForegroundColor Red
}2{
Write-Host "Cancel" -ForegroundColor Red
}
}
เอาต์พุต:
ตัวอย่าง #5
อินพุต:
Write-Host "Demo of getting confirmation along with prompt from user"
$question1 = Read-Host "do you want to continue"
if ($question1 -eq 'y') {
Write-Host "answer provided is yes" -ForegroundColor Green
}
else
{
Write-Host "answer provided is no" -ForegroundColor Red
}
$question2 = Read-Host "are you a human"
if ($question2 -eq 'y') {
Write-Host "yes, human" -ForegroundColor Green
}
else
{
Write-Host "not a human" -ForegroundColor Red
}
$question33 = Read-Host "do you believe in god"
if ($question1 -eq 'y') {
Write-Host "yes I believe in god" -ForegroundColor Green
}
else
{
Write-Host "no I dont believe in god" -ForegroundColor Red
}เอาต์พุต:
ตัวอย่าง #6
รับอินพุตหลายรายการจากผู้ใช้โดยใช้พร้อมท์
อินพุต:
Write-Host "Demo of getting multiple inputs from user" -ForegroundColor Green
$ainp = @()
do {
$ips = Read-Host -Prompt "Enter ur name"
if ($ips -ne '') {$ainp += $ips}
}
until ($ips -eq 'end')
Write-Host "Entered names are" -ForegroundColor Green
$ainp
Write-Host "Demo of getting multiple user input without loop"
$dummy = "`n"
[string[]] $nl= @()
$nl = READ-HOST -Prompt "enter the names separated by comma"
$nl = $nl.Split(',').Split(' ')
Write-Host "Entered values are" -ForegroundColor Green
$dummy + $nlเอาต์พุต:
บทสรุป
ดังนั้นบทความนี้จึงกล่าวถึงรายละเอียดที่แจ้งให้ผู้ใช้ป้อนข้อมูลใน PowerShell นอกจากจะอธิบายแนวทางต่างๆ แล้ว ยังให้ตัวอย่างอันทรงคุณค่าด้วย มันแสดงให้เห็นหลายวิธีในการรับอินพุตในรูปแบบที่ปลอดภัยและ GUI นอกจากนี้ยังอธิบายวิธีรับค่าอินพุตหลายค่าจากผู้ใช้ที่มีและไม่มีลูป หากต้องการเรียนรู้รายละเอียดเพิ่มเติม แนะนำให้เขียนและฝึกฝนสคริปต์ตัวอย่าง












