/// <summary>
        /// Checks whether the default runspace associated with the current thread has the standard Get-Help cmdlet.
        /// <summary>
        /// <return>True if Get-Help is found, false otherwise.</return>
        private static bool DoesCurrentRunspaceIncludeCoreHelpCmdlet()
        {
            InitialSessionState iss = Runspace.DefaultRunspace.InitialSessionState;

            if (iss is null)
            {
                return(false);
            }

            Collection <SessionStateCommandEntry> getHelpEntries = iss.Commands["Get-Help"];
            SessionStateCommandEntry getHelpEntry = null;

            for (int i = 0; i < getHelpEntries.Count; ++i)
            {
                if (getHelpEntries[i].Visibility is not SessionStateEntryVisibility.Public)
                {
                    continue;
                }

                // If we have multiple entries for Get-Help,
                // our assumption is that the standard Get-Help is not available.
                if (getHelpEntry is not null)
                {
                    return(false);
                }

                getHelpEntry = getHelpEntries[i];
            }

            return(getHelpEntry is SessionStateCmdletEntry getHelpCmdlet &&
                   getHelpCmdlet.ImplementingType == typeof(GetHelpCommand));
        }
        /// <summary>
        /// Gets custom initial session state
        /// It relies on the RBAC system to give list of commands allowed for a user
        /// and creates Initial Session state from that
        /// </summary>
        /// <param name="senderInfo">Sender information</param>
        /// <returns>Custom initial Session state</returns>
        public override InitialSessionState GetInitialSessionState(PSSenderInfo senderInfo)
        {
            if (senderInfo == null)
            {
                throw new ArgumentNullException("senderInfo");
            }

            if (senderInfo.UserInfo == null)
            {
                throw new ArgumentException("senderInfo.UserInfo is null");
            }

            InitialSessionState initialSessionState = InitialSessionState.CreateDefault();

            foreach (SessionStateCommandEntry command in initialSessionState.Commands)
            {
                command.Visibility = SessionStateEntryVisibility.Private;
            }

            List <string> scripts = RbacSystem.Current.GetScripts(senderInfo.UserInfo);

            foreach (string script in scripts)
            {
                initialSessionState.Commands.Add(new SessionStateScriptEntry(script));
            }

            List <string> modules = RbacSystem.Current.GetModules(senderInfo.UserInfo);

            if (modules.Count > 0)
            {
                initialSessionState.ImportPSModule(modules.ToArray());
            }

            // enable execution of scripts in this process
            System.Environment.SetEnvironmentVariable("PSExecutionPolicyPreference", "unrestricted");

            List <string> cmdletsFromRbac = RbacSystem.Current.GetCmdlets(senderInfo.UserInfo);

            // Add all commands from Rbac system to Initial Session State commands
            foreach (string cmdlet in cmdletsFromRbac)
            {
                SessionStateCommandEntry cmdletFromRbac = initialSessionState.Commands.FirstOrDefault(item => string.Equals(item.Name, cmdlet, StringComparison.OrdinalIgnoreCase));
                if (cmdletFromRbac == null)
                {
                    throw new ArgumentException("Command not found in InitialSessionState " + cmdlet);
                }

                cmdletFromRbac.Visibility = SessionStateEntryVisibility.Public;
            }

            return(initialSessionState);
        }
Пример #3
0
 // Token: 0x06001417 RID: 5143 RVA: 0x000489D3 File Offset: 0x00046BD3
 internal SessionStateCommandEntryWithMetadata(SessionStateCommandEntry sessionStateCommandEntry, CommandMetadata commandMetadata)
 {
     this.SessionStateCommandEntry = sessionStateCommandEntry;
     this.CommandMetadata          = commandMetadata;
 }