5

新しいユーザー アカウントが作成されてから 90 日で期限切れになるように設定したいと考えています。ユーザーを作成してすべてを設定するコードは次のとおりです。期限切れに設定しようとしている最後のブロックを除いて、すべてが機能します。

            DirectoryEntry newUser = dirEntry.Children.Add("CN=" + cnUser, "user");
            newUser.Properties["samAccountName"].Value = cnUser;
            newUser.Properties["userPrincipalName"].Value = cnUser;
            newUser.Properties["pwdLastSet"].Value = 0;
            newUser.CommitChanges();

            //Changes Password
            String passwrd = userPassword.ToString();
            newUser.Invoke("SetPassword", new object[] { passwrd });
            newUser.CommitChanges();

            //Sets User Account to Change Passowrd on new login
            newUser.Properties["pwdLastSet"].Value = 0;
            newUser.CommitChanges();

            //Enables account
            newUser.Properties["userAccountControl"].Value = (int)newUser.Properties["userAccountControl"].Value & ~0x2;
            newUser.CommitChanges();

            //Set the account to expire in 90 days
            var dt1 = DateTime.Today.AddDays(90);
            newUser.Properties["accountExpires"].Value = dt1.ToFileTime().ToString();
            newUser.CommitChanges();

彼の仕事を得る方法について何か提案はありますか?

ありがとう

4

3 に答える 3

6

このフィールドに関するドキュメントを参照してください。それを「ティック」に変換する必要があります-

the number of 100-nanosecond intervals since January 1, 1601 (UTC). A value of 0 or 0x7FFFFFFFFFFFFFFF (9223372036854775807) indicates that the account never expires.

new DateTime(DateTime.UtcNow.AddDays(90).Ticks - new DateTime(1601, 1, 1).Ticks)正確で正確な値が得られます。

上記の式から値を取得して実行することで、(手動で) 作業を確認できます。

w32tm.exe /ntte 130149277684873234

上記のコマンドの結果は

150635 17:42:48.4873234 - 6/5/2013 12:42:48 PM
于 2013-03-07T21:08:44.643 に答える
6

または、次のようにすることもできます。

DateTime expire = System.DateTime.Now.AddDays(90);
newUser.Properties["accountExpires"].Value = Convert.ToString((Int64)expire.ToFileTime());
newUser.CommitChanges();

これは、ダニなどをいじるよりも少し簡単に対処できます

于 2015-02-20T02:44:19.010 に答える
0

参考:https ://msdn.microsoft.com/en-us/library/ms180914(v=vs.80).aspx

//Use the DirectoryEntry.InvokeSet method to invoke the AccountExpirationDate property setter.

System.DirectoryServices.DirectoryEntry dirEntryLocalMachine =
    new System.DirectoryServices.DirectoryEntry("WinNT://" + Environment.MachineName + "/" + userID);

dirEntryLocalMachine .InvokeSet("AccountExpirationDate", new object[] {new DateTime(2005, 12, 29)});

//Commit the changes.
usr.CommitChanges();
于 2015-06-04T11:34:59.720 に答える