Read-Host http://technet.microsoft.com/en-us/library/hh849945.aspx是从用户获取字符串输入的简单选项。
$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 http://technet.microsoft.com/en-us/library/hh849928.aspx(例如,$results | gm
)。结果是一个字典,其中键是某个名称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>)
.