Importing Management Packs Programmatically (and How to Connect to a Management Server in an Untrusted Domain)

You can import MPs using the PowerShell cmdlet Import-SCSMManagementPack.  That works for .xml, .mp, or .mpb files.  But what if you wanted to do it programmatically using the SDK?  No problem.  You just need to know how to do it.

Here is some sample code to show you how itโ€™s done.  As a bonus you can see in this code sample how to Connect As some particular account so that you can programmatically connect to SCSM as a different account.  This is good for untrusted domain configurations where you need to connect using an IP address.  Note how you need to import management pack bundles (.mpb) differently than you do a .xml or a .mp file.

                EnterpriseManagementConnectionSettings cs = new EnterpriseManagementConnectionSettings("192.168.1.100");
                cs.Domain = "fabrikam";
                cs.UserName = "administrator";
                char[] charPassword = { 'S', 'M', 'X','#','2','0','0','1' };
                System.Security.SecureString ssPassword = new System.Security.SecureString();
                foreach (char c in charPassword)
                    ssPassword.AppendChar(c);
                cs.Password = ssPassword;

                EnterpriseManagementGroup emg = new EnterpriseManagementGroup(cs);

                //Import XML or .mp:
                ManagementPack managementPack = new ManagementPack("C:\\Temp\\Microsoft.SystemCenter.DataWarehouse.Library.mp");
                emg.ManagementPacks.ImportManagementPack(managementPack);

                //Import MPB:
                ManagementPackBundleReader reader = ManagementPackBundleFactory.CreateBundleReader();
                ManagementPackBundle bundle = reader.Read("MyMP.mpb",emg);
                emg.ManagementPacks.ImportBundle(bundle);