From dff549ab6f1ad106e49d1ec269a64d6806cec343 Mon Sep 17 00:00:00 2001 From: anykeypress Date: Sun, 11 Mar 2018 17:19:32 +0100 Subject: [PATCH 1/2] Edited the warning regarding unused ex variable. Added the AddMDTEventLogEntry method to write arbitrary events to the MDT event log (if present) --- .gitignore | 2 + .../ConfigMgrWebService.asmx.cs | 89 ++++++++++++++++++- .../ConfigMgrWebService.csproj | 18 ++-- ConfigMgrWebService/Web.config | 4 +- ConfigMgrWebService/packages.config | 4 +- 5 files changed, 104 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index f9f797a..4467ec0 100644 --- a/.gitignore +++ b/.gitignore @@ -244,3 +244,5 @@ ModelManifest.xml # FAKE - F# Make .fake/ /Code.txt +/AdminUI.WqlQueryEngine.dll +/Microsoft.ConfigurationManagement.ManagementProvider.dll diff --git a/ConfigMgrWebService/ConfigMgrWebService.asmx.cs b/ConfigMgrWebService/ConfigMgrWebService.asmx.cs index b2e352e..f3b18ac 100644 --- a/ConfigMgrWebService/ConfigMgrWebService.asmx.cs +++ b/ConfigMgrWebService/ConfigMgrWebService.asmx.cs @@ -2290,7 +2290,7 @@ public bool RemoveADComputer(string secret, string samAccountName) } catch (Exception ex) { - WriteEventLog("Unable to detect domain controllers in current domain.", EventLogEntryType.Error); + WriteEventLog(String.Format("Unable to detect domain controllers in current domain.", ex.Message), EventLogEntryType.Error); returnValue = false; } @@ -2825,6 +2825,93 @@ public bool AddMDTRoleMember(string secret, string computerName, string role, st return returnValue; } + + /// + /// Adds an entry to the MDT Event Log (if present) + /// + [WebMethod(Description = "Adds an arbitrary event in the MDT event log")] + public bool AddMDTEventLogEntry(string secret, UInt16 eventID, UInt16 eventSeverity, string eventMessage) { + + // Always assume the worst + bool returnValue = false; + + MethodBase method = MethodBase.GetCurrentMethod(); + MethodBegin(method); + + // Validate secret key + if(secret == secretKey) { + + // Check if event log exists, if not error to event log + try { + // An exception can be thrown if not enough permissions exist to query the event log sources... + // So to be on the safe side... + if (!EventLog.SourceExists("MDT_Monitor") ) { + WriteEventLog("The MDT Monitoring does not appear to be active. Please ensure it is activated before attempting to use this function again.", EventLogEntryType.Error); + } + else { + + // MDT_Monitor source exists.. lets do it + try { + using(EventLog mdtEventLog = new EventLog()) { + + // Define a type and set a default value + EventLogEntryType type = EventLogEntryType.Information; + + switch(eventSeverity) { + // Map 1 to information + case 1: + type = EventLogEntryType.Information; + break; + // Map 2 to warning + case 2: + type = EventLogEntryType.Warning; + break; + // Map 3 to error + case 3: + type = EventLogEntryType.Error; + break; + // Any other values just ignore (i.e. keep mapped to information) + default: + break; + + } + + // I could possibly add code to filter + // out the hard-coded event ID's from the built in MDT event logging + // but cannot be arsed + + + // Do it + mdtEventLog.Source = "MDT_Monitor"; + mdtEventLog.Log = "Application"; + mdtEventLog.WriteEntry(eventMessage, type, eventID); + + // If we get here, then everything is ok + returnValue = true; + + } + } + catch(Exception ex) { + // Write the exception information into the CfgMgr event log + WriteEventLog(String.Format("Error attemtping to write to the MDT Event Log. Error Message: {0}", ex.Message), EventLogEntryType.Error); + } + } + } + catch (Exception ex){ + // Write the exception information into the CfgMgr event log + WriteEventLog(String.Format("Error attempting to query the EventLog sources or the MDT_Monitor source is not present. Error Message: {0}",ex.Message) , EventLogEntryType.Error); + } + } + + + MethodEnd(method); + + // Return what we have + return returnValue; + } + + + [WebMethod(Description = "Remove MDT computer from all associated roles")] public bool RemoveMDTComputerFromRoles(string secret, string identity) { diff --git a/ConfigMgrWebService/ConfigMgrWebService.csproj b/ConfigMgrWebService/ConfigMgrWebService.csproj index caa1d01..2c8b70f 100644 --- a/ConfigMgrWebService/ConfigMgrWebService.csproj +++ b/ConfigMgrWebService/ConfigMgrWebService.csproj @@ -1,7 +1,7 @@  - - + + Debug @@ -45,14 +45,16 @@ - ..\..\..\..\..\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\AdminUI.WqlQueryEngine.dll + ..\AdminUI.WqlQueryEngine.dll + True - - ..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.0\lib\net45\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.dll + + ..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.8\lib\net45\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.dll True - ..\..\..\..\..\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\Microsoft.ConfigurationManagement.ManagementProvider.dll + ..\Microsoft.ConfigurationManagement.ManagementProvider.dll + True @@ -138,8 +140,8 @@ This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. - - + +