Updating Resources.mpx at build
DLLs and Other Resources
If you're developing a management pack with custom UI and/or .NET modules you've gone through the steps of getting your DLL recognized by the engine and loaded as an Assembly or DeployedAssembly depending on the project (here's an SCSM example).
During an ongoing project, constantly updating the resource mpx file to have the correct QualifiedName can be frustrating, time-consuming, and error-prone.
The goal of this quick post
Automate the updating of the QualifiedName field when I build one of my dependent DLL libraries. To get this done I'm going to use the post-build events options.
Using Visual Studio post-build events
Visual Studio has the ability to run batch scripts before and after the build process, for our example below we are going to use the Post-Build event to run a PowerShell script and perform a pair of quick steps.
- Copy the newly built DLL to the MP project folder
- Update the Resources.mpx file with the correct qualified name
Creating the PowerShell script
The script used in this example is short and simple. The snippet below takes these five steps to update my Resources.mpx
- Extract the Qualified Name of the recently built DLL
- Build the path to the MPX file that defines my resources
- Create a new XML document and load up the MPX file
- Using the
where-object
command it locates the resource file entry, then replaces with the Qualified Name extracted earlier. - Overwrite the XML with the newly updated version.
# Step one: Get the new qualified name
$qualifiedName = [System.Reflection.AssemblyName]::GetAssemblyName($executableLocation).FullName
# Step two: Build the path to the MPX resource file
$resourceXMLPath = $SolutionPath + 'servicenow-connector-mp\Cookdown.SNOW.Connector\Resources.mpx';
# Step three: Create a new XML Document and load the MPX
$xmlResources = [xml]::new()
$xmlResources.Load($resourceXMLPath)
# Step four: Replace the qualified name in the resource file with the new one
($xmlResources.ManagementPackFragment.Resources.DeployableAssembly | ? {$_.Id -eq $ResourceFileName}).QualifiedName = $qualifiedName
# Step five: Overwrite the existing file
$xmlResources.Save($resourceXMLPath)
Configuring the PowerShell script
Included in the Build Events UI is the ability to use parameters from the build when calling the script. The below example uses the build path variables as these management packs are built on Azure DevOps servers. An additional variable has the project name, as a couple of projects use this same script.
That should be it!
Next time you build your required DLL you should see you MPX file automatically reflect the changes. If anything was missed or isn't clear please contact me and I'll get things updated.
Additional Resources:
PowerPoint slides from a related presentation - https://slideplayer.com/slide/9093132/
A custom SCSM task using a .NET DLL - https://blog.jhnr.ch/2013/12/09/how-to-create-a-custom-scsm-console-task-by-using-some-c-and-xml-magic/