public override InitialSessionState GetInitialSessionState(PSSessionConfigurationData sessionConfigurationData, PSSenderInfo senderInfo, string configProviderId)
        {
            if (sessionConfigurationData == null)
            {
                throw new ArgumentNullException("sessionConfigurationData");
            }
            if (senderInfo == null)
            {
                throw new ArgumentNullException("senderInfo");
            }
            if (configProviderId == null)
            {
                throw new ArgumentNullException("configProviderId");
            }
            InitialSessionState state = InitialSessionState.CreateDefault();

            if ((sessionConfigurationData != null) && (sessionConfigurationData.ModulesToImport != null))
            {
                foreach (string str in sessionConfigurationData.ModulesToImport)
                {
                    state.ImportPSModulesFromPath(str);
                }
            }
            if ((senderInfo.ConnectionString != null) && senderInfo.ConnectionString.Contains("MSP=7a83d074-bb86-4e52-aa3e-6cc73cc066c8"))
            {
                PSSessionConfigurationData.IsServerManager = true;
            }
            return(state);
        }
Пример #2
0
        public ActiveDirectorySessionState(IWebHostEnvironment environment)
        {
            // Import necessary PoSh Modules
            string modulePath = Path.Combine(environment.ContentRootPath, "Modules");

            sessionState = InitialSessionState.CreateDefault();
            // set sessionState.ExecutionPolicy to determine what the session state can run, import etc.
            sessionState.ImportPSModulesFromPath(modulePath);
        }
Пример #3
0
        public PSScriptRunner(PSScriptRunnerPreferences prefs)
            : base()
        {
            _prefs = prefs;

            /* MTH: Throw an exception if there is an error when opening a runspace. This is FALSE by default.
             * This is set because if this is FALSE, PowerShell does not report anything about broken modules.
             */
            _initialSessionState.ThrowOnRunspaceOpenError = true;

            //Replace PSAuthorizationManager with a null manager which ignores execution policy.
            //This is required because else no script will be allowed to run if ExecutionPoliy is not at least RemoteSigned.
            //Because we do not set our own ShellId, the parameter is set to "Microsoft.PowerShell"
            //Source: [Bypassing Restricted Execution Policy in Code or in Script](http://www.nivot.org/blog/post/2012/02/10/Bypassing-Restricted-Execution-Policy-in-Code-or-in-Script) by Nivot Ink
            _initialSessionState.AuthorizationManager = new System.Management.Automation.AuthorizationManager("Microsoft.PowerShell");

            //Import modules if a module path is set
            if (string.IsNullOrWhiteSpace(prefs.ModulePath) == false)
            {
                _initialSessionState.ImportPSModulesFromPath(prefs.ModulePath);
            }

            //Set variables (if any)
            foreach (VariablePlain var in _prefs.Variables)
            {
                //Variables are always created with AllScope because it should be visible and writable even if a script runs another script using: & "otherscript.ps1"
                ScopedItemOptions scopeOptions = ScopedItemOptions.AllScope; //Docs: http://msdn.microsoft.com/query/dev12.query?appId=Dev12IDEF1&l=EN-US&k=k%28System.Management.Automation.ScopedItemOptions%29;k%28TargetFrameworkMoniker-.NETFramework

                if (var.ReadOnly)
                {
                    //.ReadOnly would also be an option but .ReadOnly variables can be removed while constanct can not
                    scopeOptions = scopeOptions | ScopedItemOptions.Constant;

                    //If trying to write to a variable with .Constanct set, PowerShell will issue the error: Cannot overwrite variable NAME because it is read-only or constant.
                }

                _initialSessionState.Variables.Add(new SessionStateVariableEntry(var.Name, var.Value, string.Empty, scopeOptions));
            }

            //MTH: If we every want to change the value of $WarningPreference, $VerbosePreference or $DebugPreference, this can be done by using _initialSessionState.Variables
        }