PowerShell Remoting Project Home

Sunday, April 09, 2006

MshObject, the Magic Therein

From Arul Kumaravel's WebLog: "Monad is the only scripting platform that provides access to .Net, WMI, COM in a well-integrated manner."

How? It is MshObject which does all the tricks. MshObject serve as an adapter to .Net, WMI and COM objects. For more details, read MSH Object Concepts.

Here are members of System.Management.Automation.MshObject:
public class MshObject : IFormattable
{
      // Methods
      static MshObject();
      public MshObject();
      public MshObject(object obj);
      public static MshObject AsMshObject(object obj);
      public virtual MshObject Copy();
      public override string ToString();
      public string ToString(string format, IFormatProvider formatProvider);

      // Properties
      public object BaseObject { get; }
      public object ImmediateBaseObject { get; }
      public MshMemberInfoCollection Members { get; }
      public MshMemberInfoCollection Methods { get; }
      public MshMemberInfoCollection Properties { get; }
      public Collection<string> TypeNames { get; }
}
MshObject is hidden under the surface of Msh. You might not notice its exsitance unless you write a cmdlet which deal with MshObject directly. I  prevously working on Serialization of MshObject in my Monad Remoting project. I wrote a small cmdlet to track Mshobject within MSH.exe
using System;
using System.Collections.Generic;
using System.Text;
using System.Management.Automation;

namespace MshCx
{
    [Cmdlet("get", "mshobject")]
    public class GetMshobject: Cmdlet
    {

        private MshObject[] inputObjects = null;
        [Parameter(Mandatory = false,ValueFromPipeline = true)]
        public MshObject[] InputObjects
        {
            get { return inputObjects; }
            set { inputObjects = value; }
        }

        protected override void ProcessRecord()
        {
            if (inputObjects != null)
            {
                StringBuilder sb = new StringBuilder();
                foreach (MshObject Item in inputObjects)
                {
                    sb.AppendLine();
                    sb.AppendLine("MshObject: ");
                    sb.AppendLine("\t" + Item.ToString());
                    sb.AppendLine("BaseObject: ");
                    sb.AppendLine("\t"+ Item.BaseObject.ToString() + " (" + Item.BaseObject.GetType().FullName + ")");
                    sb.AppendLine("ImmediateBaseObject: ");
                    sb.AppendLine("\t" + Item.ImmediateBaseObject.ToString() + " (" + Item.ImmediateBaseObject.GetType().FullName + ")");
                    sb.AppendLine("TypeNames: ");
                    foreach (string Type in Item.TypeNames)
                    {
                        sb.AppendLine("\t"+ Type);
                    }
                    sb.AppendLine("=============================================================");
                }
                WriteObject(sb.ToString());
            }
        }
    }
}
It is really intersting to see how export-clixml and import-clixml works:
> $file= (get-childitem)[0]
> $file

    Directory: Microsoft.Management.Automation.Core\FileSystem::D:\msh


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---          2006-4-3     22:27        350 1.txt

> $file | get-member

   TypeName: System.IO.FileInfo

Name                      MemberType     Definition
----                      ----------     ----------
AppendText                Method         System.IO.StreamWriter AppendText()
CopyTo                    Method         System.IO.FileInfo CopyTo(String de...
Create                    Method         System.IO.FileStream Create()
CreateObjRef              Method         System.Runtime.Remoting.ObjRef Crea...
CreateText                Method         System.IO.StreamWriter CreateText()
Decrypt                   Method         System.Void Decrypt()
Delete                    Method         System.Void Delete()
Encrypt                   Method         System.Void Encrypt()
Equals                    Method         System.Boolean Equals(Object obj)
get_Attributes            Method         System.IO.FileAttributes get_Attrib...
get_CreationTime          Method         System.DateTime get_CreationTime()
get_CreationTimeUtc       Method         System.DateTime get_CreationTimeUtc()
get_Directory             Method         System.IO.DirectoryInfo get_Directo...
get_DirectoryName         Method         System.String get_DirectoryName()
get_Exists                Method         System.Boolean get_Exists()
get_Extension             Method         System.String get_Extension()
get_FullName              Method         System.String get_FullName()
get_IsReadOnly            Method         System.Boolean get_IsReadOnly()
get_LastAccessTime        Method         System.DateTime get_LastAccessTime()
get_LastAccessTimeUtc     Method         System.DateTime get_LastAccessTimeU...
get_LastWriteTime         Method         System.DateTime get_LastWriteTime()
get_LastWriteTimeUtc      Method         System.DateTime get_LastWriteTimeUtc()
get_Length                Method         System.Int64 get_Length()
get_Name                  Method         System.String get_Name()
GetAccessControl          Method         System.Security.AccessControl.FileS...
GetHashCode               Method         System.Int32 GetHashCode()
GetLifetimeService        Method         System.Object GetLifetimeService()
GetObjectData             Method         System.Void GetObjectData(Serializa...
GetType                   Method         System.Type GetType()
InitializeLifetimeService Method         System.Object InitializeLifetimeSer...
MoveTo                    Method         System.Void MoveTo(String destFileN...
Open                      Method         System.IO.FileStream Open(FileMode ...
OpenRead                  Method         System.IO.FileStream OpenRead()
OpenText                  Method         System.IO.StreamReader OpenText()
OpenWrite                 Method         System.IO.FileStream OpenWrite()
Refresh                   Method         System.Void Refresh()
Replace                   Method         System.IO.FileInfo Replace(String d...
set_Attributes            Method         System.Void set_Attributes(FileAttr...
set_CreationTime          Method         System.Void set_CreationTime(DateTi...
set_CreationTimeUtc       Method         System.Void set_CreationTimeUtc(Dat...
set_IsReadOnly            Method         System.Void set_IsReadOnly(Boolean ...
set_LastAccessTime        Method         System.Void set_LastAccessTime(Date...
set_LastAccessTimeUtc     Method         System.Void set_LastAccessTimeUtc(D...
set_LastWriteTime         Method         System.Void set_LastWriteTime(DateT...
set_LastWriteTimeUtc      Method         System.Void set_LastWriteTimeUtc(Da...
SetAccessControl          Method         System.Void SetAccessControl(FileSe...
ToString                  Method         System.String ToString()
MshChildName              NoteProperty   System.String MshChildName=1.txt
MshDrive                  NoteProperty   System.Management.Automation.DriveI...
MshIsContainer            NoteProperty   System.Boolean MshIsContainer=False
MshParentPath             NoteProperty   System.String MshParentPath=Microso...
MshPath                   NoteProperty   System.String MshPath=Microsoft.Man...
MshProvider               NoteProperty   System.Management.Automation.Provid...
Attributes                Property       System.IO.FileAttributes Attributes...
CreationTime              Property       System.DateTime CreationTime {get;s...
CreationTimeUtc           Property       System.DateTime CreationTimeUtc {ge...
Directory                 Property       System.IO.DirectoryInfo Directory {...
DirectoryName             Property       System.String DirectoryName {get;}
Exists                    Property       System.Boolean Exists {get;}
Extension                 Property       System.String Extension {get;}
FullName                  Property       System.String FullName {get;}
IsReadOnly                Property       System.Boolean IsReadOnly {get;set;}
LastAccessTime            Property       System.DateTime LastAccessTime {get...
LastAccessTimeUtc         Property       System.DateTime LastAccessTimeUtc {...
LastWriteTime             Property       System.DateTime LastWriteTime {get;...
LastWriteTimeUtc          Property       System.DateTime LastWriteTimeUtc {g...
Length                    Property       System.Int64 Length {get;}
Name                      Property       System.String Name {get;}
Mode                      ScriptProperty System.Object Mode {get=$catr = "";
> $file | get-mshobject

MshObject:
        1.txt
BaseObject:
        1.txt (System.IO.FileInfo)
ImmediateBaseObject:
        1.txt (System.IO.FileInfo)
TypeNames:
        System.IO.FileInfo
        System.IO.FileSystemInfo
        System.MarshalByRefObject
        System.Object
=============================================================
> $file |export-clixml abc.xml

> $file = import-clixml abc.xml
> $file

    Directory: Microsoft.Management.Automation.Core\FileSystem::D:\msh


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
               2006-4-3     22:27        350 1.txt

> $file | get-member

   TypeName: Deserialized.System.IO.FileInfo

Name              MemberType   Definition
----              ----------   ----------
MshChildName      NoteProperty System.String MshChildName=1.txt
MshDrive          NoteProperty System.Management.Automation.MshObject MshDri...
MshIsContainer    NoteProperty System.Boolean MshIsContainer=False
MshParentPath     NoteProperty System.String MshParentPath=Microsoft.Managem...
MshPath           NoteProperty System.String MshPath=Microsoft.Management.Au...
MshProvider       NoteProperty System.Management.Automation.MshObject MshPro...
Attributes        Property     System.String {get;set;}
CreationTime      Property     System.DateTime {get;set;}
CreationTimeUtc   Property     System.DateTime {get;set;}
Directory         Property     System.String {get;set;}
DirectoryName     Property     System.String {get;set;}
Exists            Property     System.Boolean {get;set;}
Extension         Property     System.String {get;set;}
FullName          Property     System.String {get;set;}
IsReadOnly        Property     System.Boolean {get;set;}
LastAccessTime    Property     System.DateTime {get;set;}
LastAccessTimeUtc Property     System.DateTime {get;set;}
LastWriteTime     Property     System.DateTime {get;set;}
LastWriteTimeUtc  Property     System.DateTime {get;set;}
Length            Property     System.Int64 {get;set;}
Name              Property     System.String {get;set;}

> $file | get-mshobject

MshObject:
        @{MshPath=Microsoft.Management.Automation.Core\FileSystem::D:\msh\1.txt;
 MshPa
rentPath=Microsoft.Management.Automation.Core\FileSystem::D:\msh; MshChildName=
1.txt; MshDrive=; MshProvider=; MshIsContainer=False; Name=1.txt; Length=350; D
irectoryName=D:\msh; Directory=D:\msh; IsReadOnly=False; Exists=True; FullName=
D:\msh\1.txt; Extension=.txt; CreationTime=2006-4-3 22:27:16; CreationTimeUtc=2
006-4-4 2:27:16; LastAccessTime=2006-4-3 22:27:16; LastAccessTimeUtc=2006-4-4 2
:27:16; LastWriteTime=2006-4-3 22:27:16; LastWriteTimeUtc=2006-4-4 2:27:16; Att
ributes=Archive}
BaseObject:
         (System.Management.Automation.MshCustomObject)
ImmediateBaseObject:
         (System.Management.Automation.MshCustomObject)
TypeNames:
        Deserialized.System.IO.FileInfo
        Deserialized.System.IO.FileSystemInfo
        Deserialized.System.MarshalByRefObject
        Deserialized.System.Object
=============================================================

If you look carefully, you will notice:
1. export-clixml write MshObject.Properties into a xml file and import-clixml reconstruct a MshObject.
2. Although they looks the same, something changed  after serializtion and deserializtion:
Before: A FileInfo object is wrapped within MshObject
After:    A MshCustomObject is wrapped within MshObject. All Methods are lost.
3. Deserialized MshCustomObject is a Hash table.


Have Fun.
[Edit: Monad has now been renamed to Windows PowerShell. This script or discussion may require slight adjustments before it applies directly to newer builds.]

Tags:       


Comments:
Is your own cmdletget-mshobject?
 
Yes, You have to build it by yourself
 

Post a Comment





<< Home