PowerShell 脚本开发:用户交互、进度更新与文化感知
1. 创建选择列表
在 PowerShell 中,你可以创建一个选择列表,让用户从多个选项中进行选择。以下是创建选择列表的代码示例:
# 创建选择集合
$choices = New-Object `
Collections.ObjectModel.Collection[Management.Automation.Host.ChoiceDescription]
# 遍历每个选项,并将其添加到选择集合中
for($counter = 0; $counter -lt $option.Length; $counter++)
{
$choice = New-Object Management.Automation.Host.ChoiceDescription `
$option[$counter]
if($helpText -and $helpText[$counter])
{
$choice.HelpMessage = $helpText[$counter]
}
$choices.Add($choice)
}
# 提示用户进行选择,并返回用户选择的项
$host.UI.PromptForChoice($caption, $message, $choices, $default)
这个代码块首先创建了一个选择集合,然后遍历每个选项,将其添加到集合中。如果有帮助文本,还会为每个选项设置帮助信息。最后,使用 $host.UI.PromptForChoic
超级会员免费看
订阅专栏 解锁全文
54

被折叠的 条评论
为什么被折叠?



