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);
Version verSystemVersion = mpSystem.Version;
string strSystemKeyToken = mpSystem.KeyToken;
//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", 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.Connecto 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;
//Create Connector instance
cemoAAConnector.Commit();
//Accept the rule changes which updates the database
mpConnectors.AcceptChanges();
}
catch (Exception e)
{
MessageBox.Show(e.Message + e.InnerException.Message);
}
}