protected override void ExecuteCmdlet()
        {
            base.ExecuteCmdlet();

            if (Mode == CrmPluginStepMode.Asynchronous && Stage != CrmPluginStepStage.PostOperation)
            {
                throw new Exception("Asynchronous Execution Mode requires registration in one of the Post Stages. Please change the Mode or the Stage.");
            }
            if (!string.IsNullOrWhiteSpace(SecureConfig) && (Deployment == CrmPluginStepDeployment.OfflineOnly || Deployment == CrmPluginStepDeployment.Both))
            {
                throw new Exception("Secure Configuration is not supported for Steps deployed Offline.");
            }

            Entity crmEventSource = RetrieveEventSource(EventSource);

            if (crmEventSource == null)
            {
                throw new Exception(string.Format("No Plugin or ServiceEndpoint found with Id '{0}'.", EventSource));
            }

            SdkMessageFilter = RetrieveMessageFilter(Message, PrimaryEntity, SecondaryEntity);
            if (SdkMessageFilter == null)
            {
                throw new Exception(string.Format("No supported Steps found for '{0}' on entities {1} and {2}.", Message, PrimaryEntity, SecondaryEntity));
            }
            else
            {
                SdkMessage      = SdkMessageFilter.GetAttributeValue <EntityReference>("sdkmessageid");
                Message         = SdkMessage.Name;
                PrimaryEntity   = SdkMessageFilter.GetAttributeValue <string>("primaryobjecttypecode");
                SecondaryEntity = SdkMessageFilter.GetAttributeValue <string>("secondaryobjecttypecode");
            }

            int filterAvailability = SdkMessageFilter.GetAttributeValue <int>("availability");

            if (!IsDeploymentSupported(filterAvailability, Deployment))
            {
                throw new Exception(string.Format("The Step must be deployed '{0}'.", Enum.GetName(typeof(CrmPluginStepDeployment), filterAvailability)));
            }

            if (IsServiceEndpoint && Mode == CrmPluginStepMode.Synchronous)
            {
                throw new Exception("Only asynchronous Steps are supported for Service Endpoint plug-ins.");
            }

            string generatedDescription = string.Empty;

            if (IsServiceEndpoint)
            {
                generatedDescription = PluginManagementHelper.GenerateStepDescription(crmEventSource.GetAttributeValue <string>("name"), Message, PrimaryEntity, SecondaryEntity);
            }
            else
            {
                generatedDescription = PluginManagementHelper.GenerateStepDescription(crmEventSource.GetAttributeValue <string>("typename"), Message, PrimaryEntity, SecondaryEntity);
            }
            if (string.IsNullOrWhiteSpace(Name))
            {
                Name = generatedDescription;
            }
            if (string.IsNullOrWhiteSpace(Description))
            {
                Description = generatedDescription;
            }

            Guid secureConfigId = Guid.Empty;

            if (!string.IsNullOrWhiteSpace(SecureConfig))
            {
                secureConfigId = _repository.Add("sdkmessageprocessingstepsecureconfig", Guid.Empty, new System.Collections.Hashtable()
                {
                    { "secureconfig", SecureConfig }
                });
            }

            Guid newId = _repository.Add(GenerateCrmEntity(secureConfigId));

            if (PassThru)
            {
                WriteObject(_repository.Get("sdkmessageprocessingstep", newId));
            }
        }
        private void RegisterPluginAssembly(string path)
        {
            PluginAssemblyInfo assemblyInfo = PluginManagementHelper.RetrievePluginAssemblyInfo(path);

            PluginManagementHelper.RefreshFromExistingAssembly(_repository, assemblyInfo);

            assemblyInfo.SourceType    = AssemblyLocation ?? CrmAssemblySourceType.Database;
            assemblyInfo.IsolationMode = IsolationMode ?? CrmAssemblyIsolationMode.Sandbox;
            if (Description != null)
            {
                assemblyInfo.Description = Description;
            }
            if (assemblyInfo.AssemblyId != Guid.Empty && Force.ToBool() != true)
            {
                throw new Exception(string.Format("Assembly '{0}' is already registered. Use Force to overwrite the assembly.", assemblyInfo.Name));
            }

            List <PluginTypeInfo> pluginsToRegister = GetPluginsToRegister(assemblyInfo);

            if (pluginsToRegister.Count == 0)
            {
                throw new Exception("No Plugins for registration.");
            }
            if (assemblyInfo.IsolationMode == CrmAssemblyIsolationMode.Sandbox && pluginsToRegister.Any(p => p.Isolatable != true))
            {
                throw new Exception("Since some of the plug-ins cannot be isolated, the assembly cannot be marked as Isolated.");
            }
            if (string.IsNullOrWhiteSpace(assemblyInfo.PublicKeyToken) && pluginsToRegister.Any(p => p.PluginType == CrmPluginType.Plugin))
            {
                throw new Exception("Assemblies containing Plugins must be strongly signed. Sign the Assembly using a KeyFile.");
            }

            Entity pluginAssemblyEntity = GenerateCrmEntity(assemblyInfo);
            Guid   assemblyId           = pluginAssemblyEntity.Id;

            if (assemblyId == Guid.Empty)
            {
                assemblyId = _repository.Add(pluginAssemblyEntity);
            }
            else
            {
                _repository.Update(pluginAssemblyEntity);
            }

            foreach (PluginTypeInfo plugin in pluginsToRegister)
            {
                Entity pluginTypeEntity = GenerateCrmEntity(assemblyId, plugin);
                Guid   pluginTypeId     = pluginTypeEntity.Id;
                if (pluginTypeEntity.Id == Guid.Empty)
                {
                    pluginTypeId = _repository.Add(pluginTypeEntity);
                }
                else
                {
                    _repository.Update(pluginTypeEntity);
                }

                if (PassThru)
                {
                    WriteObject(_repository.Get("plugintype", pluginTypeId));
                }
            }
        }