Пример #1
0
        internal static ExecutionPolicy GetExecutionPolicy(
            string shellId,
            ExecutionPolicyScope scope)
        {
            switch (scope)
            {
            case ExecutionPolicyScope.Process:
                string environmentVariable = Environment.GetEnvironmentVariable("PSExecutionPolicyPreference");
                return(!string.IsNullOrEmpty(environmentVariable) ? SecuritySupport.ParseExecutionPolicy(environmentVariable) : ExecutionPolicy.Undefined);

            case ExecutionPolicyScope.CurrentUser:
            case ExecutionPolicyScope.LocalMachine:
                string localPreferenceValue = SecuritySupport.GetLocalPreferenceValue(shellId, scope);
                return(!string.IsNullOrEmpty(localPreferenceValue) ? SecuritySupport.ParseExecutionPolicy(localPreferenceValue) : ExecutionPolicy.Undefined);

            case ExecutionPolicyScope.UserPolicy:
            case ExecutionPolicyScope.MachinePolicy:
                string groupPolicyValue = SecuritySupport.GetGroupPolicyValue(shellId, scope);
                if (!string.IsNullOrEmpty(groupPolicyValue))
                {
                    Process process = Process.GetCurrentProcess();
                    string  a       = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "gpscript.exe");
                    bool    flag    = false;
                    try
                    {
                        for (; process != null; process = PsUtils.GetParentProcess(process))
                        {
                            if (string.Equals(a, PsUtils.GetMainModule(process).FileName, StringComparison.OrdinalIgnoreCase))
                            {
                                flag = true;
                                break;
                            }
                        }
                    }
                    catch (Win32Exception ex)
                    {
                    }
                    if (!flag)
                    {
                        return(SecuritySupport.ParseExecutionPolicy(groupPolicyValue));
                    }
                }
                return(ExecutionPolicy.Undefined);

            default:
                return(ExecutionPolicy.Restricted);
            }
        }
Пример #2
0
 private void ConstructParentId()
 {
     try
     {
         this.parentId = -1;
         Process parentProcess = PsUtils.GetParentProcess(this.OriginalProcessInstance);
         if (parentProcess != null)
         {
             this.parentId = parentProcess.Id;
         }
     }
     catch (Win32Exception)
     {
     }
     catch (InvalidOperationException)
     {
     }
     catch (ManagementException)
     {
     }
 }
Пример #3
0
        internal static ExecutionPolicy GetExecutionPolicy(string shellId, ExecutionPolicyScope scope)
        {
            string groupPolicyValue;
            bool   flag;

            switch (scope)
            {
            case ExecutionPolicyScope.Process:
            {
                string environmentVariable = Environment.GetEnvironmentVariable("PSExecutionPolicyPreference");
                if (string.IsNullOrEmpty(environmentVariable))
                {
                    return(ExecutionPolicy.Undefined);
                }
                return(ParseExecutionPolicy(environmentVariable));
            }

            case ExecutionPolicyScope.CurrentUser:
            case ExecutionPolicyScope.LocalMachine:
            {
                string localPreferenceValue = GetLocalPreferenceValue(shellId, scope);
                if (string.IsNullOrEmpty(localPreferenceValue))
                {
                    return(ExecutionPolicy.Undefined);
                }
                return(ParseExecutionPolicy(localPreferenceValue));
            }

            case ExecutionPolicyScope.UserPolicy:
            case ExecutionPolicyScope.MachinePolicy:
            {
                groupPolicyValue = GetGroupPolicyValue(shellId, scope);
                if (string.IsNullOrEmpty(groupPolicyValue))
                {
                    goto Label_00BA;
                }
                if (OSHelper.IsUnix)
                {
                    flag = true;
                }
                else
                {
                    Process currentProcess = Process.GetCurrentProcess();
                    string  a = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "gpscript.exe");
                    flag = false;
                    try
                    {
                        while (currentProcess != null)
                        {
                            if (string.Equals(a, PsUtils.GetMainModule(currentProcess).FileName, StringComparison.OrdinalIgnoreCase))
                            {
                                flag = true;
                                break;
                            }
                            currentProcess = PsUtils.GetParentProcess(currentProcess);
                        }
                    }
                    catch (Win32Exception)
                    {
                    }
                }
                break;
            }

            default:
                return(ExecutionPolicy.Restricted);
            }
            if (!flag)
            {
                return(ParseExecutionPolicy(groupPolicyValue));
            }
Label_00BA:
            return(ExecutionPolicy.Undefined);
        }