Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 241
  • Last Modified:

Need to have IF statement also only go if the time is after or before 2PM

What I need to do is also have the IF statement work on a time as well. For example if it is after 2 or before 2pm. This way there is no dupicated message being sent out

using System;
using System.Management;
using System.Management.Instrumentation;
using System.Xml;
using System.IO;
using System.Collections;
using Microsoft.Win32;
using QTI.Monitor.Configuration;

namespace Q.M.T
{
      /// <summary>
      /// Tell the central server the date of when the Customer  
      /// last rebooted the PC.  
      /// </summary>
      public class TaskPCLastReboot : TaskInterface
      {
            /// <SUMMARY> </SUMMARY>
            public void DoTask(XmlNode xmlNode, ref int iSeverityLevel,
                  ref String[] sValues, ref String[] sDetails)

            {
                  //system date need to see if last reboot was today or yesterday after
                  //client last ran checks.
                  System.DateTime today = System.DateTime.Today;
                  System.DateTime daybefore = today.AddDays(-1);
      
                  try
                  {
                        //This is the actual search to get the restart date from the
                        //operating system
                        string ConfigNamespace = @"\\.\root\cimv2";
                        string query = "select * from Win32_OperatingSystem";

                        ManagementObjectSearcher searcher = new
                              ManagementObjectSearcher( ConfigNamespace, query );
                              
                        ManagementObjectCollection collection = searcher.Get();

                        DateTime bootup;      
                        foreach (ManagementObject item in collection)
                        {
                             string DMTF= item.Properties["LastBootUpTime"].Value.ToString();
                              bootup = ManagementDateTimeConverter.ToDateTime(DMTF).Date;

                              //Compare dates to see if it was today before the 8am Client runs
                              //or if it was yesterday after the Client ran
                              if (bootup == daybefore) //and time after 2pm
                              {
                                    post message if time after 2pm

                              }
                              else if (bootup == today)//and time before 2pm
                              {
                                    post message if time before 2pm
                              }
                  
                        }
                        searcher.Dispose();
                  }
                  catch (Exception e)
                  {
                      System.Diagnostics.EventLog.WriteEntry(this.ToString(),
                        ERROR.FAILED + e.ToString(),
                              System.Diagnostics.EventLogEntryType.Error);
                  }
            }
            
            public  string GetMsgId() {return m_sMsgId;}
            public  int GetMsgNum() {return m_iMsgNum;}
            private static string m_sMsgId = MESSAGETYPES.LAST_PC_RESTART_MONITOR;
            private static int m_iMsgNum = MESSAGENUMBERS.LAST_PC_RESTART_MONITOR;
      }
}
0
NewMom2Brandon
Asked:
NewMom2Brandon
  • 2
1 Solution
 
rcarlanCommented:
                         bootup = ManagementDateTimeConverter.ToDateTime(DMTF);

                         //Compare dates to see if it was today before the 8am Client runs
                         //or if it was yesterday after the Client ran
                         if (bootup.Date==daybefore && bootup.hour>=14) // bootup yesterday after 2pm
                         {
                              post message if time after 2pm

                         }
                         else if (bootup.Date==today && bootup.hour<14) // bootup today before 2pm
                         {
                              post message if time before 2pm
                         }
0
 
NewMom2BrandonAuthor Commented:
I did get this error

'System.DateTime' does not contain a definition for 'hour'
0
 
NewMom2BrandonAuthor Commented:
got it fixed...sorry!!
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now