Beispiel #1
0
        public override InitialSessionState GetInitialSessionState(PSSessionConfigurationData sessionConfigurationData, PSSenderInfo senderInfo, string configProviderId)
        {
            Tracer tracer = new Tracer();

            tracer.Correlate();
            if (sessionConfigurationData != null)
            {
                if (senderInfo != null)
                {
                    if (!string.IsNullOrEmpty(configProviderId))
                    {
                        if (Interlocked.CompareExchange(ref PSWorkflowSessionConfiguration._modulesLoaded, 1, 0) == 0)
                        {
                            try
                            {
                                PSWorkflowConfigurationProvider configuration = WorkflowJobSourceAdapter.GetInstance().GetPSWorkflowRuntime().Configuration;
                                if (configuration != null)
                                {
                                    configuration.Populate(sessionConfigurationData.PrivateData, configProviderId, senderInfo);
                                    if (sessionConfigurationData.ModulesToImport != null)
                                    {
                                        foreach (string modulesToImport in sessionConfigurationData.ModulesToImport)
                                        {
                                            PSWorkflowSessionConfiguration.InitialSessionState.ImportPSModulesFromPath(modulesToImport);
                                        }
                                    }
                                }
                                else
                                {
                                    throw new InvalidOperationException("PSWorkflowConfigurationProvider is null");
                                }
                            }
                            catch (Exception exception)
                            {
                                Interlocked.CompareExchange(ref PSWorkflowSessionConfiguration._modulesLoaded, 0, 1);
                                throw;
                            }
                        }
                        if (configProviderId.ToLower(CultureInfo.InvariantCulture).Equals("http://schemas.microsoft.com/powershell/microsoft.windows.servermanagerworkflows"))
                        {
                            PSSessionConfigurationData.IsServerManager = true;
                        }
                        return(PSWorkflowSessionConfiguration.InitialSessionState);
                    }
                    else
                    {
                        throw new ArgumentNullException("configProviderId");
                    }
                }
                else
                {
                    throw new ArgumentNullException("senderInfo");
                }
            }
            else
            {
                throw new ArgumentNullException("sessionConfigurationData");
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="sessionConfigurationData"></param>
        /// <param name="senderInfo"></param>
        /// <param name="configProviderId"></param>
        /// <returns></returns>
        public override InitialSessionState GetInitialSessionState(PSSessionConfigurationData sessionConfigurationData, PSSenderInfo senderInfo, string configProviderId)
        {
            Tracer structuredTracer = new Tracer();

            structuredTracer.Correlate();

            if (sessionConfigurationData == null)
            {
                throw new ArgumentNullException("sessionConfigurationData");
            }

            if (senderInfo == null)
            {
                throw new ArgumentNullException("senderInfo");
            }

            if (string.IsNullOrEmpty(configProviderId))
            {
                throw new ArgumentNullException("configProviderId");
            }

            if (Interlocked.CompareExchange(ref _modulesLoaded, ModulesLoaded, ModulesNotLoaded) == ModulesNotLoaded)
            {
                // it is sufficient if Populate() is called the first time and
                // modules are loaded once

                try
                {
                    IsWorkflowTypeEndpoint = true;

                    PSWorkflowConfigurationProvider workflowConfiguration = WorkflowJobSourceAdapter.GetInstance().GetPSWorkflowRuntime().Configuration;
                    if (workflowConfiguration == null)
                    {
                        throw new InvalidOperationException("PSWorkflowConfigurationProvider is null");
                    }

                    workflowConfiguration.Populate(sessionConfigurationData.PrivateData, configProviderId, senderInfo);

                    // now get all the modules in the specified path and import the same
                    if (sessionConfigurationData.ModulesToImport != null)
                    {
                        foreach (var module in sessionConfigurationData.ModulesToImport)
                        {
                            ModuleSpecification moduleSpec = null;
                            if (ModuleSpecification.TryParse(module, out moduleSpec))
                            {
                                var modulesToImport = new Collection <ModuleSpecification> {
                                    moduleSpec
                                };
                                InitialSessionState.ImportPSModule(modulesToImport);
                            }
                            else
                            {
                                InitialSessionState.ImportPSModule(new[] { Environment.ExpandEnvironmentVariables(module) });
                            }
                        }
                    }

                    // Start the workflow job manager, if not started, to add an event handler for zero active sessions changed events
                    // This is required to auto shutdown the workflow type shared process when no workflow jobs have scheduled/inprogress and when no active sessions
                    WorkflowJobSourceAdapter.GetInstance().GetJobManager();
                }
                catch (Exception)
                {
                    // if there is an exception in either Populate() or Importing modules
                    // we consider that it is not loaded
                    Interlocked.CompareExchange(ref _modulesLoaded, ModulesNotLoaded, ModulesLoaded);
                    throw;
                }
            }

            if (configProviderId.ToLower(CultureInfo.InvariantCulture).Equals("http://schemas.microsoft.com/powershell/microsoft.windows.servermanagerworkflows"))
            {
                PSSessionConfigurationData.IsServerManager = true;
            }

            return(InitialSessionState);
        }