Get-Credential
获取基于用户名和密码的凭据对象。
语法
Get-Credential
[-Credential] <PSCredential>
[<CommonParameters>]
Get-Credential
-Message <String>
[[-UserName] <String>]
[<CommonParameters>]
说明
Get-Credential
cmdlet 为指定的用户名和密码创建凭据对象。 可以在安全作中使用凭据对象。
从 PowerShell 3.0 开始,可以使用 Message 参数在对话框中指定自定义消息,提示用户输入其名称和密码。
Get-Credential
cmdlet 会提示用户输入密码或用户名和密码。 默认情况下,将显示一个身份验证对话框来提示用户。 但是,在某些主机程序(如 PowerShell 控制台)中,可以通过更改注册表项在命令行中提示用户。 有关此注册表项的详细信息,请参阅说明和示例。
示例
示例 1
$c = Get-Credential
此命令获取凭据对象并将其保存在 $c
变量中。
输入命令时,将显示一个请求用户名和密码的对话框。 输入请求的信息时,该 cmdlet 将创建一个 PSCredential 对象,该对象表示用户的凭据,并将其保存在 $c
变量中。
可以将对象用作请求用户身份验证的 cmdlet 的输入,例如具有 Credential 参数的 cmdlet。 但是,某些随 PowerShell 一起安装的提供程序不支持 Credential 参数。
示例 2
$c = Get-Credential -Credential User01
$c.Username
User01
此示例创建一个凭据,其中包含没有域名的用户名。
第一个命令获取用户名 User01 的凭据,并将其存储在 $c
变量中。
第二个命令显示生成的凭据对象的 Username 属性的值。
示例 3
$Credential = $Host.UI.PromptForCredential("Need credentials", "Please enter your user name and password.", "", "NetBiosUserName")
此命令使用 PromptForCredential 方法提示用户输入其用户名和密码。 该命令将生成的凭据保存在 $Credential
变量中。
PromptForCredential 方法是使用 Get-Credential
cmdlet 的替代方法。 使用 PromptForCredential时,可以指定消息框中显示的标题、消息和用户名。
有关详细信息,请参阅 SDK 中的 PromptForCredential 文档。
示例 4
Set-ItemProperty "HKLM:\SOFTWARE\Microsoft\PowerShell\1\ShellIds" -Name ConsolePrompting -Value $true
此示例演示如何修改注册表,以便在命令行中提示用户,而不是使用对话框。
该命令创建 ConsolePrompting 注册表项,并将其值设置为 True。 若要运行此命令,请使用“以管理员身份运行”选项启动 PowerShell。
若要使用对话框进行提示,请将 ConsolePrompting 的值设置为 false($false),或使用 Remove-ItemProperty
cmdlet 将其删除。
ConsolePrompting 注册表项适用于某些主机程序,例如 PowerShell 控制台。 它可能无法在所有主机程序中工作。
示例 5
此示例演示如何创建与 Get-Credential
返回的凭据对象相同的凭据对象。
$User = "Domain01\User01"
$PWord = Read-Host -Prompt 'Enter a Password' -AsSecureString
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWord
第一个命令将用户名分配给 $User
变量。 确保该值遵循“Domain\User”或“ComputerName\User”格式。
第二个命令使用 Read-Host
cmdlet 从用户输入创建安全字符串。
Prompt 参数请求用户输入,AsSecureString 参数会掩码输入并将其转换为安全字符串。
第三个命令使用 New-Object
cmdlet 从存储在 和 $User
变量中的值创建 $PWord
对象。
示例 6
Get-Credential -Message "Credential are required for access to the \\Server1\Scripts file share." -User Server01\PowerUser
PowerShell Credential Request
Credential are required for access to the \\Server1\Scripts file share.
Password for user Server01\PowerUser:
此命令使用 cmdlet 的 Message 和 Get-Credential
参数。 此命令格式专为共享脚本和函数而设计。 在这种情况下,该消息告知用户需要凭据的原因,并让他们确信请求是合法的。
示例 7
Invoke-Command -ComputerName Server01 {Get-Credential Domain01\User02}
PowerShell Credential Request : PowerShell Credential Request
Warning: This credential is being requested by a script or application on the SERVER01 remote computer. Enter your credentials only if you
trust the remote computer and the application or script requesting it.
Enter your credentials.
Password for user Domain01\User02: ***************
PSComputerName : Server01
RunspaceId : 422bdf52-9886-4ada-ab2f-130497c6777f
PSShowComputerName : True
UserName : Domain01\User01
Password : System.Security.SecureString
此命令从 Server01 远程计算机获取凭据。 该命令使用 Invoke-Command
cmdlet 在远程计算机上运行 Get-Credential
命令。 输出显示 Get-Credential
身份验证提示中包含的远程安全消息。
参数
-Credential
指定凭据的用户名,例如 User01 或 Domain01\User01。 参数名称 -Credential
是可选的。
提交命令并指定用户名时,系统会提示输入密码。 如果省略此参数,系统会提示输入用户名和密码。
从 PowerShell 3.0 开始,如果在没有域的情况下输入用户名,Get-Credential
不再在名称前插入反斜杠。
凭据存储在 PSCredential 对象中,密码存储为 SecureString。
注意
有关 SecureString 数据保护的详细信息,请参阅 SecureString 的安全性如何?。
类型: | PSCredential |
Position: | 1 |
默认值: | None |
必需: | True |
接受管道输入: | False |
接受通配符: | False |
-Message
指定在身份验证提示中显示的消息。 此参数旨在用于函数或脚本。 可以使用消息向用户解释你请求凭据的原因及其使用方式。
此参数是在 PowerShell 3.0 中引入的。
类型: | String |
Position: | Named |
默认值: | None |
必需: | True |
接受管道输入: | False |
接受通配符: | False |
-UserName
指定用户名。 身份验证提示请求用户名的密码。 默认情况下,用户名为空,身份验证提示请求用户名和密码。
当身份验证提示出现在对话框中时,用户可以编辑指定的用户名。 但是,当提示出现在命令行时,用户无法更改用户名。 在共享函数或脚本中使用此参数时,请考虑所有可能的演示文稿。
此参数是在 PowerShell 3.0 中引入的。
类型: | String |
Position: | 1 |
默认值: | None (blank) |
必需: | False |
接受管道输入: | False |
接受通配符: | False |
输入
None
不能通过管道将对象传递给此 cmdlet。
输出
此 cmdlet 返回凭据对象。
备注
可以使用 在请求用户身份验证的 cmdlet 中创建的 Get-Credential
对象,例如具有 Credential 参数的对象。
默认情况下,身份验证提示将显示在对话框中。 若要在命令行中显示身份验证提示,请添加 ConsolePrompting 注册表项(HKLM:\SOFTWARE\Microsoft\PowerShell\1\ShellIds\ConsolePrompting
),并将其值设置为 True。
如果 ConsolePrompting 注册表项不存在,或者其值为 False,身份验证提示将显示在对话框中。 有关说明,请参阅示例。
ConsolePrompting 注册表项在 PowerShell 控制台中工作,但它在所有主机程序中都不起作用。
例如,它在 PowerShell 集成脚本环境(ISE)中不起作用。 有关 ConsolePrompting 注册表项的效果的信息,请参阅主机程序的帮助主题。
所有随 PowerShell 一起安装的提供程序都不支持 Credential 参数。
从 PowerShell 3.0 开始,支持选择 cmdlet,例如 Get-Content
和 New-PSDrive
cmdlet。