Ejemplo n.º 1
0
        internal static object InvokeAdaptedMember(object obj, string methodName, object[] args)
        {
            TypeTable        typeTable;
            ExecutionContext executionContextFromTLS = LocalPipeline.GetExecutionContextFromTLS();
            object           obj1 = obj;

            if (executionContextFromTLS != null)
            {
                typeTable = executionContextFromTLS.TypeTable;
            }
            else
            {
                typeTable = null;
            }
            PSObject.AdapterSet mappedAdapter = PSObject.GetMappedAdapter(obj1, typeTable);
            PSMethodInfo        pSMethodInfo  = mappedAdapter.OriginalAdapter.BaseGetMember <PSMemberInfo>(obj, methodName) as PSMethodInfo;

            if (pSMethodInfo == null && mappedAdapter.DotNetAdapter != null)
            {
                pSMethodInfo = mappedAdapter.DotNetAdapter.BaseGetMember <PSMemberInfo>(obj, methodName) as PSMethodInfo;
            }
            if (pSMethodInfo == null)
            {
                object[] typeFullName = new object[2];
                typeFullName[0] = ParserOps.GetTypeFullName(obj);
                typeFullName[1] = methodName;
                throw InterpreterError.NewInterpreterException(methodName, typeof(RuntimeException), null, "MethodNotFound", ParserStrings.MethodNotFound, typeFullName);
            }
            else
            {
                return(pSMethodInfo.Invoke(args));
            }
        }
Ejemplo n.º 2
0
        public void TooManyArgumentsForPSMethodInvoke()
        {
            var          obj      = new Object();
            var          psObject = new PSObject(obj);
            PSMethodInfo method   = psObject.Methods.First(m => m.Name == "ToString");

            MethodException ex = Assert.Throws <MethodException>(() => method.Invoke(1, 2, 3, 4, 5));

            Assert.AreEqual("Cannot find an overload for \"ToString\" and the argument count: \"5\".", ex.Message);
        }
Ejemplo n.º 3
0
        private void MethodCallWithArguments()
        {
            ReadOnlyPSMemberInfoCollection <PSMemberInfo> infos = this._inputObject.Members.Match(this._propertyOrMethodName, PSMemberTypes.ParameterizedProperty | PSMemberTypes.Methods);

            if (infos.Count > 1)
            {
                StringBuilder builder = new StringBuilder();
                foreach (PSMemberInfo info in infos)
                {
                    builder.AppendFormat(CultureInfo.InvariantCulture, " {0}", new object[] { info.Name });
                }
                base.WriteError(GenerateNameParameterError("Name", InternalCommandStrings.AmbiguousMethodName, "AmbiguousMethodName", this._inputObject, new object[] { this._propertyOrMethodName, builder }));
            }
            else if ((infos.Count == 0) || !(infos[0] is PSMethodInfo))
            {
                base.WriteError(GenerateNameParameterError("Name", InternalCommandStrings.MethodNotFound, "MethodNotFound", this._inputObject, new object[] { this._propertyOrMethodName }));
            }
            else
            {
                PSMethodInfo  info2    = infos[0] as PSMethodInfo;
                StringBuilder builder2 = new StringBuilder(GetStringRepresentation(this._arguments[0]));
                for (int i = 1; i < this._arguments.Length; i++)
                {
                    builder2.AppendFormat(CultureInfo.InvariantCulture, ", {0}", new object[] { GetStringRepresentation(this._arguments[i]) });
                }
                string action = string.Format(CultureInfo.InvariantCulture, InternalCommandStrings.ForEachObjectMethodActionWithArguments, new object[] { info2.Name, builder2 });
                try
                {
                    if (base.ShouldProcess(this.targetString, action) && !this.BlockMethodInLanguageMode(this.InputObject))
                    {
                        object obj2 = info2.Invoke(this._arguments);
                        this.WriteToPipelineWithUnrolling(obj2);
                    }
                }
                catch (PipelineStoppedException)
                {
                    throw;
                }
                catch (Exception exception)
                {
                    CommandProcessorBase.CheckForSevereException(exception);
                    base.WriteError(new ErrorRecord(exception, "MethodInvocationError", ErrorCategory.InvalidOperation, this._inputObject));
                }
            }
        }
Ejemplo n.º 4
0
        protected override void ProcessRecord()
        {
            string         sddlForm;
            ObjectSecurity objectSecurity = this.securityDescriptor as ObjectSecurity;

            if (this.inputObject == null)
            {
                if (this.Path != null)
                {
                    if (objectSecurity != null)
                    {
                        if ((this.CentralAccessPolicy != null || this.ClearCentralAccessPolicy) && !DownLevelHelper.IsWin8AndAbove())
                        {
                            Exception parameterBindingException = new ParameterBindingException();
                            base.WriteError(new ErrorRecord(parameterBindingException, "SetAcl_OperationNotSupported", ErrorCategory.InvalidArgument, null));
                            return;
                        }
                        else
                        {
                            if (this.CentralAccessPolicy == null || !this.ClearCentralAccessPolicy)
                            {
                                IntPtr zero = IntPtr.Zero;
                                NativeMethods.TOKEN_PRIVILEGE tOKENPRIVILEGE = new NativeMethods.TOKEN_PRIVILEGE();
                                try
                                {
                                    if (this.CentralAccessPolicy == null)
                                    {
                                        if (this.ClearCentralAccessPolicy)
                                        {
                                            zero = this.GetEmptySacl();
                                            if (zero == IntPtr.Zero)
                                            {
                                                SystemException systemException = new SystemException(UtilsStrings.GetEmptySaclFail);
                                                base.WriteError(new ErrorRecord(systemException, "SetAcl_ClearCentralAccessPolicy", ErrorCategory.InvalidResult, null));
                                                return;
                                            }
                                        }
                                    }
                                    else
                                    {
                                        zero = this.GetSaclWithCapId(this.CentralAccessPolicy);
                                        if (zero == IntPtr.Zero)
                                        {
                                            SystemException systemException1 = new SystemException(UtilsStrings.GetSaclWithCapIdFail);
                                            base.WriteError(new ErrorRecord(systemException1, "SetAcl_CentralAccessPolicy", ErrorCategory.InvalidResult, null));
                                            return;
                                        }
                                    }
                                    string[] path = this.Path;
                                    for (int i = 0; i < (int)path.Length; i++)
                                    {
                                        string str = path[i];
                                        Collection <PathInfo> pathInfos             = new Collection <PathInfo>();
                                        CmdletProviderContext cmdletProviderContext = base.CmdletProviderContext;
                                        cmdletProviderContext.PassThru = this.Passthru;
                                        if (!this.isLiteralPath)
                                        {
                                            pathInfos = base.SessionState.Path.GetResolvedPSPathFromPSPath(str, base.CmdletProviderContext);
                                        }
                                        else
                                        {
                                            ProviderInfo providerInfo = null;
                                            PSDriveInfo  pSDriveInfo  = null;
                                            string       unresolvedProviderPathFromPSPath = base.SessionState.Path.GetUnresolvedProviderPathFromPSPath(str, out providerInfo, out pSDriveInfo);
                                            pathInfos.Add(new PathInfo(pSDriveInfo, providerInfo, unresolvedProviderPathFromPSPath, base.SessionState));
                                            cmdletProviderContext.SuppressWildcardExpansion = true;
                                        }
                                        foreach (PathInfo pathInfo in pathInfos)
                                        {
                                            if (!base.ShouldProcess(pathInfo.Path))
                                            {
                                                continue;
                                            }
                                            try
                                            {
                                                base.InvokeProvider.SecurityDescriptor.Set(pathInfo.Path, objectSecurity, cmdletProviderContext);
                                                if (this.CentralAccessPolicy != null || this.ClearCentralAccessPolicy)
                                                {
                                                    if (pathInfo.Provider.NameEquals(base.Context.ProviderNames.FileSystem))
                                                    {
                                                        IntPtr tokenWithEnabledPrivilege = this.GetTokenWithEnabledPrivilege("SeSecurityPrivilege", tOKENPRIVILEGE);
                                                        if (tokenWithEnabledPrivilege != IntPtr.Zero)
                                                        {
                                                            int num = NativeMethods.SetNamedSecurityInfo(pathInfo.ProviderPath, NativeMethods.SeObjectType.SE_FILE_OBJECT, NativeMethods.SecurityInformation.SCOPE_SECURITY_INFORMATION, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, zero);
                                                            if (tokenWithEnabledPrivilege != IntPtr.Zero)
                                                            {
                                                                NativeMethods.TOKEN_PRIVILEGE tOKENPRIVILEGE1 = new NativeMethods.TOKEN_PRIVILEGE();
                                                                uint num1 = 0;
                                                                NativeMethods.AdjustTokenPrivileges(tokenWithEnabledPrivilege, false, ref tOKENPRIVILEGE, Marshal.SizeOf(tOKENPRIVILEGE1), ref tOKENPRIVILEGE1, ref num1);
                                                                NativeMethods.CloseHandle(tokenWithEnabledPrivilege);
                                                            }
                                                            if (num != 0)
                                                            {
                                                                SystemException win32Exception = new Win32Exception(num, UtilsStrings.SetCentralAccessPolicyFail);
                                                                base.WriteError(new ErrorRecord(win32Exception, "SetAcl_SetNamedSecurityInfo", ErrorCategory.InvalidResult, null));
                                                            }
                                                        }
                                                        else
                                                        {
                                                            SystemException systemException2 = new SystemException(UtilsStrings.GetTokenWithEnabledPrivilegeFail);
                                                            base.WriteError(new ErrorRecord(systemException2, "SetAcl_AdjustTokenPrivileges", ErrorCategory.InvalidResult, null));
                                                            return;
                                                        }
                                                    }
                                                    else
                                                    {
                                                        Exception argumentException = new ArgumentException("Path");
                                                        base.WriteError(new ErrorRecord(argumentException, "SetAcl_Path", ErrorCategory.InvalidArgument, this.AclObject));
                                                        continue;
                                                    }
                                                }
                                            }
                                            catch (NotSupportedException notSupportedException)
                                            {
                                                object[] objArray = new object[1];
                                                objArray[0] = pathInfo.Path;
                                                ErrorRecord errorRecord = SecurityUtils.CreateNotSupportedErrorRecord(UtilsStrings.OperationNotSupportedOnPath, "SetAcl_OperationNotSupported", objArray);
                                                base.WriteError(errorRecord);
                                            }
                                        }
                                    }
                                    return;
                                }
                                finally
                                {
                                    Marshal.FreeHGlobal(zero);
                                }
                            }
                            else
                            {
                                Exception   exception    = new ArgumentException(UtilsStrings.InvalidCentralAccessPolicyParameters);
                                ErrorRecord errorRecord1 = SecurityUtils.CreateInvalidArgumentErrorRecord(exception, "SetAcl_OperationNotSupported");
                                base.WriteError(errorRecord1);
                                return;
                            }
                        }
                    }
                    else
                    {
                        Exception argumentException1 = new ArgumentException("AclObject");
                        base.WriteError(new ErrorRecord(argumentException1, "SetAcl_AclObject", ErrorCategory.InvalidArgument, this.AclObject));
                        return;
                    }
                }
                else
                {
                    Exception exception1 = new ArgumentException("Path");
                    base.WriteError(new ErrorRecord(exception1, "SetAcl_Path", ErrorCategory.InvalidArgument, this.AclObject));
                }
            }
            else
            {
                PSMethodInfo item = this.inputObject.Methods["SetSecurityDescriptor"];
                if (item == null)
                {
                    ErrorRecord errorRecord2 = SecurityUtils.CreateNotSupportedErrorRecord(UtilsStrings.SetMethodNotFound, "SetAcl_OperationNotSupported", new object[0]);
                    base.WriteError(errorRecord2);
                    return;
                }
                else
                {
                    CommonSecurityDescriptor commonSecurityDescriptor = this.securityDescriptor as CommonSecurityDescriptor;
                    if (objectSecurity == null)
                    {
                        if (commonSecurityDescriptor == null)
                        {
                            Exception argumentException2 = new ArgumentException("AclObject");
                            base.WriteError(new ErrorRecord(argumentException2, "SetAcl_AclObject", ErrorCategory.InvalidArgument, this.AclObject));
                            return;
                        }
                        else
                        {
                            sddlForm = commonSecurityDescriptor.GetSddlForm(AccessControlSections.All);
                        }
                    }
                    else
                    {
                        sddlForm = objectSecurity.GetSecurityDescriptorSddlForm(AccessControlSections.All);
                    }
                    try
                    {
                        object[] objArray1 = new object[1];
                        objArray1[0] = sddlForm;
                        item.Invoke(objArray1);
                        return;
                    }
                    catch (Exception exception3)
                    {
                        Exception exception2 = exception3;
                        CommandProcessorBase.CheckForSevereException(exception2);
                        ErrorRecord errorRecord3 = SecurityUtils.CreateNotSupportedErrorRecord(UtilsStrings.MethodInvokeFail, "SetAcl_OperationNotSupported", new object[0]);
                        base.WriteError(errorRecord3);
                    }
                }
            }
        }
Ejemplo n.º 5
0
        protected override void ProcessRecord()
        {
            AccessControlSections accessControlSection = AccessControlSections.Access | AccessControlSections.Owner | AccessControlSections.Group;

            if (this.audit)
            {
                accessControlSection = accessControlSection | AccessControlSections.Audit;
            }
            if (this.inputObject == null)
            {
                string[] path = this.Path;
                for (int i = 0; i < (int)path.Length; i++)
                {
                    string        str  = path[i];
                    List <string> strs = new List <string>();
                    string        str1 = null;
                    try
                    {
                        if (!this.isLiteralPath)
                        {
                            Collection <PathInfo> resolvedPSPathFromPSPath = base.SessionState.Path.GetResolvedPSPathFromPSPath(str, base.CmdletProviderContext);
                            foreach (PathInfo pathInfo in resolvedPSPathFromPSPath)
                            {
                                strs.Add(pathInfo.Path);
                            }
                        }
                        else
                        {
                            strs.Add(base.SessionState.Path.GetUnresolvedProviderPathFromPSPath(str));
                        }
                        foreach (string str2 in strs)
                        {
                            str1 = str2;
                            CmdletProviderContext cmdletProviderContext = new CmdletProviderContext(base.Context);
                            cmdletProviderContext.SuppressWildcardExpansion = true;
                            if (base.InvokeProvider.Item.Exists(str2, false, this.isLiteralPath))
                            {
                                base.InvokeProvider.SecurityDescriptor.Get(str2, accessControlSection, cmdletProviderContext);
                                Collection <PSObject> accumulatedObjects = cmdletProviderContext.GetAccumulatedObjects();
                                if (accumulatedObjects == null)
                                {
                                    continue;
                                }
                                SecurityDescriptorCommandsBase.AddBrokeredProperties(accumulatedObjects, this.audit, this.allCentralAccessPolicies);
                                base.WriteObject(accumulatedObjects, true);
                            }
                            else
                            {
                                ErrorRecord errorRecord = SecurityUtils.CreatePathNotFoundErrorRecord(str2, "GetAcl_PathNotFound");
                                base.WriteError(errorRecord);
                            }
                        }
                    }
                    catch (NotSupportedException notSupportedException)
                    {
                        object[] objArray = new object[1];
                        objArray[0] = str1;
                        ErrorRecord errorRecord1 = SecurityUtils.CreateNotSupportedErrorRecord(UtilsStrings.OperationNotSupportedOnPath, "GetAcl_OperationNotSupported", objArray);
                        base.WriteError(errorRecord1);
                    }
                    catch (ItemNotFoundException itemNotFoundException)
                    {
                        ErrorRecord errorRecord2 = SecurityUtils.CreatePathNotFoundErrorRecord(str, "GetAcl_PathNotFound_Exception");
                        base.WriteError(errorRecord2);
                    }
                }
            }
            else
            {
                PSMethodInfo item = this.inputObject.Methods["GetSecurityDescriptor"];
                if (item == null)
                {
                    ErrorRecord errorRecord3 = SecurityUtils.CreateNotSupportedErrorRecord(UtilsStrings.GetMethodNotFound, "GetAcl_OperationNotSupported", new object[0]);
                    base.WriteError(errorRecord3);
                    return;
                }
                else
                {
                    object commonSecurityDescriptor = null;
                    try
                    {
                        commonSecurityDescriptor = PSObject.Base(item.Invoke(new object[0]));
                        if (commonSecurityDescriptor as FileSystemSecurity == null)
                        {
                            commonSecurityDescriptor = new CommonSecurityDescriptor(false, false, commonSecurityDescriptor.ToString());
                        }
                    }
                    catch (Exception exception1)
                    {
                        Exception exception = exception1;
                        CommandProcessorBase.CheckForSevereException(exception);
                        ErrorRecord errorRecord4 = SecurityUtils.CreateNotSupportedErrorRecord(UtilsStrings.MethodInvokeFail, "GetAcl_OperationNotSupported", new object[0]);
                        base.WriteError(errorRecord4);
                        return;
                    }
                    base.WriteObject(commonSecurityDescriptor, true);
                    return;
                }
            }
        }
Ejemplo n.º 6
0
        protected override void ProcessRecord()
        {
            string parameterSetName = base.ParameterSetName;

            if (parameterSetName == null)
            {
                return;
            }
            if (!(parameterSetName == "ScriptBlockSet"))
            {
                if (!(parameterSetName == "PropertyAndMethodSet"))
                {
                    return;
                }
            }
            else
            {
                for (int i = this.start; i < this.end; i++)
                {
                    if (this.scripts[i] != null)
                    {
                        this.scripts[i].InvokeUsingCmdlet(this, false, ScriptBlock.ErrorHandlingBehavior.WriteToCurrentErrorPipe, this.InputObject, new object[] { this.InputObject }, AutomationNull.Value, new object[0]);
                    }
                }
                return;
            }
            this.targetString = string.Format(CultureInfo.InvariantCulture, InternalCommandStrings.ForEachObjectTarget, new object[] { GetStringRepresentation(this.InputObject) });
            if (LanguagePrimitives.IsNull(this.InputObject))
            {
                if ((this._arguments != null) && (this._arguments.Length > 0))
                {
                    base.WriteError(GenerateNameParameterError("InputObject", ParserStrings.InvokeMethodOnNull, "InvokeMethodOnNull", this._inputObject, new object[0]));
                    return;
                }
                string action = string.Format(CultureInfo.InvariantCulture, InternalCommandStrings.ForEachObjectPropertyAction, new object[] { this._propertyOrMethodName });
                if (base.ShouldProcess(this.targetString, action))
                {
                    if (base.Context.IsStrictVersion(2))
                    {
                        base.WriteError(GenerateNameParameterError("InputObject", InternalCommandStrings.InputObjectIsNull, "InputObjectIsNull", this._inputObject, new object[0]));
                        return;
                    }
                    base.WriteObject(null);
                }
                return;
            }
            ErrorRecord errorRecord = null;

            if ((this._arguments != null) && (this._arguments.Length > 0))
            {
                this.MethodCallWithArguments();
            }
            else
            {
                if (this.GetValueFromIDictionaryInput())
                {
                    return;
                }
                PSMemberInfo info = null;
                if (WildcardPattern.ContainsWildcardCharacters(this._propertyOrMethodName))
                {
                    ReadOnlyPSMemberInfoCollection <PSMemberInfo> infos = this._inputObject.Members.Match(this._propertyOrMethodName, PSMemberTypes.All);
                    if (infos.Count > 1)
                    {
                        StringBuilder builder = new StringBuilder();
                        foreach (PSMemberInfo info2 in infos)
                        {
                            builder.AppendFormat(CultureInfo.InvariantCulture, " {0}", new object[] { info2.Name });
                        }
                        base.WriteError(GenerateNameParameterError("Name", InternalCommandStrings.AmbiguousPropertyOrMethodName, "AmbiguousPropertyOrMethodName", this._inputObject, new object[] { this._propertyOrMethodName, builder }));
                        return;
                    }
                    if (infos.Count == 1)
                    {
                        info = infos[0];
                    }
                }
                else
                {
                    info = this._inputObject.Members[this._propertyOrMethodName];
                }
                if (info == null)
                {
                    errorRecord = GenerateNameParameterError("Name", InternalCommandStrings.PropertyOrMethodNotFound, "PropertyOrMethodNotFound", this._inputObject, new object[] { this._propertyOrMethodName });
                }
                else
                {
                    if (info is PSMethodInfo)
                    {
                        PSParameterizedProperty property = info as PSParameterizedProperty;
                        if (property != null)
                        {
                            string str2 = string.Format(CultureInfo.InvariantCulture, InternalCommandStrings.ForEachObjectPropertyAction, new object[] { property.Name });
                            if (base.ShouldProcess(this.targetString, str2))
                            {
                                base.WriteObject(info.Value);
                            }
                            return;
                        }
                        PSMethodInfo info3 = info as PSMethodInfo;
                        try
                        {
                            string str3 = string.Format(CultureInfo.InvariantCulture, InternalCommandStrings.ForEachObjectMethodActionWithoutArguments, new object[] { info3.Name });
                            if (base.ShouldProcess(this.targetString, str3) && !this.BlockMethodInLanguageMode(this.InputObject))
                            {
                                object obj2 = info3.Invoke(new object[0]);
                                this.WriteToPipelineWithUnrolling(obj2);
                            }
                            goto Label_0451;
                        }
                        catch (PipelineStoppedException)
                        {
                            throw;
                        }
                        catch (Exception exception)
                        {
                            CommandProcessorBase.CheckForSevereException(exception);
                            MethodException exception2 = exception as MethodException;
                            if (((exception2 != null) && (exception2.ErrorRecord != null)) && (exception2.ErrorRecord.FullyQualifiedErrorId == "MethodCountCouldNotFindBest"))
                            {
                                base.WriteObject(info3.Value);
                            }
                            else
                            {
                                base.WriteError(new ErrorRecord(exception, "MethodInvocationError", ErrorCategory.InvalidOperation, this._inputObject));
                            }
                            goto Label_0451;
                        }
                    }
                    string str4 = string.Format(CultureInfo.InvariantCulture, InternalCommandStrings.ForEachObjectPropertyAction, new object[] { info.Name });
                    if (base.ShouldProcess(this.targetString, str4))
                    {
                        try
                        {
                            this.WriteToPipelineWithUnrolling(info.Value);
                        }
                        catch (TerminateException)
                        {
                            throw;
                        }
                        catch (MethodException)
                        {
                            throw;
                        }
                        catch (PipelineStoppedException)
                        {
                            throw;
                        }
                        catch (Exception exception3)
                        {
                            CommandProcessorBase.CheckForSevereException(exception3);
                            base.WriteObject(null);
                        }
                    }
                }
            }
Label_0451:
            if (errorRecord != null)
            {
                string str5 = string.Format(CultureInfo.InvariantCulture, InternalCommandStrings.ForEachObjectPropertyAction, new object[] { this._propertyOrMethodName });
                if (base.ShouldProcess(this.targetString, str5))
                {
                    if (base.Context.IsStrictVersion(2))
                    {
                        base.WriteError(errorRecord);
                        return;
                    }
                    base.WriteObject(null);
                }
            }
        }