Exemplo n.º 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));
        }
Exemplo n.º 2
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);
        }