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);
this.Status.UpdateStatus(95, "Updating Database", "Writing last network logon dates to the Database");
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[]
@@ -50,44 +50,16 @@ namespace Disco.BI.Interop.ActiveDirectory
});
}
//public void InitalizeScheduledTask(DiscoDataContext dbContext, IScheduler Scheduler)
//{
// // UpdateLastNetworkLogonDates @ 11:30pm
// IJobDetail jobDetail = new JobDetailImpl("UpdateLastNetworkLogonDates", typeof(ActiveDirectoryUpdateLastNetworkLogonDateJob));
// ITrigger trigger = TriggerBuilder.Create().
// WithIdentity("UpdateLastNetworkLogonDatesTrigger").
// StartNow().
// WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(23, 30)).
// Build();
// Scheduler.ScheduleJob(jobDetail, trigger);
//}
public static ScheduledTaskStatus ScheduleImmediately()
{
var existingTask = ScheduledTasks.GetTaskStatuses(typeof(ActiveDirectoryUpdateLastNetworkLogonDateJob)).Where(s => s.IsRunning).FirstOrDefault();
if (existingTask != null)
return existingTask;
var instance = new ActiveDirectoryUpdateLastNetworkLogonDateJob();
return instance.ScheduleTask();
}
//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)
{
System.DateTime? computerLastLogonDate = Device.LastNetworkLogonDate;
@@ -178,7 +150,7 @@ namespace Disco.BI.Interop.ActiveDirectory
UpdateLastNetworkLogonDate = false;
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>();
@@ -202,10 +174,7 @@ namespace Disco.BI.Interop.ActiveDirectory
using (DirectoryEntry dRootEntry = ActiveDirectoryHelpers.DefaultDCLdapRoot(dcName))
{
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))
{
@@ -222,9 +191,8 @@ namespace Disco.BI.Interop.ActiveDirectory
{
string computerName = ((string)dProp[0]).TrimEnd(new char[] { '$' }).ToUpper();
if (status != null)
if (progressItemCount % 150 == 0) // Only Update Status every 150 devices
status.UpdateStatus(progressDCStart + (progressItemProgress * progressItemCount), string.Format("Analysing Device: {0}", computerName));
if (progressItemCount % 150 == 0) // Only Update Status every 150 devices
status.UpdateStatus(progressDCStart + (progressItemProgress * progressItemCount), string.Format("Analysing Device: {0}", computerName));
dProp = dResult.Properties["lastLogon"];
if (dProp != null && dProp.Count > 0)
@@ -19,19 +19,9 @@ namespace Disco.Web.Areas.API.Controllers
public virtual ActionResult UpdateLastNetworkLogonDates()
{
//ActiveDirectoryUpdateLastNetworkLogonDateJob updateJob = new ActiveDirectoryUpdateLastNetworkLogonDateJob();
var taskStatus = ActiveDirectoryUpdateLastNetworkLogonDateJob.ScheduleImmediately();
ActiveDirectoryUpdateLastNetworkLogonDateJob.UpdateLastNetworkLogonDates(dbContext);
var resultCount = dbContext.SaveChanges();
var model = new Models.System.UpdateLastNetworkLogonDatesModel()
{
Result = "OK",
UpdateCount = resultCount
};
return Json(model, JsonRequestBehavior.AllowGet);
return RedirectToAction(MVC.Config.Logging.TaskStatus(taskStatus.SessionId));
}
public virtual ActionResult UpdateAttachmentThumbnails()
@@ -173,6 +173,7 @@
</table>
</div>
<div class="actionBar">
@Html.ActionLinkButton("Update Device Last Network Logons", MVC.API.System.UpdateLastNetworkLogonDates())
<input type="submit" class="button" value="Save Configuration" />
</div>
}
@@ -603,7 +603,18 @@ WriteLiteral(" <div");
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\"");
@@ -614,7 +625,7 @@ WriteLiteral(" value=\"Save Configuration\"");
WriteLiteral(" />\r\n </div>\r\n");
#line 178 "..\..\Areas\Config\Views\SystemConfig\Index.cshtml"
#line 179 "..\..\Areas\Config\Views\SystemConfig\Index.cshtml"
}