예제 #1
0
        internal object ConvertFromDotNetToCim(object dotNetObject)
        {
            if (dotNetObject == null)
            {
                return(null);
            }

            PSObject psObject   = PSObject.AsPSObject(dotNetObject);
            Type     dotNetType = psObject.BaseObject.GetType();

            if (typeof(PSCredential).IsAssignableFrom(dotNetType))
            {
                var credential = (PSCredential)(psObject.BaseObject);

                string escapedUsername = credential.UserName;
                escapedUsername = escapedUsername.Replace("\\", "\\\\"); // Esc backslashes
                escapedUsername = escapedUsername.Replace(PSCredentialDelimiter, "\\" + PSCredentialDelimiter);

                var sensitiveString = new SensitiveString(escapedUsername.Length + PSCredentialDelimiter.Length + credential.Password.Length);
                lock (_trackedDisposables) { _trackedDisposables.Add(sensitiveString); }

                sensitiveString.Copy(escapedUsername, 0);
                sensitiveString.Copy(PSCredentialDelimiter, escapedUsername.Length);
                sensitiveString.Copy(credential.Password, escapedUsername.Length + PSCredentialDelimiter.Length);
                return(sensitiveString.Value);
            }

            if (typeof(SecureString).IsAssignableFrom(dotNetType))
            {
                SecureString secureString    = (SecureString)psObject.BaseObject;
                var          sensitiveString = new SensitiveString(secureString.Length);
                lock (_trackedDisposables) { _trackedDisposables.Add(sensitiveString); }

                sensitiveString.Copy(secureString, 0);
                return(sensitiveString.Value);
            }

            if (dotNetType.IsArray)
            {
                Type dotNetElementType = CimValueConverter.GetElementType(dotNetType);
                if (dotNetElementType != null)
                {
                    var   dotNetArray    = (Array)psObject.BaseObject;
                    Type  cimElementType = GetCimType(dotNetElementType);
                    Array cimArray       = Array.CreateInstance(cimElementType, dotNetArray.Length);
                    for (int i = 0; i < cimArray.Length; i++)
                    {
                        object cimElement = ConvertFromDotNetToCim(dotNetArray.GetValue(i));
                        cimArray.SetValue(cimElement, i);
                    }

                    return(cimArray);
                }
            }

            return(CimValueConverter.ConvertFromDotNetToCim(dotNetObject));
        }
예제 #2
0
 internal static object ConvertFromDotNetToCim(object dotNetObject)
 {
     if (dotNetObject != null)
     {
         PSObject pSObject = PSObject.AsPSObject(dotNetObject);
         Type     type     = pSObject.BaseObject.GetType();
         if (!LanguagePrimitives.IsCimIntrinsicScalarType(type))
         {
             if (!typeof(CimInstance).IsAssignableFrom(type))
             {
                 if (!typeof(PSReference).IsAssignableFrom(type))
                 {
                     if (type.IsArray)
                     {
                         Type elementType = CimValueConverter.GetElementType(type);
                         if (elementType != null)
                         {
                             Array baseObject = (Array)pSObject.BaseObject;
                             Type  cimType    = CimValueConverter.GetCimType(elementType);
                             Array arrays     = Array.CreateInstance(cimType, baseObject.Length);
                             for (int i = 0; i < arrays.Length; i++)
                             {
                                 object cim = CimValueConverter.ConvertFromDotNetToCim(baseObject.GetValue(i));
                                 arrays.SetValue(cim, i);
                             }
                             return(arrays);
                         }
                     }
                     Type convertibleCimType = CimValueConverter.GetConvertibleCimType(type);
                     if (convertibleCimType == null)
                     {
                         if (!typeof(ObjectSecurity).IsAssignableFrom(type))
                         {
                             if (!typeof(X509Certificate2).IsAssignableFrom(type))
                             {
                                 if (!typeof(X500DistinguishedName).IsAssignableFrom(type))
                                 {
                                     if (!typeof(PhysicalAddress).IsAssignableFrom(type))
                                     {
                                         if (!typeof(IPEndPoint).IsAssignableFrom(type))
                                         {
                                             if (!typeof(WildcardPattern).IsAssignableFrom(type))
                                             {
                                                 if (!typeof(XmlDocument).IsAssignableFrom(type))
                                                 {
                                                     throw CimValueConverter.GetInvalidCastException(null, "InvalidDotNetToCimCast", dotNetObject, CmdletizationResources.CimConversion_CimIntrinsicValue);
                                                 }
                                                 else
                                                 {
                                                     XmlDocument xmlDocument = (XmlDocument)pSObject.BaseObject;
                                                     string      outerXml    = xmlDocument.OuterXml;
                                                     return(outerXml);
                                                 }
                                             }
                                             else
                                             {
                                                 WildcardPattern wildcardPattern = (WildcardPattern)pSObject.BaseObject;
                                                 return(wildcardPattern.ToWql());
                                             }
                                         }
                                         else
                                         {
                                             object obj = LanguagePrimitives.ConvertTo(dotNetObject, typeof(string), CultureInfo.InvariantCulture);
                                             return(obj);
                                         }
                                     }
                                     else
                                     {
                                         object obj1 = LanguagePrimitives.ConvertTo(dotNetObject, typeof(string), CultureInfo.InvariantCulture);
                                         return(obj1);
                                     }
                                 }
                                 else
                                 {
                                     X500DistinguishedName x500DistinguishedName = (X500DistinguishedName)pSObject.BaseObject;
                                     byte[] rawData = x500DistinguishedName.RawData;
                                     return(rawData);
                                 }
                             }
                             else
                             {
                                 X509Certificate2 x509Certificate2 = (X509Certificate2)pSObject.BaseObject;
                                 byte[]           numArray         = x509Certificate2.RawData;
                                 return(numArray);
                             }
                         }
                         else
                         {
                             string sddl = SecurityDescriptorCommandsBase.GetSddl(pSObject);
                             return(sddl);
                         }
                     }
                     else
                     {
                         object obj2 = LanguagePrimitives.ConvertTo(dotNetObject, convertibleCimType, CultureInfo.InvariantCulture);
                         return(obj2);
                     }
                 }
                 else
                 {
                     PSReference pSReference = (PSReference)pSObject.BaseObject;
                     if (pSReference.Value != null)
                     {
                         PSObject pSObject1 = PSObject.AsPSObject(pSReference.Value);
                         return(CimValueConverter.ConvertFromDotNetToCim(pSObject1.BaseObject));
                     }
                     else
                     {
                         return(null);
                     }
                 }
             }
             else
             {
                 return(pSObject.BaseObject);
             }
         }
         else
         {
             return(pSObject.BaseObject);
         }
     }
     else
     {
         return(null);
     }
 }