当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] Get-Credential (Microsoft.PowerShell.Security)

作者:精品下载站 日期:2024-12-14 02:09:24 浏览:15 分类:玩电脑

Get-Credential (Microsoft.PowerShell.Security)


Get-Credential

模块 :Microsoft.PowerShell.Security

根据用户名和密码获取凭证对象。

句法

Get-Credential
   [[-Credential] <PSCredential>]
   [<CommonParameters>]
Get-Credential
   [-Message <String>]
   [[-UserName] <String>]
   [-Title <String>]
   [<CommonParameters>]

描述

Get-Credential cmdlet 为指定的用户名和密码创建凭据对象。您可以在安全操作中使用凭证对象。

Get-Credential cmdlet 提示用户输入密码或用户名和密码。您可以使用 Message 参数为提示指定自定义消息。

在 Windows PowerShell 5.1 及更早版本中,Windows 会显示一个对话框来提示输入用户名和密码。在 PowerShell 6.0 及更高版本中,所有平台的控制台中都会显示提示。

示例

实施例1

$c = Get-Credential

此命令获取一个凭证对象并将其保存在 $c 变量中。

输入命令时,系统会提示您输入用户名和密码。当您输入请求的信息时,cmdlet 将创建一个表示用户凭据的 PSCredential 对象,并将其保存在 $c 变量中。

您可以使用该对象作为请求用户身份验证的 cmdlet(例如带有 Credential 参数的 cmdlet)的输入。但是,某些随 PowerShell 一起安装的提供程序不支持 Credential 参数。

实施例2

$c = Get-Credential -credential User01
$c.Username
User01

此示例创建一个包含用户名但不包含域名的凭据。

第一个命令获取用户名 User01 的凭据并将其存储在 $c 变量中。第二个命令显示生成的凭据对象的用户名属性的值。

实施例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

此示例演示如何创建与 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 变量。确保该值遵循“域\用户”或“计算机名\用户”格式。

第二个命令使用 Read-Host cmdlet 根据用户输入创建安全字符串。 Prompt 参数请求用户输入,AsSecureString 参数屏蔽输入并将其转换为安全字符串。

第三条命令使用 New-Object cmdlet 根据 $User$PWordPSCredential 对象。 /代码> 变量。

实施例5

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:

此命令使用 Get-Credential cmdlet 的 MessageUserName 参数。该命令格式是为共享脚本和函数而设计的。在这种情况下,该消息会告诉用户为什么需要凭据,并让他们相信该请求是合法的。

实施例6

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

指定凭据的用户名,例如 User01Domain01\User01。参数名称 -Credential 是可选的。

当您提交命令并指定用户名时,系统会提示您输入密码。如果省略此参数,系统将提示您输入用户名和密码。

从 PowerShell 3.0 开始,如果输入不带域的用户名,Get-Credential 不再在名称前插入反斜杠。

凭证存储在 PSCredential 对象中,密码存储为 SecureString。

笔记

有关 SecureString 数据保护的更多信息,请参阅 SecureString 的安全性如何?。

类型 :

PS凭证

位置:

1

默认值:

None

必需的:

False

接受管道输入:

False

接受通配符:

False

-Message

指定身份验证提示中显示的消息。此参数设计用于函数或脚本中。您可以使用该消息向用户解释您请求凭据的原因以及它们将如何使用。

此参数是在 PowerShell 3.0 中引入的。

类型 :

String

位置:

命名

默认值:

None

必需的:

False

接受管道输入:

False

接受通配符:

False

-Title

设置控制台中身份验证提示的标题行文本。

此参数是在 PowerShell 6.0 中引入的。

类型 :

String

位置:

命名

默认值:

None

必需的:

False

接受管道输入:

False

接受通配符:

False

-UserName

指定用户名。身份验证提示请求用户名的密码。默认情况下,用户名为空,并且身份验证提示要求输入用户名和密码。

此参数是在 PowerShell 3.0 中引入的。

类型 :

String

位置:

1

默认值:

无(空白)

必需的:

False

接受管道输入:

False

接受通配符:

False

输入

您无法将对象通过管道传输到此 cmdlet。

输出

PSCredential

此 cmdlet 返回一个凭据对象。

笔记

您可以使用 Get-Credential 在请求用户身份验证的 cmdlet 中创建的 PSCredential 对象,例如带有 Credential 参数的 cmdlet。

并非所有与 PowerShell 一起安装的提供程序都支持 Credential 参数。从 PowerShell 3.0 开始,部分 cmdlet 支持它,例如 Get-ContentNew-PSDrive cmdlet。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯