Powershell 检查本地管理员凭据 [英] Powershell To Check Local Admin Credentials
问题描述
我正在尝试运行一个需要管理员输入才能处理某些事情的脚本.我不是让脚本运行失败,而是试图捕获错误并将其扔回凭据中,但我找不到可以将本地管理员凭据传递给陷阱的命令.有没有人有任何可能有效的东西?
I'm trying to run a script that requires Administrator input in order to process certain things. Rather than have the script run unsuccessfully I'm trying to trap the error and throw it back into the Credentials, but I can't find a command I can pass Local Admin Credentials with to a Trap. Does anyone have anything that might work?
我发现很多会检查域凭据,但这是一个本地管理员帐户.
I've found MANY that will check domain credentials, but this is a LOCAL Admin account.
为了澄清,我正在使用:
To clarify, I am using:
$Cred = Get-Credential
我需要验证输出是否正确,并且具有管理员访问权限以在脚本中进一步运行内容.
I need to verify the output from that is correct and has Admin access to run stuff further down in the script.
工作解决方案(感谢 User978511)
Working Solution (Thanks to User978511)
$Cred = Get-Credential
$Computer = (gwmi Win32_ComputerSystem).Name
$User = $Cred.Username
$Pass = $Cred.GetNetworkCredential().Password
$Users = ("$Computer"+"$User")
Add-Type -assemblyname System.DirectoryServices.AccountManagement
$DS = New-Object System.DirectoryServices.AccountManagement.PrincipalContext([System.DirectoryServices.AccountManagement.ContextType]::Machine)
$DS.ValidateCredentials($Users, $pass)
if ($Result -ne "True")
{
<Perform Tasks Here>
}
推荐答案
这将返回本地管理员(另一个答案可能更适合这里):
This will return you local admins (another answer is probably better fit here):
$group =[ADSI]"WinNT://./Administrators"
$members = @($group.psbase.Invoke("Members"))
$admins = $members | foreach {$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)}
这将检查凭据:
Add-Type -assemblyname system.DirectoryServices.accountmanagement
$DS = New-Object System.DirectoryServices.AccountManagement.PrincipalContext([System.DirectoryServices.AccountManagement.ContextType]::Machine)
$DS.ValidateCredentials("test", "password")
您所要做的就是检查凭据是否正常以及该用户是否是管理员组的成员
All you have to do is to check that credentials are ok and that user is member of Admins group
这篇关于Powershell 检查本地管理员凭据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!