SCOM 2012 – Create an MPB to Deploy Files Using VSAE

One of the improvements made in Operations Manager when System Center 2012 was released was the MP schema upgrade from v1 to v2, which is the same version used by Service Manager.  So now, as with Service Manager, it is possible to import MPB files, as well as MP and XML files.  What’s the advantage to this I hear you ask????  Well, MPB stands for Management Pack Bundle which means an MPB can contain many MP files, but further to this it can contain any type of file which can then be deployed.  An example of this was the updated x-plat management packs for Operations Manager which are now MPB’s as the x-plat agents are now contained in them and then deployed to the required location automatically.

This is a great feature, and I think it could be utilised a lot more, but unfortunately there isn’t much guidance out there on how to do this.  I’m hoping my guide below will rectify this issue and encourage more MPB authoring 🙂

Before starting, you will need Visual Studio 2010 Professional, Premium, or Ultimate, and the Visual Studio Authoring Extensions (VSAE) installed.  You can download the VSAE here, http://www.microsoft.com/en-us/download/details.aspx?id=30169.

Now we’re good to go…..

 

First, a new management pack needs to be created.

1.  Open Visual Studio.

2.  Select “New Project…” on the Start Page.

NewProject1

 

3.  Select the Operations Manager templates in the left column, and then select the Operations Manager 2012 Add-on Management Pack template from the central column.  In the Name and Location fields at the bottom, enter a relevant name and a location where the project is to be stored.

NewProject2

 

4.  Once the project is created, it will show in the Solution Explorer and the MPB is ready to start having some content added to it.

NewProject3

 

Now the management pack is created, the files we want to package can be added as well as the MPX fragments required to add the files as resources and control the deployment.  In the following steps I will be adding folders to make things clearer, it is up to you whether you want to add folders or not when building your MP.

5.  Right click your project in the Solution Explorer, and select “Add –> New Folder”.  After the new folder has been added, rename it to “Resources”.

NewFolder1

 

 

 

 

6.  Right click the “Resources” folder and select “Add –> New Item”.  In the Add New Item Windows, select the Code templates from the left menu, and then select “Empty Management Pack Fragment” from the central column.  Provide a name such as “Resources.mpx”, and click Add.

NewItem1

 

7.  Add the following XML code to the newly created “Resources.mpx” file.

<Resources>

  <DeployableResource ID=”MPBExample.Resource.DeployableFileZip” Accessibility=”Public” Filename=”DeployableFile.zip” />

</Resource>

 

The ID parameter can be updated as required based on your MP name and how you like to format your ID’s.  The Filename parameter must match the name of the file that requires packaging exactly.

ResourcesMPX1

 

8.  Right click the “Resources” folder again, and select “Add –> Existing Item”.

ResourceAdd1

 

9.  Select the file that needs to be packaged, as defined previously, and click Add.  In this case, it is DeployableFile.zip.

ResourceAdd2

 

10.  Select the newly added file, and in the Properties window change the Build Action value to Embedded Resource.

ResourceAdd3

 

Now we have the file included in our project, and it has been referenced as a resource in the “Resources.mpx” file, the components are required to deploy the file.  The next steps add a PowerShell script to the project which will deploy the files.

11.  As in step 5, right click your project in the Solution Explorer, and select “Add –> New Folder”.  After the new folder has been added, rename it to “Scripts”.

12.  As in step 6, right click the “Resources” folder and select “Add –> New Item”.  In the Add New Item Windows, select the Resources templates from the left menu, and then select “PowerShell script file” from the central column.  Provide a name such as “DeployableFile.ps1”, and click Add.

13.  Add the following PowerShell code to the newly created “DeployableFile.ps1” file.

param ([string]$FilePaths)

  $api = New-Object -comObject ‘MOM.ScriptAPI’
$api.LogScriptEvent(“DeployableFile.ps1”, 5678, 0, “Script started”)


$TargetDirectory = “C:\DeployedFiles”

    If (!(Test-Path -Path “$TargetDirectory”)) { New-Item -ItemType:Directory -Path:”$TargetDirectory” }

    [array]$ArFiles = $FilePaths.Split(“,”)
ForEach ($FilePath in $ArFiles) {
$ArFilePath = $FilePath.Split(“\”)
$FileName = $arFilePath[$arFilePath.Length -1]

      $api.LogScriptEvent(“DeployableFile.ps1”, 5678, 0, “File Name: $FileName”)

      If ((Test-Path -Path “$TargetDirectory”) -and (Test-Path -Path “$File”)){
$TargetFile = $TargetDirectory + “\” + $FileName
If (!(Test-Path -Path $TargetFile)){
Try{
Copy-Item “$File” “$TargetDirectory”
$api.LogScriptEvent(“DeployableFile.ps1”, 5678, 0, “Copied agent file: $File to the directory: $TargetDirectory”)
}
Catch{
$api.LogScriptEvent(“DeployableFile.ps1”, 5678, 0, “Failed to copy agent file: $File to the directory: $TargetDirectory”)
}
}
}Else{
Call $api.LogScriptEvent(“DeployableFile.ps1”, 5678, 4, “Failed to copy agent file: $File to the directory: $TargetDirectory.  The source or target does not exist.”)
}
}

$api.LogScriptEvent(“DeployableFile.ps1”, 5678, 0, “Script completed”)

 

The target directory for where the file will be deployed is hard coded in the script, in this example.  The target directory can be updated as required.

 

With the PowerShell script added, the steps below will create a new WriteAction module that utilises this script.

14.  As in step 5, right click your project in the Solution Explorer, and select “Add –> New Folder”.  After the new folder has been added, rename it to “Write Actions”.

15.  As in step 6, right click the “Resources” folder and select “Add –> New Item”.  In the Add New Item Windows, select the Code templates from the left menu, and then select “Empty Management Pack Fragment” from the central column.  Provide a name such as “DeployFile.mpx”, and click Add.

16.  Add the following XML code to the newly created “DeployFile.mpx” file.

<ManagementPackFragment SchemaVersion=”2.0″ xmlns:xsd=”http://www.w3.org/2001/XMLSchema”>
<TypeDefinitions>
<ModuleTypes>
<WriteActionModuleType ID=”MPBExample.WriteAction.DeployableFile” Accessibility=”Public” Batching=”false”>
<Configuration>
<xsd:element name=”FilePaths” type=”xsd:string” />
</Configuration>
<ModuleImplementation Isolation=”Any”>
<Composite>
<MemberModules>
<WriteAction ID=”WA” TypeID=”Windows!Microsoft.Windows.PowerShellWriteAction”>
<ScriptName>DeployableFile.ps1</ScriptName>
<ScriptBody>$IncludeFileContent/Scripts/DeployableFile.ps1$</ScriptBody>
<Parameters>
<Parameter>
<Name>FilePaths</Name>
<Value>$Config/FilePaths$</Value>
</Parameter>
</Parameters>
<TimeoutSeconds>300</TimeoutSeconds>
</WriteAction>
</MemberModules>
<Composition>
<Node ID=”WA” />
</Composition>
</Composite>
</ModuleImplementation>
<OutputType>System!System.BaseData</OutputType>
<InputType>System!System.BaseData</InputType>
</WriteActionModuleType>
</ModuleTypes>
</TypeDefinitions>
<LanguagePacks>
<LanguagePack ID=”ENU” IsDefault=”true”>
<DisplayStrings>
<DisplayString ElementID=”MPBExample.WriteAction.DeployableFile”>
<Name>Deploy File Write Action</Name>
<Description>Write action for deploying a file.</Description>
</DisplayString>
</DisplayStrings>
</LanguagePack>
</LanguagePacks>
</ManagementPackFragment>

 

WriteAction1

 

With the new WriteAction created, the steps below create a new rule that uses the WriteAction module.

17.  As in step 5, right click your project in the Solution Explorer, and select “Add –> New Folder”.  After the new folder has been added, rename it to “Rules”.

18.  As in step 6, right click the “Rules” folder and select “Add –> New Item”.  In the Add New Item Windows, select the Templates templates from the left menu, and then select “Rule (Custom)” from the central column.  Provide a name such as “FileDeployment.mptg”, and click Add.

This creates a management pack template group for custom rules, which provides a much easier and quicker way of creating rules without having to create all the XML.

Rules1

 

19.  Double-click the new “FIleDeployment.mptg” file and select the the one New Rule row already in place.

Rules2

Note:  All edits to this rule will be performed in the Properties window as highlighted above.

 

20.  Within the Properties window, set the following configuration:

  • Category – Operations
  • Description – File deployment rule.
  • Display Name – File Deployment
  • ID – Rule.FileDeployment
  • Target – SC!Microsoft.SystemCenter.ManagementServer

Note:  The properties above are for this example where file deployment is required on the management servers.

All other properties under the Advanced and General headings, leave to their default values.

Rules3

 

21.  Under the Modules heading in the Properties windows, select the ellipsis next to Data Sources.

Rules4

 

22.  Click the Add button to add a new Data Source, and then set the following values:

  • Data Source Type – System!System.Scheduler
  • Data Source Configuration (In between configuration tags) –
    <Scheduler>
    <SimpleReccuringSchedule>
    <Interval Unit=”Seconds”>28800</Interval>
    <SyncTime></SyncTime>
    </SimpleReccuringSchedule>
    <ExcludeDates></ExcludeDates>
    </Scheduler>
    Click OK to close the Data Sources window.

Rules5

 

23.  Under the Modules heading in the Properties windows, select the ellipsis next to Write Actions.

24.  Click the Add button to add a new Write Action, and then set the following values:

  • Write Action Type ID – MPBExample.WriteAction.DeployableFile
  • Write Action Configuration (In between Configuration tags) –
    <FilePaths>$FileResource[Name=’MPBExample.Resource.DeployableFileZip’]/Path$</FilePaths>

 

Click OK to close the Write Actions window.

 

 

 

 

 

Save the project and it is done!  We now have a rule that is scheduled to run every 28800 seconds, which calls a write action to run a PowerShell script that will deploy the embedded resource files, if they haven’t already been deployed.

All that is left to do now, is to provide a certificate for sealing the management pack and to build and import.

25.  Right-click the project in the Solution Explorer window, and select properties.  Select the Build tab, check the box to generate a signed and sealed management pack, and then provide the relevant information and path to a seal key.  Save the project.

Seal1

 

26.  Hit the F5 key to build the project!

 

Now, all you need to do is import the MPB file from the build output path into your management group, and wait for the files to be deployed.

You can also download a ZIP file that contains my example solution file, and the built MPB file ready to be imported, here http://sdrv.ms/17zGPjX.

 

Enjoy!

3 thoughts on “SCOM 2012 – Create an MPB to Deploy Files Using VSAE

  1. Profile photo of AlexAlex

    Hi,

    I’m trying to deploy a file like you described in this article. Unfortunately nothing is happening. Neither with my own solution nor with your mpb file nor simulating it in visual studio.
    The MPB imports fine but then nothing happens. seems the script gets not executed. Any advice?

    Thanks Alex

  2. Profile photo of AlexAlex

    Hi,

    me again. Had to restart my server and in above script are errors. $File should be $FilePath. It is correct in your solution.

    Regards Alex

     

  3. Pingback: SCOM: Sample Maintenance Mode MP works on SCOM 2016 | SystemCenterTipps

Leave a Reply