예제 #1
0
        protected XmlDocument LoadXmlDocumentFromFileLoadingInfo(AuthorizationManager authorizationManager, PSHost host, out bool isFullyTrusted)
        {
            // get file contents
            ExternalScriptInfo ps1xmlInfo   = new ExternalScriptInfo(FilePath, FilePath);
            string             fileContents = ps1xmlInfo.ScriptContents;

            isFullyTrusted = false;
            if (ps1xmlInfo.DefiningLanguageMode == PSLanguageMode.FullLanguage)
            {
                isFullyTrusted = true;
            }

            if (authorizationManager != null)
            {
                try
                {
                    authorizationManager.ShouldRunInternal(ps1xmlInfo, CommandOrigin.Internal, host);
                }
                catch (PSSecurityException reason)
                {
                    string errorMessage = StringUtil.Format(TypesXmlStrings.ValidationException,
                                                            string.Empty /* TODO/FIXME snapin */,
                                                            FilePath,
                                                            reason.Message);
                    ReportLogEntryHelper(errorMessage, XmlLoaderLoggerEntry.EntryType.Error, failToLoadFile: true);
                    return(null);
                }
            }

            // load file into XML document
            try
            {
                XmlDocument doc = InternalDeserializer.LoadUnsafeXmlDocument(
                    fileContents,
                    true,  /* preserve whitespace, comments, etc. */
                    null); /* default maxCharacters */
                this.ReportTrace("XmlDocument loaded OK");
                return(doc);
            }
            catch (XmlException e)
            {
                this.ReportError(StringUtil.Format(FormatAndOutXmlLoadingStrings.ErrorInFile, FilePath, e.Message));
                this.ReportTrace("XmlDocument discarded");
                return(null);
            }
            catch (Exception e) // will rethrow
            {
                CommandProcessor.CheckForSevereException(e);
                throw;
            }
        }
예제 #2
0
        /// <exception cref="PSInvalidCastException">The only kind of exception this method can throw</exception>
        internal static object ConvertFromCimToDotNet(object cimObject, Type expectedDotNetType)
        {
            if (expectedDotNetType == null)
            {
                throw new ArgumentNullException("expectedDotNetType");
            }

            if (cimObject == null)
            {
                return(null);
            }

            if (expectedDotNetType.GetTypeInfo().IsGenericType&& expectedDotNetType.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                expectedDotNetType = expectedDotNetType.GetGenericArguments()[0];
            }

            if (LanguagePrimitives.IsCimIntrinsicScalarType(expectedDotNetType))
            {
                return(LanguagePrimitives.ConvertTo(cimObject, expectedDotNetType, CultureInfo.InvariantCulture));
            }
            if (expectedDotNetType == typeof(CimInstance))
            {
                return(LanguagePrimitives.ConvertTo(cimObject, expectedDotNetType, CultureInfo.InvariantCulture));
            }

            if (expectedDotNetType.IsArray)
            {
                Type dotNetElementType = GetElementType(expectedDotNetType);
                if (dotNetElementType != null)
                {
                    var   cimArray    = (Array)LanguagePrimitives.ConvertTo(cimObject, typeof(Array), CultureInfo.InvariantCulture);
                    Array dotNetArray = Array.CreateInstance(dotNetElementType, cimArray.Length);
                    for (int i = 0; i < dotNetArray.Length; i++)
                    {
                        object dotNetElement = ConvertFromCimToDotNet(cimArray.GetValue(i), dotNetElementType);
                        dotNetArray.SetValue(dotNetElement, i);
                    }
                    return(dotNetArray);
                }
            }

            Type convertibleCimType = GetConvertibleCimType(expectedDotNetType);

            if (convertibleCimType != null)
            {
                object cimIntrinsicValue = LanguagePrimitives.ConvertTo(cimObject, convertibleCimType, CultureInfo.InvariantCulture);
                object dotNetObject      = LanguagePrimitives.ConvertTo(cimIntrinsicValue, expectedDotNetType, CultureInfo.InvariantCulture);
                return(dotNetObject);
            }

            Func <Func <object>, object> exceptionSafeReturn = delegate(Func <object> innerAction)
            {
                try
                {
                    return(innerAction());
                }
                catch (Exception e)
                {
                    CommandProcessor.CheckForSevereException(e);
                    throw CimValueConverter.GetInvalidCastException(
                              e,
                              "InvalidCimToDotNetCast",
                              cimObject,
                              expectedDotNetType.FullName);
                }
            };

            if (typeof(ObjectSecurity).IsAssignableFrom(expectedDotNetType))
            {
                var sddl = (string)LanguagePrimitives.ConvertTo(cimObject, typeof(string), CultureInfo.InvariantCulture);
                return(exceptionSafeReturn(delegate
                {
                    var objectSecurity = (ObjectSecurity)Activator.CreateInstance(expectedDotNetType);
                    objectSecurity.SetSecurityDescriptorSddlForm(sddl);
                    return objectSecurity;
                }));
            }

            if (typeof(X509Certificate2) == expectedDotNetType)
            {
                var cimIntrinsicValue = (byte[])LanguagePrimitives.ConvertTo(cimObject, typeof(byte[]), CultureInfo.InvariantCulture);
                return(exceptionSafeReturn(delegate
                {
                    return new X509Certificate2(cimIntrinsicValue);
                }));
            }

            if (typeof(X500DistinguishedName) == expectedDotNetType)
            {
                var cimIntrinsicValue = (byte[])LanguagePrimitives.ConvertTo(cimObject, typeof(byte[]), CultureInfo.InvariantCulture);
                return(exceptionSafeReturn(delegate
                {
                    return new X500DistinguishedName(cimIntrinsicValue);
                }));
            }

            if (typeof(PhysicalAddress) == expectedDotNetType)
            {
                var cimIntrinsicValue = (string)LanguagePrimitives.ConvertTo(cimObject, typeof(string), CultureInfo.InvariantCulture);
                return(exceptionSafeReturn(delegate
                {
                    return PhysicalAddress.Parse(cimIntrinsicValue);
                }));
            }

            if (typeof(IPEndPoint) == expectedDotNetType)
            {
                var cimIntrinsicValue = (string)LanguagePrimitives.ConvertTo(cimObject, typeof(string), CultureInfo.InvariantCulture);
                return(exceptionSafeReturn(delegate
                {
                    int indexOfLastColon = cimIntrinsicValue.LastIndexOf(':');
                    int port = int.Parse(cimIntrinsicValue.Substring(indexOfLastColon + 1), NumberStyles.Integer, CultureInfo.InvariantCulture);
                    IPAddress address = IPAddress.Parse(cimIntrinsicValue.Substring(0, indexOfLastColon));
                    return new IPEndPoint(address, port);
                }));
            }

            // WildcardPattern is only supported as an "in" parameter - we do not support the reverse translation (i.e. from "a%" to "a*")

            if (typeof(XmlDocument) == expectedDotNetType)
            {
                var cimIntrinsicValue = (string)LanguagePrimitives.ConvertTo(cimObject, typeof(string), CultureInfo.InvariantCulture);
                return(exceptionSafeReturn(delegate
                {
                    XmlDocument doc = InternalDeserializer.LoadUnsafeXmlDocument(
                        cimIntrinsicValue,
                        true,                                /* preserve non elements: whitespace, processing instructions, comments, etc. */
                        null);                               /* default maxCharactersInDocument */
                    return doc;
                }));
            }

            // unrecognized type = throw invalid cast exception
            throw CimValueConverter.GetInvalidCastException(
                      null, /* inner exception */
                      "InvalidCimToDotNetCast",
                      cimObject,
                      expectedDotNetType.FullName);
        }