我想提示用户进行一系列输入,包括密码和文件名。
我有一个使用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?' -AsSecureStringTo 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>).
更多推荐
发布评论