[TOC]
[TOC]
描述: 在安全操作中使用该凭证对象;
语法参数:
Get-Credential
[[-Credential] <PSCredential>]
[<CommonParameters>]
Get-Credential
[-Message <String>]
[[-UserName] <String>]
[<CommonParameters>]
基础示例:
# 1.获取凭证对象并将其保存在$c变量中
$cred = Get-Credential -Credential WEIYIGEEK\WeiyiGeek # 该`cmdlet`提示用户输入密码或用户名和密码
# UserName Password
# -------- --------
# WEIYIGEEK\WeiyiGeek System.Security.SecureString
# - PowerShell 采用此种方式未复现成功(貌似已不支持-Credential参数)
# Get-CimInstance Win32_DiskDrive -ComputerName WeiyiGeek -Credential $cred
# Get-CimInstance Win32_BIOS -ComputerName WeiyiGeek -Credential (Get-Credential -Credential WEIYIGEEK\WeiyiGeek)
# 2.此示例创建一个凭证,其中包含一个没有域名的用户名。
$cred = Get-Credential -credential WeiyiGeek
$cred .username
WeiyiGeek
# 3.此命令使用PromptForCredential方法提示用户输入用户名和密码。该命令将生成的凭据保存在$Credential变量中。
# 使用PromptForCredential时,可以指定提示中显示的标题、消息和用户名。
$Credential = $host.ui.PromptForCredential("Need credentials", "Please enter your user name and password.", "WeiyiGeek", "NetBiosUserName")
# 4.此示例演示如何创建与获取凭证返回的对象相同的凭证对象,而不提示用户。此方法需要纯文本密码(这可能违反某些企业的安全标准-通常不建议)。
$User = "Domain01\User01"
$PWord = ConvertTo-SecureString -String "P@sSwOrd" -AsPlainText -Force
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWord
# 5.此命令使用Get-Credential cmdlet的消息和用户名参数
$cred = Get-Credential -User WeiyiGeek -Message "请输入安全认证密码"
# 6.使用凭据进行远程主机管理必须开启 WinRM 服务并且添加远程主机信任。
PS > Get-Service -Name WinRM | Start-Service
PS > Set-Item WSMan:localhost\client\trustedhosts -value 10.20.172.106 -Force
PS > Invoke-Command -ComputerName 10.20.172.106 {Write-Output "Hello" > d:\helo.txt} -Credential $cred3
PS > Invoke-Command -ComputerName 10.20.172.106 {calc}
WeiyiGeek.Credential-remote-use