简体   繁体   English

获取密码过期的 Active Directory 用户列表

[英]get list of Active Directory users with expiring password

Good day!再会!

I know how to see the expiration date of a user's password in ActiveDirectory:我知道如何在 ActiveDirectory 中查看用户密码的到期日期:

  Public Shared Function GetPasswordExpirationDate1(ByVal userId As String) As Date
        Dim forestGc As String = String.Format("GC://{0}", Forest.GetCurrentForest().Name)
        Dim searcher = New DirectorySearcher(New DirectoryEntry(forestGc)) With {
            .Filter = "(sAMAccountName=" & userId & ")"
        }
        Dim results = searcher.FindOne().GetDirectoryEntry()
        Return CDate(results.InvokeGet("PasswordExpirationDate"))
    End Function

question.问题。 How to display a complete list of users whose password will expire after 10 days?如何显示密码将在 10 天后过期的用户的完整列表? List get in DataGridView with fields: sAMAccountName and Password Expiration Date.列出在 DataGridView 中的字段:sAMAccountName 和密码到期日期。

I know since it looks like you are attempting to do this with vb.net you can just run the following PowerShell command and then pipe it's output to wherever you would like. I know since it looks like you are attempting to do this with vb.net you can just run the following PowerShell command and then pipe it's output to wherever you would like. You may have to create a PowerShell instance like the example I have below and then just run the code for doing it with C# in a Win forms application.您可能必须创建一个 PowerShell 实例,如下面的示例,然后在 Win forms 应用程序中运行代码以使用 C# 执行此操作。

Create Powershell RunSpace:创建 Powershell 运行空间:

private string RunScript(string script)
        {
            Runspace runspace = RunspaceFactory.CreateRunspace();
            runspace.Open();
            Pipeline pipeline = runspace.CreatePipeline();
            pipeline.Commands.AddScript(script);
            pipeline.Commands.Add("Out-String");
            Collection<PSObject> results = pipeline.Invoke();
            runspace.Close();
            StringBuilder stringBuilder = new StringBuilder();
            foreach (PSObject pSObject in results)
                stringBuilder.AppendLine(pSObject.ToString());
            return stringBuilder.ToString();
        }

Call powershell commmand into runspace (You can put this code on button click, or someting else):将 powershell 命令调用到运行空间(您可以将此代码放在按钮单击或其他位置):

_ = RunScript("Get-ADUser -filter {Enabled -eq $True -and PasswordNeverExpires -eq 
$False} –Properties "DisplayName", "msDS-UserPasswordExpiryTimeComputed" | Select- 
Object -Property "sAMAccountName",@{Name="ExpiryDate";Expression= 
{[datetime]::FromFileTime($_."msDS-UserPasswordExpiryTimeComputed")}}");

Then pipe the output to the data grid view using the "Out-String".然后 pipe output 使用“Out-String”到数据网格视图。

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

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