0

AD内のすべてのコンピューターと、現在ログインしているコンピューターを受信しようとしています。「lastLogonStamp」をチェックしてこれを実行しようとしましたが、サーバーが8日前にADにログインしたため、間違った値が返されます。 。サーバーを再起動しても同じように表示されます。ここで別の質問からコードを取得しました:

すべてのコンピューターと、それらがADに最後にログオンした時刻を一覧表示するにはどうすればよいですか?

public DataTable GetListOfComputers(string domain, string userName, string password)
    {
        DirectoryEntry entry = new DirectoryEntry("LDAP://" + domain,
                userName, password, AuthenticationTypes.Secure);
        DirectorySearcher search = new DirectorySearcher(entry);
        string query = "(objectclass=computer)";
        search.Filter = query;

        search.PropertiesToLoad.Add("name");
        search.PropertiesToLoad.Add("lastLogonTimestamp");

        SearchResultCollection mySearchResultColl = search.FindAll();

        DataTable results = new DataTable();
        results.Columns.Add("name");
        results.Columns.Add("lastLogonTimestamp");

        foreach (SearchResult sr in mySearchResultColl)
        {
            DataRow dr = results.NewRow();
            DirectoryEntry de = sr.GetDirectoryEntry();
            dr["name"] = de.Properties["Name"].Value;
            dr["lastLogonTimestamp"] = DateTime.FromFileTimeUtc(long.Parse(sr.Properties["lastLogonTimestamp"][0].ToString()));
            results.Rows.Add(dr);
            de.Close();
        }

        return results;
    }
4

1 に答える 1

1

.NET 3.5 以降を使用している場合はPrincipalSearcher、「例によるクエリ」プリンシパルを使用して検索を実行できます。

// create your domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

// define a "query-by-example" principal - here, we search for a ComputerPrincipal 
ComputerPrincipal qbeComputer = new ComputerPrincipal(ctx);

// create your principal searcher passing in the QBE principal    
PrincipalSearcher srch = new PrincipalSearcher(qbeComputer);

// find all matches
foreach(var found in srch.FindAll())
{
    // do whatever here - "found" is of type "Principal" - it could be user, group, computer.....          
    ComputerPrincipal cp = found as ComputerPrincipal;

    if(cp != null)
    {
       string computerName = cp.Name;
       DateTime lastLogon = cp.LastLogon;
    }
}

まだお読みでない場合は、.NET Framework 3.5でディレクトリ セキュリティ プリンシパルを管理するという MSDN の記事を必ずお読みくださいSystem.DirectoryServices.AccountManagement。または、System.DirectoryServices.AccountManagement 名前空間に関する MSDN ドキュメントを参照してください。

于 2012-05-10T18:28:08.560 に答える