Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add arbitrary event logging functionality using the MDT Event Log as the source. #7

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,5 @@ ModelManifest.xml
# FAKE - F# Make
.fake/
/Code.txt
/AdminUI.WqlQueryEngine.dll
/Microsoft.ConfigurationManagement.ManagementProvider.dll
89 changes: 88 additions & 1 deletion ConfigMgrWebService/ConfigMgrWebService.asmx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -2825,6 +2825,93 @@ public bool AddMDTRoleMember(string secret, string computerName, string role, st
return returnValue;
}


/// <summary>
/// Adds an entry to the MDT Event Log (if present)
/// </summary>
[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)
{
Expand Down
18 changes: 10 additions & 8 deletions ConfigMgrWebService/ConfigMgrWebService.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.0\build\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props" Condition="Exists('..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.0\build\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props')" />
<Import Project="..\packages\Microsoft.Net.Compilers.1.0.0\build\Microsoft.Net.Compilers.props" Condition="Exists('..\packages\Microsoft.Net.Compilers.1.0.0\build\Microsoft.Net.Compilers.props')" />
<Import Project="..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.8\build\net45\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props" Condition="Exists('..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.8\build\net45\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props')" />
<Import Project="..\packages\Microsoft.Net.Compilers.1.3.2\build\Microsoft.Net.Compilers.props" Condition="Exists('..\packages\Microsoft.Net.Compilers.1.3.2\build\Microsoft.Net.Compilers.props')" />
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
Expand Down Expand Up @@ -45,14 +45,16 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="AdminUI.WqlQueryEngine">
<HintPath>..\..\..\..\..\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\AdminUI.WqlQueryEngine.dll</HintPath>
<HintPath>..\AdminUI.WqlQueryEngine.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.0\lib\net45\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.dll</HintPath>
<Reference Include="Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.8.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.8\lib\net45\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.ConfigurationManagement.ManagementProvider">
<HintPath>..\..\..\..\..\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\Microsoft.ConfigurationManagement.ManagementProvider.dll</HintPath>
<HintPath>..\Microsoft.ConfigurationManagement.ManagementProvider.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.DirectoryServices" />
Expand Down Expand Up @@ -138,8 +140,8 @@
<PropertyGroup>
<ErrorText>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}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\Microsoft.Net.Compilers.1.0.0\build\Microsoft.Net.Compilers.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Net.Compilers.1.0.0\build\Microsoft.Net.Compilers.props'))" />
<Error Condition="!Exists('..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.0\build\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.0\build\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props'))" />
<Error Condition="!Exists('..\packages\Microsoft.Net.Compilers.1.3.2\build\Microsoft.Net.Compilers.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Net.Compilers.1.3.2\build\Microsoft.Net.Compilers.props'))" />
<Error Condition="!Exists('..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.8\build\net45\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.8\build\net45\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props'))" />
</Target>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
4 changes: 2 additions & 2 deletions ConfigMgrWebService/Web.config
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs"
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.8.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701"/>
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb"
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.8.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"/>
</compilers>
</system.codedom>
Expand Down
4 changes: 2 additions & 2 deletions ConfigMgrWebService/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="1.0.0" targetFramework="net452" />
<package id="Microsoft.Net.Compilers" version="1.0.0" targetFramework="net452" developmentDependency="true" />
<package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="1.0.8" targetFramework="net452" />
<package id="Microsoft.Net.Compilers" version="1.3.2" targetFramework="net452" developmentDependency="true" />
</packages>
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ The list below shows an overview of the available methods:

### Microsoft Deployment Toolkit

- AddMDTEventLogEntry
- AddMDTRoleMember
- AddMDTRoleMemberByAssetTag
- AddMDTRoleMemberByMacAddress
Expand Down