/// <summary>
        /// Serializes an PSObject whose baseobject is of primitive type.
        /// </summary>
        /// <param name="source">
        /// source from which notes are written
        /// </param>
        /// <param name="primitive">
        /// primitive object which is written as base object. In most cases it
        /// is same source.ImmediateBaseObject. When PSObject is serialized as string,
        /// </param>
        /// <param name="pktInfo">
        /// TypeSerializationInfo for the primitive.
        /// </param>
        /// <param name="property"></param>
        /// <param name="depth"></param>
        /// <param name="result"></param>
        private void CreateCimInstanceForPrimitiveTypePSObject
        (
            PSObject source,
            object primitive,
            MITypeSerializationInfo pktInfo,
            string property,
            int depth,
            out CimInstance result
        )
        {
            // To avoid compiler error
            result = CreateNullCimInstance();

            Dbg.Assert(source != null, "Caller should validate source != null");

            string toStringValue = SerializationUtilities.GetToStringForPrimitiveObject(source);
            bool   hasModifiedTypesCollection = false;
            //hasModifiedTypesCollection = PSObjectHasModifiedTypesCollection(source);

            bool hasNotes = false;
            //hasNotes = PSObjectHasNotes(source);

            bool hasModifiedToString = (toStringValue != null);

            if (hasNotes || hasModifiedTypesCollection || hasModifiedToString)
            {
                //TODO, insivara : TO BE IMPLEMENTED
                //WritePrimitiveTypePSObjectWithNotes(
                //    source,
                //    primitive,
                //    hasModifiedTypesCollection,
                //    toStringValue,
                //    pktInfo,
                //    streamName,
                //    property,
                //    depth);
                //return;
            }
            else
            {
                if (primitive != null)
                {
                    CreateCimInstanceForOnePrimitiveKnownType(this, property, primitive, pktInfo, out result);
                    return;
                }
                else
                {
                    //TODO, insivara : Return Null CimInstance
                    return;
                }
            }
        }
예제 #2
0
        internal static RemoteCommandInfo FromPSObjectForRemoting(PSObject psObject)
        {
            RemoteCommandInfo remoteCommandInfo = (RemoteCommandInfo)null;

            if (SerializationUtilities.GetPsObjectPropertyBaseObject(psObject, "CommandInfo_CommandType") != null)
            {
                CommandTypes propertyValue = RemotingDecoder.GetPropertyValue <CommandTypes>(psObject, "CommandInfo_CommandType");
                remoteCommandInfo            = new RemoteCommandInfo(RemotingDecoder.GetPropertyValue <string>(psObject, "CommandInfo_Name"), propertyValue);
                remoteCommandInfo.definition = RemotingDecoder.GetPropertyValue <string>(psObject, "CommandInfo_Definition");
                remoteCommandInfo.Visibility = RemotingDecoder.GetPropertyValue <SessionStateEntryVisibility>(psObject, "CommandInfo_Visibility");
            }
            return(remoteCommandInfo);
        }
예제 #3
0
 internal InformationalRecord(PSObject serializedObject)
 {
     this.message = (string)SerializationUtilities.GetPropertyValue(serializedObject, "InformationalRecord_Message");
     this.serializeExtendedInfo = (bool)SerializationUtilities.GetPropertyValue(serializedObject, "InformationalRecord_SerializeInvocationInfo");
     if (this.serializeExtendedInfo)
     {
         this.invocationInfo = new System.Management.Automation.InvocationInfo(serializedObject);
         ArrayList psObjectPropertyBaseObject = (ArrayList)SerializationUtilities.GetPsObjectPropertyBaseObject(serializedObject, "InformationalRecord_PipelineIterationInfo");
         this.pipelineIterationInfo = new ReadOnlyCollection <int>((int[])psObjectPropertyBaseObject.ToArray(Type.GetType("System.Int32")));
     }
     else
     {
         this.invocationInfo = null;
     }
 }
예제 #4
0
        /// <summary>
        /// Creates a RemoteCommandInfo from an instance serialized as a PSObject by ToPSObjectForRemoting.
        /// </summary>
        internal static RemoteCommandInfo FromPSObjectForRemoting(PSObject psObject)
        {
            RemoteCommandInfo commandInfo = null;

            object ctype = SerializationUtilities.GetPsObjectPropertyBaseObject(psObject, "CommandInfo_CommandType");

            if (ctype != null)
            {
                CommandTypes type = RemotingDecoder.GetPropertyValue <CommandTypes>(psObject, "CommandInfo_CommandType");
                string       name = RemotingDecoder.GetPropertyValue <string>(psObject, "CommandInfo_Name");

                commandInfo             = new RemoteCommandInfo(name, type);
                commandInfo._definition = RemotingDecoder.GetPropertyValue <string>(psObject, "CommandInfo_Definition");
                commandInfo.Visibility  = RemotingDecoder.GetPropertyValue <SessionStateEntryVisibility>(psObject, "CommandInfo_Visibility");
            }
            return(commandInfo);
        }
예제 #5
0
 internal InformationalRecord(PSObject serializedObject)
 {
     using (InformationalRecord.tracer.TraceMethod())
     {
         this.message = (string)SerializationUtilities.GetPropertyValue(serializedObject, "InformationalRecord_Message");
         this.serializeExtendedInfo = (bool)SerializationUtilities.GetPropertyValue(serializedObject, "InformationalRecord_SerializeInvocationInfo");
         if (this.serializeExtendedInfo)
         {
             this.invocationInfo        = new InvocationInfo(serializedObject);
             this.pipelineIterationInfo = new ReadOnlyCollection <int>((IList <int>)((ArrayList)SerializationUtilities.GetPsObjectPropertyBaseObject(serializedObject, "InformationalRecord_PipelineIterationInfo")).ToArray(Type.GetType("System.Int32")));
         }
         else
         {
             this.invocationInfo = (InvocationInfo)null;
         }
     }
 }
예제 #6
0
        /// <summary>
        /// Creates an InformationalRecord object from a record serialized as a PSObject by ToPSObjectForRemoting.
        /// </summary>
        internal InformationalRecord(PSObject serializedObject)
        {
            _message = (string)SerializationUtilities.GetPropertyValue(serializedObject, "InformationalRecord_Message");
            _serializeExtendedInfo = (bool)SerializationUtilities.GetPropertyValue(serializedObject, "InformationalRecord_SerializeInvocationInfo");

            if (_serializeExtendedInfo)
            {
                _invocationInfo = new InvocationInfo(serializedObject);

                ArrayList pipelineIterationInfo = (ArrayList)SerializationUtilities.GetPsObjectPropertyBaseObject(serializedObject, "InformationalRecord_PipelineIterationInfo");

                _pipelineIterationInfo = new ReadOnlyCollection <int>((int[])pipelineIterationInfo.ToArray(typeof(int)));
            }
            else
            {
                _invocationInfo = null;
            }
        }
예제 #7
0
        /// <summary>
        /// Creates an InformationalRecord from an instance serialized as a PSObject by ToPSObjectForRemoting.
        /// </summary>
        internal InvocationInfo(PSObject psObject)
        {
            CommandOrigin    = (CommandOrigin)SerializationUtilities.GetPsObjectPropertyBaseObject(psObject, "InvocationInfo_CommandOrigin");
            ExpectingInput   = (bool)SerializationUtilities.GetPropertyValue(psObject, "InvocationInfo_ExpectingInput");
            _invocationName  = (string)SerializationUtilities.GetPropertyValue(psObject, "InvocationInfo_InvocationName");
            HistoryId        = (long)SerializationUtilities.GetPropertyValue(psObject, "InvocationInfo_HistoryId");
            PipelineLength   = (int)SerializationUtilities.GetPropertyValue(psObject, "InvocationInfo_PipelineLength");
            PipelinePosition = (int)SerializationUtilities.GetPropertyValue(psObject, "InvocationInfo_PipelinePosition");

            string scriptName       = (string)SerializationUtilities.GetPropertyValue(psObject, "InvocationInfo_ScriptName");
            int    scriptLineNumber = (int)SerializationUtilities.GetPropertyValue(psObject, "InvocationInfo_ScriptLineNumber");
            int    offsetInLine     = (int)SerializationUtilities.GetPropertyValue(psObject, "InvocationInfo_OffsetInLine");
            string line             = (string)SerializationUtilities.GetPropertyValue(psObject, "InvocationInfo_Line");
            var    scriptPosition   = new ScriptPosition(scriptName, scriptLineNumber, offsetInLine, line);

            ScriptPosition scriptEndPosition;

            if (!string.IsNullOrEmpty(line))
            {
                int endColumn = line.Length + 1;
                scriptEndPosition = new ScriptPosition(scriptName, scriptLineNumber, endColumn, line);
            }
            else
            {
                scriptEndPosition = scriptPosition;
            }
            _scriptPosition = new ScriptExtent(scriptPosition, scriptEndPosition);

            MyCommand = RemoteCommandInfo.FromPSObjectForRemoting(psObject);

            //
            // Arrays are de-serialized as ArrayList so we need to convert the deserialized
            // object into an int[] before assigning to pipelineIterationInfo.
            //
            var list = (ArrayList)SerializationUtilities.GetPsObjectPropertyBaseObject(psObject, "InvocationInfo_PipelineIterationInfo");

            if (list != null)
            {
                PipelineIterationInfo = (int[])list.ToArray(typeof(Int32));
            }
            else
            {
                PipelineIterationInfo = Utils.EmptyArray <int>();
            }

            //
            // Dictionaries are de-serialized as Hashtables so we need to convert the deserialized object into a dictionary
            // before assigning to CommandLineParameters.
            //
            Hashtable hashtable = (Hashtable)SerializationUtilities.GetPsObjectPropertyBaseObject(psObject, "InvocationInfo_BoundParameters");

            Dictionary <string, object> dictionary = new Dictionary <string, object>();

            if (hashtable != null)
            {
                foreach (DictionaryEntry entry in hashtable)
                {
                    dictionary.Add((string)entry.Key, entry.Value);
                }
            }

            _boundParameters = dictionary;

            //
            // The unbound parameters are de-serialized as an ArrayList, which we need to convert to a List
            //
            var unboundArguments = (ArrayList)SerializationUtilities.GetPsObjectPropertyBaseObject(psObject, "InvocationInfo_UnboundArguments");

            _unboundArguments = new List <object>();

            if (unboundArguments != null)
            {
                foreach (object o in unboundArguments)
                {
                    _unboundArguments.Add(o);
                }
            }

            object value           = SerializationUtilities.GetPropertyValue(psObject, "SerializeExtent");
            bool   serializeExtent = false;

            if (value != null)
            {
                serializeExtent = (bool)value;
            }

            if (serializeExtent)
            {
                DisplayScriptPosition = ScriptExtent.FromPSObjectForRemoting(psObject);
            }
        }
        private void HandleComplexTypePSObject
        (
            object source,
            string property,
            int depth,
            out CimInstance result
        )
        {
            List <CimInstance> listOfCimInstancesProperties = null;

            Dbg.Assert(source != null, "caller should validate the parameter");
            PSObject mshSource = PSObject.AsPSObject(source);

            // Figure out what kind of object we are dealing with
            bool isErrorRecord         = false;
            bool isInformationalRecord = false;
            bool isEnum     = false;
            bool isPSObject = false;

            //TODO, insivara : To be implemented
            //bool isCimInstance = false;

            if (!mshSource.immediateBaseObjectIsEmpty)
            {
                ErrorRecord errorRecord = mshSource.ImmediateBaseObject as ErrorRecord;
                if (errorRecord == null)
                {
                    InformationalRecord informationalRecord = mshSource.ImmediateBaseObject as InformationalRecord;
                    if (informationalRecord == null)
                    {
                        isEnum     = mshSource.ImmediateBaseObject is Enum;
                        isPSObject = mshSource.ImmediateBaseObject is PSObject;
                    }
                    else
                    {
                        informationalRecord.ToPSObjectForRemoting(mshSource);
                        isInformationalRecord = true;
                    }
                }
                else
                {
                    errorRecord.ToPSObjectForRemoting(mshSource);
                    isErrorRecord = true;
                }
            }

            bool writeToString = true;

            if (mshSource.ToStringFromDeserialization == null)
            // continue to write ToString from deserialized objects, but...
            {
                if (mshSource.immediateBaseObjectIsEmpty) // ... don't write ToString for property bags
                {
                    writeToString = false;
                }
            }

            // This will create a CimInstance for PS_Object and populate the typenames.
            result = CreateCimInstanceForPSObject(cimClassName: "PS_Object",
                                                  psObj: mshSource,
                                                  writeToString: writeToString);

            PSMemberInfoInternalCollection <PSPropertyInfo> specificPropertiesToSerialize =
                SerializationUtilities.GetSpecificPropertiesToSerialize(mshSource, AllPropertiesCollection, _typeTable);

            if (isEnum)
            {
                CimInstance enumCimInstance = CreateCimInstanceForEnum(mshSource, depth, property != null);
                CimProperty p = CimProperty.Create("Value", enumCimInstance,
                                                   Microsoft.Management.Infrastructure.CimType.Reference,
                                                   Microsoft.Management.Infrastructure.CimFlags.Property);
                result.CimInstanceProperties.Add(p);
            }
            else if (isPSObject)
            {
                CimInstance psObjectCimInstance;
                CreateCimInstanceForOneObject(mshSource.ImmediateBaseObject, property, depth, out psObjectCimInstance);
                CimProperty valueProperty = CimProperty.Create("Value", psObjectCimInstance,
                                                               Microsoft.Management.Infrastructure.CimType.Reference,
                                                               Microsoft.Management.Infrastructure.CimFlags.Property);
                result.CimInstanceProperties.Add(valueProperty);
            }
            else if (isErrorRecord || isInformationalRecord)
            {
                // nothing to do
            }
            else
            {
                CreateCimInstanceForPSObjectProperties(mshSource, depth, specificPropertiesToSerialize, out listOfCimInstancesProperties);
            }

            //TODO, insivara : Implement serialization of CimInstance
            //if (isCimInstance)
            //{
            //    CimInstance cimInstance = mshSource.ImmediateBaseObject as CimInstance;
            //    PrepareCimInstanceForSerialization(mshSource, cimInstance);
            //}

            //TODO, insivara : ExtendedProperties implementation will be done in a subsequent checkin
            //SerializeExtendedProperties(mshSource, depth, specificPropertiesToSerialize, out listOfCimInstancesExtendedProperties);

            if (listOfCimInstancesProperties != null && listOfCimInstancesProperties.Count > 0)
            {
                CimInstance[] referenceArray = listOfCimInstancesProperties.ToArray();
                CimProperty   properties     = CimProperty.Create("Properties", referenceArray,
                                                                  Microsoft.Management.Infrastructure.CimType.ReferenceArray,
                                                                  Microsoft.Management.Infrastructure.CimFlags.Property);
                result.CimInstanceProperties.Add(properties);
            }
        }
        private bool HandleKnownContainerTypes
        (
            object source,
            string property,
            int depth,
            out CimInstance result
        )
        {
            Dbg.Assert(source != null, "caller should validate the parameter");

            result = CreateNullCimInstance();
            ContainerType ct         = ContainerType.None;
            PSObject      mshSource  = source as PSObject;
            IEnumerable   enumerable = null;
            IDictionary   dictionary = null;

            //If passed in object is PSObject with no baseobject, return false.
            if (mshSource != null && mshSource.immediateBaseObjectIsEmpty)
            {
                return(false);
            }

            //Check if source (or baseobject in mshSource) is known container type
            SerializationUtilities.GetKnownContainerTypeInfo(mshSource != null ? mshSource.ImmediateBaseObject : source, out ct,
                                                             out dictionary, out enumerable);

            if (ct == ContainerType.None)
            {
                return(false);
            }

            result = CreateCimInstanceForPSObject(cimClassName: "PS_Object",
                                                  psObj: mshSource ?? PSObject.AsPSObject(source),
                                                  writeToString: false);

            List <CimInstance> listOfCimInstances = null;

            switch (ct)
            {
            case ContainerType.Dictionary:
                WriteDictionary(dictionary, depth, out listOfCimInstances);
                break;

            case ContainerType.Stack:
                WriteEnumerable(enumerable, property, depth, out listOfCimInstances);
                break;

            case ContainerType.Queue:
                WriteEnumerable(enumerable, property, depth, out listOfCimInstances);
                break;

            case ContainerType.List:
                WriteEnumerable(enumerable, property, depth, out listOfCimInstances);
                break;

            case ContainerType.Enumerable:
                WriteEnumerable(enumerable, property, depth, out listOfCimInstances);
                break;

            default:
                Dbg.Assert(false, "All containers should be handled in the switch");
                break;
            }

            CimInstance[] instanceArray = listOfCimInstances.ToArray();
            CimProperty   valueProperty = CimProperty.Create("Value", instanceArray, Microsoft.Management.Infrastructure.CimType.InstanceArray, CimFlags.Property);

            result.CimInstanceProperties.Add(valueProperty);
            return(true);
        }
예제 #10
0
        internal InvocationInfo(PSObject psObject)
        {
            this._historyId             = -1L;
            this._pipelineIterationInfo = new int[0];
            this._commandOrigin         = (System.Management.Automation.CommandOrigin)SerializationUtilities.GetPsObjectPropertyBaseObject(psObject, "InvocationInfo_CommandOrigin");
            this._expectingInput        = (bool)SerializationUtilities.GetPropertyValue(psObject, "InvocationInfo_ExpectingInput");
            this._invocationName        = (string)SerializationUtilities.GetPropertyValue(psObject, "InvocationInfo_InvocationName");
            this._historyId             = (long)SerializationUtilities.GetPropertyValue(psObject, "InvocationInfo_HistoryId");
            this._pipelineLength        = (int)SerializationUtilities.GetPropertyValue(psObject, "InvocationInfo_PipelineLength");
            this._pipelinePosition      = (int)SerializationUtilities.GetPropertyValue(psObject, "InvocationInfo_PipelinePosition");
            string propertyValue    = (string)SerializationUtilities.GetPropertyValue(psObject, "InvocationInfo_ScriptName");
            int    scriptLineNumber = (int)SerializationUtilities.GetPropertyValue(psObject, "InvocationInfo_ScriptLineNumber");
            int    offsetInLine     = (int)SerializationUtilities.GetPropertyValue(psObject, "InvocationInfo_OffsetInLine");
            string line             = (string)SerializationUtilities.GetPropertyValue(psObject, "InvocationInfo_Line");

            System.Management.Automation.Language.ScriptPosition startPosition = new System.Management.Automation.Language.ScriptPosition(propertyValue, scriptLineNumber, offsetInLine, line);
            this._scriptPosition = new ScriptExtent(startPosition, startPosition);
            this._commandInfo    = RemoteCommandInfo.FromPSObjectForRemoting(psObject);
            ArrayList psObjectPropertyBaseObject = (ArrayList)SerializationUtilities.GetPsObjectPropertyBaseObject(psObject, "InvocationInfo_PipelineIterationInfo");

            if (psObjectPropertyBaseObject != null)
            {
                this._pipelineIterationInfo = (int[])psObjectPropertyBaseObject.ToArray(Type.GetType("System.Int32"));
            }
            else
            {
                this._pipelineIterationInfo = new int[0];
            }
            Hashtable hashtable = (Hashtable)SerializationUtilities.GetPsObjectPropertyBaseObject(psObject, "InvocationInfo_BoundParameters");
            Dictionary <string, object> dictionary = new Dictionary <string, object>();

            if (hashtable != null)
            {
                foreach (DictionaryEntry entry in hashtable)
                {
                    dictionary.Add((string)entry.Key, entry.Value);
                }
            }
            this._boundParameters = dictionary;
            ArrayList list2 = (ArrayList)SerializationUtilities.GetPsObjectPropertyBaseObject(psObject, "InvocationInfo_UnboundArguments");

            this._unboundArguments = new List <object>();
            if (list2 != null)
            {
                foreach (object obj2 in list2)
                {
                    this._unboundArguments.Add(obj2);
                }
            }
            object obj3 = SerializationUtilities.GetPropertyValue(psObject, "SerializeExtent");
            bool   flag = false;

            if (obj3 != null)
            {
                flag = (bool)obj3;
            }
            if (flag)
            {
                this._displayScriptPosition = ScriptExtent.FromPSObjectForRemoting(psObject);
            }
        }