【问题标题】:How can I change the WindowTitle of the Read-Host prompt in the PowerShell ISE?如何更改 PowerShell ISE 中读取主机提示的 WindowTitle?
【发布时间】:2011-05-05 23:05:53
【问题描述】:

我知道如何使用类似的方式更改主 ISE 窗口的标题

$Host.UI.RawUI.WindowTitle = "My Awesome New Window Title"

但我想知道如何访问在读取主机请求时弹出的窗口标题,例如:

$strP4Pass = Read-Host "Please enter Perforce Password" -assecurestring

它以 Windows PowerShell ISE - Input 作为标题弹出 - 是的,我知道我有我的 Please enter Perforce Password 提示 窗口中 - 但我真的希望能够自定义标题 -有什么想法吗?

【问题讨论】:

    标签: powershell powershell-ise


    【解决方案1】:

    您可以使用 System.Management.Automation.Host.ChoiceDescription 创建具有多项选择的自定义提示

    $Yes = New-Object System.Management.Automation.Host.ChoiceDescription "Yes Please"
    $No = New-Object System.Management.Automation.Host.ChoiceDescription "No, Thank you"
    $YesNoChoices = [System.Management.Automation.Host.ChoiceDescription[]]($No,$Yes)
    $Answer = $Host.UI.PromptForChoice("Caption goes here", "Message Goes here", $YesNoChoices, 1)
    

    这将产生与使用 -confirm 或 -whatif 获得的类似 UI,但您可以指定所需的响应。这适用于任何 PowerShell 主机、ISE 或 PowerShell.exe。

    【讨论】:

    • 谢谢!正是我想要的!
    • 为了让这更容易,我创建了一个 PowerShell 模块 get-MultipleChoiceQuestionAnswered 以便我可以编写类似 $Priority = get-MultipleChoiceQuestionAnswered -Question "What priority level should this request have?" -Choices 1,2,3,4 的东西
    【解决方案2】:

    这是一个个人答案,但对我来说,ISE 是一个用于编辑和调试脚本的 WorkFrame。我更喜欢PowerGUI

    它不是执行 scipts 的最终 PowerShell 解释器。因此,如果您想在代码中添加 UI,您可以查看如何在脚本中集成 Windows 窗体或 WPF。它还存在一些模块来帮助您。

    这是关于 WPF 的 Microsoft 系列

    WPF & PowerShell – Part 1 ( Hello World & Welcome to the Week of WPF )

    WPF & PowerShell – Part 2 (Exploring WPF (and the rest of .NET) with Scripts)

    WPF & PowerShell -- Part 3 (Handling Events)

    看看 WPK (WPF PowerShell Toolkit)

    【讨论】:

    • 你说得很好——我不希望我的最终用户使用 ISE——但对于我自己的测试,我使用 KeePass 来自动化很多重复的手动输入,而这将为我自己的易用性创造奇迹。我会查看这些资源,感谢您的指导!
    猜你喜欢
    • 2010-10-01
    • 1970-01-01
    • 2012-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-24
    相关资源
    最近更新 更多