如果用户名/密码不正确,如何重新提示凭证框?
Import-Module ActiveDirectory
Write-Host "Please enter Admin credential..."
$Creds = Get-Credential
我的脚本甚至在输入错误的用户名/ pw(w错误地显示错误)后运行每行
每次用户/传递无效时我都需要Get-Credential
出现,因此每次输入错误信息时我都不会重新运行相同的脚本。
更新:
下面是示例脚本:
Import-Module ActiveDirectory
Write-Host "Please enter Admin credential..."
$Creds = Get-Credential
$Tpm = Get-wmiobject -ComputerName $wsid -Credential $Creds -Namespace ROOT\CIMV2\Security\MicrosoftTpm -Class Win32_Tpm
我需要获取另一台计算机的TPM,并且需要输入user / pw才能完成这项工作。我需要的是验证用户/名称pw是否正确,如果没有,请重新启动Get-Credentials
屏幕。
答案 0 :(得分:0)
您无法使用get-credential验证凭据,而是可以对get-wmiobject使用try catch语句,例如
$c=get-credential
try{
gwmi -ComputerName comp1 -Credential $c -Class win32_bios
}
catch [System.UnauthorizedAccessException]{
"wrong credential retry please"
$c=get-credential
}
答案 1 :(得分:0)
function Test-Credentials() {
param (
[Parameter(Mandatory = $false)]
[PsCredential]$Creds
)
$IDX = 0 # Retry Limit Counter
$limit = 3 # Retry Limiter
$DS = New-Object System.DirectoryServices.AccountManagement.PrincipalContext('domain')
while (!$DS.ValidateCredentials($Creds.UserName, $Creds.GetNetworkCredential().password) -and ($IDX -lt $limit)) {
$IDX++
Write-Host " - Failed - " -ForegroundColor Yellow
$Creds = Get-Credential -Message "Validaton Failed: Please Re-enter your credentials" -UserName $Creds.UserName
}
return $Creds