[玩转系统] PowerShell 提示输入 |各种方法和适当的例子
作者:精品下载站 日期:2024-12-14 05:00:28 浏览:15 分类:玩电脑
PowerShell 提示输入 |各种方法和适当的例子
PowerShell输入提示简介
在 PowerShell 中,用户可以通过使用 Read-Host Cmdlet 提示来检索输入。它充当标准输入并读取用户从控制台提供的输入。由于输入也可以存储为安全字符串,因此可以使用此 cmdlet 提示输入密码。在普通的 PowerShell 或 ISE 中,在 GUI 增强型 ISE 中,请求输入的提示末尾会显示冒号;将显示一个弹出窗口以及一些按钮。本文将详细解释如何使用提示符在 PowerShell 中获取用户输入。
语法:
Read-Host
NAME
Read-Host
语法:
Read-Host [[-Prompt] <Object>] [-AsSecureString] [<CommonParameters>]
ALIASES
None
参数:
-AsSecureString:
这表明用户键入的输入被 * 隐藏。使用此参数时,输出是特定的字符串对象。该参数的数据类型为Switch。默认值为无。不接受管道输入和通配符。
-掩码输入:
这类似于功能中的安全字符串参数,只不过它返回的输出是字符串而不是安全字符串。该参数的数据类型为Switch。默认值为无。不接受管道输入和通配符。
-提示:
这表示应向用户显示的提示文本。这需要是一个字符串。如果有空格,则应将它们括在引号内。该参数的数据类型是对象。默认值为无。不接受管道输入和通配符。
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 获取输入的多种方法。它还解释了如何使用和不使用循环从用户那里获取多个输入值。要了解更多详细信息,建议编写和练习示例脚本。
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag