在PowerShell中提示用户输入(Prompt for user input in PowerShell)

编程入门 行业动态 更新时间:2024-10-23 09:21:14
在PowerShell中提示用户输入(Prompt for user input in PowerShell)

我想提示用户进行一系列输入,包括密码和文件名。

我有一个使用host.ui.prompt的例子,这似乎是明智的,但我无法理解回报。

有没有更好的方法来获得用户在PowerShell中的输入?

I want to prompt the user for a series of inputs, including a password and a filename.

I have an example of using host.ui.prompt, which seems sensible, but I can't understand the return.

Is there a better way to get user input in PowerShell?

最满意答案

Read-Host是从用户获取字符串输入的简单选项。

$name = Read-Host 'What is your username?'

要隐藏您可以使用的密码:

$pass = Read-Host 'What is your password?' -AsSecureString

要将密码转换为纯文本:

[Runtime.InteropServices.Marshal]::PtrToStringAuto( [Runtime.InteropServices.Marshal]::SecureStringToBSTR($pass))

对于$host.UI.Prompt()返回的类型,如果您在@ Christian的评论中发布的链接中运行代码,您可以通过将其传递给Get-Member来找到返回类型(例如, $results | gm )。 结果是一个Dictionary,其中的key是在提示符中使用的FieldDescription对象的名称。 要访问链接示例中第一个提示的结果,您可以键入: $results['String Field'] 。

要访问信息而不调用方法,请保留括号:

PS> $Host.UI.Prompt MemberType : Method OverloadDefinitions : {System.Collections.Generic.Dictionary[string,psobject] Pr ompt(string caption, string message, System.Collections.Ob jectModel.Collection[System.Management.Automation.Host.Fie ldDescription] descriptions)} TypeNameOfValue : System.Management.Automation.PSMethod Value : System.Collections.Generic.Dictionary[string,psobject] Pro mpt(string caption, string message, System.Collections.Obj ectModel.Collection[System.Management.Automation.Host.Fiel dDescription] descriptions) Name : Prompt IsInstance : True

$Host.UI.Prompt.OverloadDefinitions将给你方法的定义。 每个定义显示为<Return Type> <Method Name>(<Parameters>) 。

Read-Host is a simple option for getting string input from a user.

$name = Read-Host 'What is your username?'

To hide passwords you can use:

$pass = Read-Host 'What is your password?' -AsSecureString

To convert the password to plain text:

[Runtime.InteropServices.Marshal]::PtrToStringAuto( [Runtime.InteropServices.Marshal]::SecureStringToBSTR($pass))

As for the type returned by $host.UI.Prompt(), if you run the code at the link posted in @Christian's comment, you can find out the return type by piping it to Get-Member (for example, $results | gm). The result is a Dictionary where the key is the name of a FieldDescription object used in the prompt. To access the result for the first prompt in the linked example you would type: $results['String Field'].

To access information without invoking a method, leave the parentheses off:

PS> $Host.UI.Prompt MemberType : Method OverloadDefinitions : {System.Collections.Generic.Dictionary[string,psobject] Pr ompt(string caption, string message, System.Collections.Ob jectModel.Collection[System.Management.Automation.Host.Fie ldDescription] descriptions)} TypeNameOfValue : System.Management.Automation.PSMethod Value : System.Collections.Generic.Dictionary[string,psobject] Pro mpt(string caption, string message, System.Collections.Obj ectModel.Collection[System.Management.Automation.Host.Fiel dDescription] descriptions) Name : Prompt IsInstance : True

$Host.UI.Prompt.OverloadDefinitions will give you the definition(s) of the method. Each definition displays as <Return Type> <Method Name>(<Parameters>).

更多推荐

本文发布于:2023-08-02 19:26:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1380669.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:提示   用户   PowerShell   input   user

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!