private void CreateConnectorInstance() { try { //Get the server name to connect to String strServerName = Registry.GetValue("HKEY_CURRENT_USER\\Software\\Microsoft\\System Center\\2010\\Service Manager\\Console\\User Settings", "SDKServiceMachine", "localhost").ToString(); //Conneect to the server EnterpriseManagementGroup emg = new EnterpriseManagementGroup(strServerName); //Get the System MP so we can get the system key token and version so we can get other MPs using that info ManagementPack mpSystem = emg.ManagementPacks.GetManagementPack(SystemManagementPack.System); string strSystemKeyToken = mpSystem.KeyToken; ManagementPack mpSubscriptions = emg.GetManagementPack("Microsoft.SystemCenter.Subscriptions", strSystemKeyToken, new Version("1.0.0.0")); //Also get the System Center and Connector MPs - we'll need things from those MPs later ManagementPack mpSystemCenter = emg.ManagementPacks.GetManagementPack(SystemManagementPack.SystemCenter); ManagementPack mpConnectors = emg.GetManagementPack("SCSM.AzureAutomation", "ac1fe0583b6c84af", new Version("1.0.0.0")); ManagementPack mpAAConnectorWorkflows = emg.GetManagementPack("SCSM.AzureAutomation.Workflows", null, new Version("1.0.0.0")); //Get the AzureAutomationConnector class in the Connectors MP ManagementPackClass classAAConnector = mpConnectors.GetClass("SCSM.AzureAutomation.Connector"); //Create a new CreatableEnterpriseManagementObject. We'll set the properties on this and then post it to the database. EnterpriseManagementObject cemoAAConnector = new CreatableEnterpriseManagementObject(emg, classAAConnector); //Sytem.Entity properties cemoAAConnector[classAAConnector, "DisplayName"].Value = this.DisplayName; //Required //Microsoft.SystemCenter.Connector properties //This is just a tricky way to create a unique ID which conforms to the syntax rules for MP element ID attribute values. String strConnectorID = String.Format(CultureInfo.InvariantCulture, "{0}.{1}", "AAConnector", Guid.NewGuid().ToString("N")); cemoAAConnector[classAAConnector, "Id"].Value = strConnectorID; //Required; Key //System.LinkingFramework.DataSource properties cemoAAConnector[classAAConnector, "DataProviderDisplayName"].Value = "Azure Automation Connector"; //Optional, shown in Connectors view cemoAAConnector[classAAConnector, "Enabled"].Value = true; //Optional, shown in Connectors view cemoAAConnector[classAAConnector, "Name"].Value = this.DisplayName; //SCSM.AzureAutomation.Connector properties cemoAAConnector[classAAConnector, "AutomationAccount"].Value = this.AutomationAccount; cemoAAConnector[classAAConnector, "SubscriptionID"].Value = this.SubscriptionID; cemoAAConnector[classAAConnector, "ResourceGroup"].Value = this.ResourceGroup; cemoAAConnector[classAAConnector, "RunAsAccountName"].Value = this.RunAsAccountName; cemoAAConnector[classAAConnector, "RunAsAccountPassword"].Value = this.RunAsAccountPassword; cemoAAConnector[classAAConnector, "RefreshIntervalHours"].Value = this.RefreshIntervalHours; //Create Connector instance cemoAAConnector.Commit(); //Now we need to create the CSV Connector rule... //Get the Scheduler data source module type from the System MP and the Windows Workflow Task Write Action Module Type from the Subscription MP ManagementPackDataSourceModuleType dsmtScheduler = (ManagementPackDataSourceModuleType)mpSystem.GetModuleType("System.Scheduler"); ManagementPackWriteActionModuleType wamtWindowsWorkflowTaskWriteAction = (ManagementPackWriteActionModuleType)mpSubscriptions.GetModuleType("Microsoft.EnterpriseManagement.SystemCenter.Subscription.WindowsWorkflowTaskWriteAction"); //Create a new rule for the CSV Connector in the Connectors MP. Set the name of this rule to be the same as the connector instance ID so there is a pairing between them ManagementPackRule ruleAAConnector = new ManagementPackRule(mpAAConnectorWorkflows, strConnectorID); //Set the target and other properties of the rule ruleAAConnector.Target = mpSystemCenter.GetClass("Microsoft.SystemCenter.SubscriptionWorkflowTarget"); //Create a new Data Source Module in the new CSV Connector rule ManagementPackDataSourceModule dsmSchedule = new ManagementPackDataSourceModule(ruleAAConnector, "DS1"); //Set the configuration of the data source rule. Pass in the frequency (number of minutes) dsmSchedule.Configuration = "<Scheduler>" + "<SimpleReccuringSchedule>" + "<Interval Unit=\"Hours\">" + strRefreshIntervalHours + "</Interval>" + "</SimpleReccuringSchedule>" + "<ExcludeDates />" + "</Scheduler>"; //Set the Schedule Data Source Module Type to the Simple Schedule Module Type from the System MP dsmSchedule.TypeID = dsmtScheduler; //Add the Scheduler Data Source to the Rule ruleAAConnector.DataSourceCollection.Add(dsmSchedule); //Now repeat essentially the same process for the Write Action module... //Create a new Write Action Module in the CSV Connector rule ManagementPackWriteActionModule wamAAConnector = new ManagementPackWriteActionModule(ruleAAConnector, "WA1"); //Set the Configuration XML wamAAConnector.Configuration = "<Subscription>" + "<WindowsWorkflowConfiguration>" + //Specify the Windows Workflow Foundation workflow Assembly name here "<AssemblyName>SCSM.AzureAutomation.Workflows.AT</AssemblyName>" + //Specify the type name of the workflow to call in the assembly here: "<WorkflowTypeName>WorkflowAuthoring.RefreshConnector</WorkflowTypeName>" + "<WorkflowParameters>" + //Pass in the parameters here. In this case the two parameters are the data file path and the mapping file path "<WorkflowParameter Name=\"RefreshConnectorScript_ConnectorId\" Type=\"string\">" + strConnectorID + "</WorkflowParameter>" + "</WorkflowParameters>" + "<RetryExceptions />" + "<RetryDelaySeconds>60</RetryDelaySeconds>" + "<MaximumRunningTimeSeconds>300</MaximumRunningTimeSeconds>" + "</WindowsWorkflowConfiguration>" + "</Subscription>"; //Set the module type of the module to be the Windows Workflow Task Write Action Module Type from the Subscriptions MP. wamAAConnector.TypeID = wamtWindowsWorkflowTaskWriteAction; //Add the Write Action Module to the rule ruleAAConnector.WriteActionCollection.Add(wamAAConnector); //Mark the rule as pending update ruleAAConnector.Status = ManagementPackElementStatus.PendingAdd;; //Accept the rule changes which updates the database mpAAConnectorWorkflows.AcceptChanges(); } catch (Exception e) { MessageBox.Show(e.Message + e.InnerException.Message); } }
public static void CreateNotificationRule(string strDisplayName, string strName, string strCriteria, EnterpriseManagementObject emoUser, ManagementPackClass mpc, ManagementPackObjectTemplate mpt, ref ManagementPack mp, EnterpriseManagementGroup emg) { ManagementPack mpSystemCenterLibrary = Helper.GetManagementPackByName("Microsoft.SystemCenter.Library", emg); ManagementPack mpSubscriptions = Helper.GetManagementPackByName("Microsoft.SystemCenter.Subscriptions", emg); string strSubscriptionsMPAlias = MakeMPElementSafeName(mpSubscriptions.Name); AddManagementPackReference(strSubscriptionsMPAlias, mpSubscriptions.Name, ref mp, emg); string strSystemCenterLibraryMPAlias = MakeMPElementSafeName(mpSystemCenterLibrary.Name); AddManagementPackReference(strSystemCenterLibraryMPAlias, mpSystemCenterLibrary.Name, ref mp, emg); ManagementPackClass mpcSubscriptionWorkflowTarget = GetClassByName("Microsoft.SystemCenter.SubscriptionWorkflowTarget", emg); ManagementPackDataSourceModuleType mpdsmtSubscription = (ManagementPackDataSourceModuleType)mpSubscriptions.GetModuleType("Microsoft.SystemCenter.CmdbInstanceSubscription.DataSourceModule"); ManagementPackRule mpr = new ManagementPackRule(mp, MakeMPElementSafeName(String.Format("NotificationSubscription.{0}", Guid.NewGuid()))); mpr.Enabled = ManagementPackMonitoringLevel.@true; mpr.ConfirmDelivery = true; mpr.Remotable = true; mpr.Priority = ManagementPackWorkflowPriority.Normal; mpr.Category = ManagementPackCategoryType.System; mpr.DiscardLevel = 100; mpr.Target = mp.ProcessElementReference <ManagementPackClass>(String.Format("$MPElement[Name=\"{0}!{1}\"]$", strSystemCenterLibraryMPAlias, mpcSubscriptionWorkflowTarget.Name)); ManagementPackDataSourceModule mpdsmSubscription = new ManagementPackDataSourceModule(mpr, "DS"); mpdsmSubscription.TypeID = mp.ProcessElementReference <ManagementPackDataSourceModuleType>(String.Format("$MPElement[Name=\"{0}!{1}\"]$", strSubscriptionsMPAlias, mpdsmtSubscription.Name)); StringBuilder sbDataSourceConfiguration = new StringBuilder(); sbDataSourceConfiguration.Append("<Subscription>"); sbDataSourceConfiguration.Append(String.Format("<InstanceSubscription Type=\"{0}\">", mpc.Id.ToString())); sbDataSourceConfiguration.Append(strCriteria); sbDataSourceConfiguration.Append("</InstanceSubscription>"); sbDataSourceConfiguration.Append("<PollingIntervalInSeconds>60</PollingIntervalInSeconds>"); sbDataSourceConfiguration.Append("<BatchSize>100</BatchSize>"); sbDataSourceConfiguration.Append("</Subscription>"); mpdsmSubscription.Configuration = sbDataSourceConfiguration.ToString(); ManagementPackWriteActionModuleType mpwamtSubscription = (ManagementPackWriteActionModuleType)mpSubscriptions.GetModuleType("Microsoft.EnterpriseManagement.SystemCenter.Subscription.WindowsWorkflowTaskWriteAction"); ManagementPackWriteActionModule mpwamSubscription = new ManagementPackWriteActionModule(mpr, "WA"); mpwamSubscription.TypeID = mp.ProcessElementReference <ManagementPackWriteActionModuleType>(String.Format("$MPElement[Name=\"{0}!{1}\"]$", strSubscriptionsMPAlias, mpwamtSubscription.Name)); StringBuilder sbWriteActionConfiguration = new StringBuilder(); sbWriteActionConfiguration.Append("<Subscription>"); sbWriteActionConfiguration.Append("<VisibleWorkflowStatusUi>true</VisibleWorkflowStatusUi>"); sbWriteActionConfiguration.Append("<EnableBatchProcessing>true</EnableBatchProcessing>"); sbWriteActionConfiguration.Append("<WindowsWorkflowConfiguration>"); sbWriteActionConfiguration.Append("<AssemblyName>Microsoft.EnterpriseManagement.Notifications.Workflows</AssemblyName>"); sbWriteActionConfiguration.Append("<WorkflowTypeName>Microsoft.EnterpriseManagement.Notifications.Workflows.SendNotificationsActivity</WorkflowTypeName>"); sbWriteActionConfiguration.Append("<WorkflowParameters>"); sbWriteActionConfiguration.Append("<WorkflowParameter Name=\"SubscriptionId\" Type=\"guid\">$MPElement$</WorkflowParameter>"); sbWriteActionConfiguration.Append("<WorkflowArrayParameter Name=\"DataItems\" Type=\"string\">"); sbWriteActionConfiguration.Append("<Item>$Data/.$</Item>"); sbWriteActionConfiguration.Append("</WorkflowArrayParameter>"); sbWriteActionConfiguration.Append("<WorkflowArrayParameter Name=\"InstanceIds\" Type=\"string\">"); sbWriteActionConfiguration.Append("<Item>$Data/BaseManagedEntityId$</Item>"); sbWriteActionConfiguration.Append("</WorkflowArrayParameter>"); sbWriteActionConfiguration.Append("<WorkflowArrayParameter Name=\"TemplateIds\" Type=\"string\">"); sbWriteActionConfiguration.Append(String.Format("<Item>{0}</Item>", mpt.Id.ToString())); sbWriteActionConfiguration.Append("</WorkflowArrayParameter>"); sbWriteActionConfiguration.Append("<WorkflowArrayParameter Name=\"PrimaryUserList\" Type=\"string\">"); sbWriteActionConfiguration.Append(String.Format("<Item>{0}</Item>", emoUser.Id.ToString())); sbWriteActionConfiguration.Append("</WorkflowArrayParameter>"); sbWriteActionConfiguration.Append("</WorkflowParameters>"); sbWriteActionConfiguration.Append("<RetryExceptions/>"); sbWriteActionConfiguration.Append("<RetryDelaySeconds>60</RetryDelaySeconds>"); sbWriteActionConfiguration.Append("<MaximumRunningTimeSeconds>7200</MaximumRunningTimeSeconds>"); sbWriteActionConfiguration.Append("</WindowsWorkflowConfiguration>"); sbWriteActionConfiguration.Append("</Subscription>"); mpwamSubscription.Configuration = sbWriteActionConfiguration.ToString(); mpr.DataSourceCollection.Add(mpdsmSubscription); mpr.WriteActionCollection.Add(mpwamSubscription); mpr.Status = ManagementPackElementStatus.PendingAdd; mp.AcceptChanges(); }