我正在尝试运行一个需要管理员输入才能处理某些事情的脚本。我不想让脚本运行失败,而是尝试捕获错误并将其扔回到凭据中,但我找不到可以将本地管理凭据传递给陷阱的命令。有人有什么可能有用的东西吗?
我发现很多都会检查域凭据,但这是一个本地管理员帐户。
为了澄清,我正在使用:
$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>
}
function Is-Current-User-Admin
{
return ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)