检查用户密码输入在 Powershell 脚本中是否有效

Dol*_*kle 33 powershell password domain

我正在使用一个 Powershell 脚本,该脚本将计划任务添加到我们域中的系统中。当我运行这个脚本时,它会提示我输入密码。有时我会粗指密码并启动该过程,从而锁定我的帐户。有没有办法验证我的凭据以确保我输入的内容将通过域进行验证?

我想找到一种查询域控制器的方法。我已经进行了一些 Google 搜索,我应该能够执行 WMI 查询并捕获错误。如果可能,我想避免这种验证方式。

有任何想法吗?提前致谢。

Jim*_*m B 30

我的图书馆里有这个:

$cred = Get-Credential #Read credentials
 $username = $cred.username
 $password = $cred.GetNetworkCredential().password

 # Get current domain using logged-on user's credentials
 $CurrentDomain = "LDAP://" + ([ADSI]"").distinguishedName
 $domain = New-Object System.DirectoryServices.DirectoryEntry($CurrentDomain,$UserName,$Password)

if ($domain.name -eq $null)
{
 write-host "Authentication failed - please verify your username and password."
 exit #terminate the script.
}
else
{
 write-host "Successfully authenticated with domain $domain.name"
}
Run Code Online (Sandbox Code Playgroud)

  • 6年前还没有活动目录模块 (3认同)

jbs*_*ith 18

这是我过去使用的;它应该适用于本地计算机帐户和“应用程序目录”,但到目前为止,我只成功地使用了 AD 凭据:

    function Test-Credential {
    <#
    .SYNOPSIS
        Takes a PSCredential object and validates it against the domain (or local machine, or ADAM instance).

    .PARAMETER cred
        A PScredential object with the username/password you wish to test. Typically this is generated using the Get-Credential cmdlet. Accepts pipeline input.

    .PARAMETER context
        An optional parameter specifying what type of credential this is. Possible values are 'Domain','Machine',and 'ApplicationDirectory.' The default is 'Domain.'

    .OUTPUTS
        A boolean, indicating whether the credentials were successfully validated.

    #>
    param(
        [parameter(Mandatory=$true,ValueFromPipeline=$true)]
        [System.Management.Automation.PSCredential]$credential,
        [parameter()][validateset('Domain','Machine','ApplicationDirectory')]
        [string]$context = 'Domain'
    )
    begin {
        Add-Type -assemblyname system.DirectoryServices.accountmanagement
        $DS = New-Object System.DirectoryServices.AccountManagement.PrincipalContext([System.DirectoryServices.AccountManagement.ContextType]::$context) 
    }
    process {
        $DS.ValidateCredentials($credential.UserName, $credential.GetNetworkCredential().password)
    }
}
Run Code Online (Sandbox Code Playgroud)