我试图运行需要管理员输入以处理某些事情的脚本。 而不是让脚本运行失败我试图捕获错误,并把它放回凭证,但我无法找到一个命令,我可以通过本地管理员凭据到一个陷阱。 有没有人有任何可能的工作?
我发现许多将检查域凭据,但是这是一个本地管理员帐户。
为了澄清,我使用:
$Cred = Get-Credential
我需要验证的输出是正确的,具有管理员权限运行的东西在脚本进一步下跌。
工作液(感谢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>
}
这将返回本地管理员(另一种答案可能是更适合这里):
$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")
所有你需要做的是检查凭证都OK,而该用户是管理员组的成员
function Is-Current-User-Admin
{
return ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")
}