示例#1
0
        protected override void BeginProcessing()
        {
            WriteVerbose("Getting current process handle");
            _process = PrivilegeHelper.GetCurrentProcess();

            WriteVerbose("Getting privilege info for all privileges on the current process");
            _privInfo = PrivilegeHelper.GetAllPrivilegeInfo(_process);
        }
示例#2
0
        protected override void ProcessRecord()
        {
            WriteVerbose("Getting current process handle");
            using SafeHandle processToken = PrivilegeHelper.GetCurrentProcess();

            WriteVerbose("Getting privilege info for all privileges on the current process");
            Dictionary <string, PrivilegeAttributes> privilegeInfo = PrivilegeHelper.GetAllPrivilegeInfo(processToken);

            if (Name.Length == 0)
            {
                Name = privilegeInfo.Keys.ToArray();
            }

            foreach (string privName in Name)
            {
                if (!PrivilegeHelper.CheckPrivilegeName(privName))
                {
                    ItemNotFoundException exp = new ItemNotFoundException($"Invalid privilege name '{privName}'");
                    WriteError(new ErrorRecord(exp, "PrivilegeNotFound", ErrorCategory.ObjectNotFound, privName));
                    continue;
                }

                string description       = PrivilegeHelper.GetPrivilegeDisplayName(privName);
                bool   enabled           = false;
                bool   enableByDefault   = false;
                PrivilegeAttributes attr = PrivilegeAttributes.Removed;
                bool isRemoved           = true;

                if (privilegeInfo.ContainsKey(privName))
                {
                    attr            = privilegeInfo[privName];
                    enabled         = (attr & PrivilegeAttributes.Enabled) != 0;
                    enableByDefault = (attr & PrivilegeAttributes.EnabledByDefault) != 0;
                    isRemoved       = false;
                }

                WriteObject(new Privilege()
                {
                    Name             = privName,
                    Description      = description,
                    Enabled          = enabled,
                    EnabledByDefault = enableByDefault,
                    Attributes       = attr,
                    IsRemoved        = isRemoved,
                });
            }
        }