简体   繁体   English

Powershell - 本地凭证验证

[英]Powershell - local credential verification

I utilized Powershell To Check Local Admin Credentials as the base for this snippet and am running into problems. 我使用Powershell检查本地管理员凭据作为此代码段的基础并遇到问题。 (didn't want to threadjack) (不想诈骗)

Add-Type -AssemblyName System.DirectoryServices.AccountManagement
$pc = New-Object System.DirectoryServices.AccountManagement.PrincipalContext('machine',$entry)
if($pc.ValidateCredentials("secretadmin", "secretpassword")){Write-host "good"}else{write-host "bad"}

it works about 50 percent of the time. 它约占50%的时间。 the rest of the time, i get this 剩下的时间,我明白了

Exception calling "ValidateCredentials" with "2" argument(s): "Multiple connections to a server or shared resource by the same user, using more than one user name, are not allowed. Disconnect all previous connections to the server or shared resource and try again. 使用“2”参数调用“ValidateCredentials”的异常:“不允许同一用户使用多个用户名与服务器或共享资源建立多个连接。断开以前与服务器或共享资源的所有连接,再试一次。

which reminds me of batch scripts using net use to verify credentials where we just needed to delete the mapped drive to fix the issue. 这让我想起了使用net use验证凭据的批处理脚本,我们只需要删除映射驱动器来解决问题。 since there is no saved object that im aware of, im unsure how to clear this logon. 因为没有我知道的保存对象,我不确定如何清除此登录。 I checked logged on users and saw nothing 我检查登录用户,什么也没看见

Basically, we're building hundreds of servers for hospitals and need to ensure compliance with policies, checking these credentials is just part of this but this is where i have problems. 基本上,我们正在为医院建立数百台服务器,并且需要确保遵守政策,检查这些凭据只是其中的一部分,但这是我遇到问题的地方。

Any help would be greatly appreciated. 任何帮助将不胜感激。 As a side note before these questions are asked. 在提出这些问题之前,请注意一下。 the credentials are correct, they're hard-coded in the script, i can receive a failure and immediately log on via RDP and psexec with the same credentials that throw the error. 凭证是正确的,它们在脚本中是硬编码的,我可以收到故障,并立即通过RDP和psexec使用相同的凭据登录错误。 the firewall allows any from my host to the destination. 防火墙允许从我的主机到目的地。

PrincipalContext implements IDisposable so I recommend doing this: PrincipalContext实现了IDisposable所以我建议这样做:

Add-Type -AssemblyName System.DirectoryServices.AccountManagement
$pc = New-Object DirectoryServices.AccountManagement.PrincipalContext('machine',$entry)

try {
    if($pc.ValidateCredentials("secretadmin", "secretpassword")) {
        Write-host "good"
    }
    else {
        Write-host "bad"
    }
}
finally {
    $pc.Dispose()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM