Feature: AD Update Last Network Logon Date

Ability to run this task on-demand from the web UI is added
This commit is contained in:
Gary Sharp
2013-02-07 18:30:51 +11:00
parent a9bbcabd87
commit c7cbef9189
4 changed files with 1297 additions and 1327 deletions
@@ -40,7 +40,7 @@ namespace Disco.BI.Interop.ActiveDirectory
UpdateLastNetworkLogonDates(dbContext, this.Status); UpdateLastNetworkLogonDates(dbContext, this.Status);
this.Status.UpdateStatus(95, "Updating Database", "Writing last network logon dates to the Database"); this.Status.UpdateStatus(95, "Updating Database", "Writing last network logon dates to the Database");
changeCount = dbContext.SaveChanges(); changeCount = dbContext.SaveChanges();
this.Status.UpdateStatus(100, "Finished", string.Format("{0} Device last network logon dates updated", changeCount)); this.Status.Finished(string.Format("{0} Device last network logon dates updated", changeCount), "/Config/SystemConfig");
} }
SystemLog.LogInformation(new string[] SystemLog.LogInformation(new string[]
@@ -50,44 +50,16 @@ namespace Disco.BI.Interop.ActiveDirectory
}); });
} }
//public void InitalizeScheduledTask(DiscoDataContext dbContext, IScheduler Scheduler) public static ScheduledTaskStatus ScheduleImmediately()
//{ {
// // UpdateLastNetworkLogonDates @ 11:30pm var existingTask = ScheduledTasks.GetTaskStatuses(typeof(ActiveDirectoryUpdateLastNetworkLogonDateJob)).Where(s => s.IsRunning).FirstOrDefault();
// IJobDetail jobDetail = new JobDetailImpl("UpdateLastNetworkLogonDates", typeof(ActiveDirectoryUpdateLastNetworkLogonDateJob)); if (existingTask != null)
// ITrigger trigger = TriggerBuilder.Create(). return existingTask;
// WithIdentity("UpdateLastNetworkLogonDatesTrigger").
// StartNow(). var instance = new ActiveDirectoryUpdateLastNetworkLogonDateJob();
// WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(23, 30)). return instance.ScheduleTask();
// Build(); }
// Scheduler.ScheduleJob(jobDetail, trigger);
//}
//void IJob.Execute(IJobExecutionContext context)
//{
// DiscoDataContext dbContext = new DiscoDataContext();
// try
// {
// ActiveDirectoryUpdateLastNetworkLogonDateJob.UpdateLastNetworkLogonDates(dbContext);
// int changeCount = dbContext.SaveChanges();
// SystemLog.LogInformation(new string[]
// {
// "Updated LastNetworkLogon Device Property for Device/s",
// changeCount.ToString()
// });
// }
// catch (System.Exception ex)
// {
// SystemLog.LogException("ActiveDirectoryUpdateLastNetworkLogonDateJob", ex);
// }
// finally
// {
// bool flag = dbContext != null;
// if (flag)
// {
// ((System.IDisposable)dbContext).Dispose();
// }
// }
//}
public static bool UpdateLastNetworkLogonDate(Device Device) public static bool UpdateLastNetworkLogonDate(Device Device)
{ {
System.DateTime? computerLastLogonDate = Device.LastNetworkLogonDate; System.DateTime? computerLastLogonDate = Device.LastNetworkLogonDate;
@@ -178,7 +150,7 @@ namespace Disco.BI.Interop.ActiveDirectory
UpdateLastNetworkLogonDate = false; UpdateLastNetworkLogonDate = false;
return UpdateLastNetworkLogonDate; return UpdateLastNetworkLogonDate;
} }
public static void UpdateLastNetworkLogonDates(DiscoDataContext context, ScheduledTaskStatus status = null) private static void UpdateLastNetworkLogonDates(DiscoDataContext context, ScheduledTaskStatus status)
{ {
System.Collections.Generic.Dictionary<string, System.DateTime> computerLastLogonDates = new System.Collections.Generic.Dictionary<string, System.DateTime>(); System.Collections.Generic.Dictionary<string, System.DateTime> computerLastLogonDates = new System.Collections.Generic.Dictionary<string, System.DateTime>();
@@ -202,10 +174,7 @@ namespace Disco.BI.Interop.ActiveDirectory
using (DirectoryEntry dRootEntry = ActiveDirectoryHelpers.DefaultDCLdapRoot(dcName)) using (DirectoryEntry dRootEntry = ActiveDirectoryHelpers.DefaultDCLdapRoot(dcName))
{ {
double progressDCStart = 5 + (progressDCCount * progressDCProgress); double progressDCStart = 5 + (progressDCCount * progressDCProgress);
if (status != null)
{
status.UpdateStatus(progressDCStart, string.Format("Querying Domain Controller: {0}", dcName), "Searching..."); status.UpdateStatus(progressDCStart, string.Format("Querying Domain Controller: {0}", dcName), "Searching...");
}
using (DirectorySearcher dSearcher = new DirectorySearcher(dRootEntry, "(objectClass=computer)", new string[] { "sAMAccountName", "lastLogon" }, SearchScope.Subtree)) using (DirectorySearcher dSearcher = new DirectorySearcher(dRootEntry, "(objectClass=computer)", new string[] { "sAMAccountName", "lastLogon" }, SearchScope.Subtree))
{ {
@@ -222,7 +191,6 @@ namespace Disco.BI.Interop.ActiveDirectory
{ {
string computerName = ((string)dProp[0]).TrimEnd(new char[] { '$' }).ToUpper(); string computerName = ((string)dProp[0]).TrimEnd(new char[] { '$' }).ToUpper();
if (status != null)
if (progressItemCount % 150 == 0) // Only Update Status every 150 devices if (progressItemCount % 150 == 0) // Only Update Status every 150 devices
status.UpdateStatus(progressDCStart + (progressItemProgress * progressItemCount), string.Format("Analysing Device: {0}", computerName)); status.UpdateStatus(progressDCStart + (progressItemProgress * progressItemCount), string.Format("Analysing Device: {0}", computerName));
@@ -19,19 +19,9 @@ namespace Disco.Web.Areas.API.Controllers
public virtual ActionResult UpdateLastNetworkLogonDates() public virtual ActionResult UpdateLastNetworkLogonDates()
{ {
//ActiveDirectoryUpdateLastNetworkLogonDateJob updateJob = new ActiveDirectoryUpdateLastNetworkLogonDateJob(); var taskStatus = ActiveDirectoryUpdateLastNetworkLogonDateJob.ScheduleImmediately();
ActiveDirectoryUpdateLastNetworkLogonDateJob.UpdateLastNetworkLogonDates(dbContext); return RedirectToAction(MVC.Config.Logging.TaskStatus(taskStatus.SessionId));
var resultCount = dbContext.SaveChanges();
var model = new Models.System.UpdateLastNetworkLogonDatesModel()
{
Result = "OK",
UpdateCount = resultCount
};
return Json(model, JsonRequestBehavior.AllowGet);
} }
public virtual ActionResult UpdateAttachmentThumbnails() public virtual ActionResult UpdateAttachmentThumbnails()
@@ -173,6 +173,7 @@
</table> </table>
</div> </div>
<div class="actionBar"> <div class="actionBar">
@Html.ActionLinkButton("Update Device Last Network Logons", MVC.API.System.UpdateLastNetworkLogonDates())
<input type="submit" class="button" value="Save Configuration" /> <input type="submit" class="button" value="Save Configuration" />
</div> </div>
} }
@@ -603,7 +603,18 @@ WriteLiteral(" <div");
WriteLiteral(" class=\"actionBar\""); WriteLiteral(" class=\"actionBar\"");
WriteLiteral(">\r\n <input"); WriteLiteral(">\r\n");
WriteLiteral(" ");
#line 176 "..\..\Areas\Config\Views\SystemConfig\Index.cshtml"
Write(Html.ActionLinkButton("Update Device Last Network Logons", MVC.API.System.UpdateLastNetworkLogonDates()));
#line default
#line hidden
WriteLiteral("\r\n <input");
WriteLiteral(" type=\"submit\""); WriteLiteral(" type=\"submit\"");
@@ -614,7 +625,7 @@ WriteLiteral(" value=\"Save Configuration\"");
WriteLiteral(" />\r\n </div>\r\n"); WriteLiteral(" />\r\n </div>\r\n");
#line 178 "..\..\Areas\Config\Views\SystemConfig\Index.cshtml" #line 179 "..\..\Areas\Config\Views\SystemConfig\Index.cshtml"
} }