crcsupport
asked on
powershell, identifying object type using get-member
I'm trying to get a plain event log name list from a computer. Get-Eventlog returns list of objects which may make slow to get list of multiple computers. So I want to make sure the PS code requests only list of eventlog names of the target computer. So, I tried this;
Get-EventLog -ComputerName SRV1 -List
This returned the list of event log names correctly. Then, I ran piping. It shows it's Eventlog object type!;
Get-EventLog -ComputerName SRV1 -List | Get-Member
output:
Then, I see 'LogDisplayName' and I piped again to make sure I get only string of event log names. Output looks fine. ;
Get-EventLog -ComputerName localhost -list | select logdisplayname
Then, I checked it's object type with Get-Member again and it's still EventLog object type!!!;
Get-EventLog -ComputerName localhost -list | select logdisplayname
OUtput:
Then, I see LogDisplayName as note property of string type as one of its member. So I ran piping again and still an object with PSCustomObject type!!!:
Get-EventLog -ComputerName localhost -list | select logdisplayname | select logdisplayname | gm
Where is plain string of list of event log?
Get-EventLog -ComputerName SRV1 -List
This returned the list of event log names correctly. Then, I ran piping. It shows it's Eventlog object type!;
Get-EventLog -ComputerName SRV1 -List | Get-Member
output:
TypeName: System.Diagnostics.EventLog
Name MemberType Definition
---- ---------- ----------
Disposed Event System.EventHandler Disposed(System.Object, System.EventArgs)
EntryWritten Event System.Diagnostics.EntryWrittenEventHandler EntryWritten(System.Object, System.Diagnostics.EntryWrittenEventArgs)
BeginInit Method void BeginInit(), void ISupportInitialize.BeginInit()
Clear Method void Clear()
Close Method void Close()
CreateObjRef Method System.Runtime.Remoting.ObjRef CreateObjRef(type requestedType)
Dispose Method void Dispose(), void IDisposable.Dispose()
EndInit Method void EndInit(), void ISupportInitialize.EndInit()
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetLifetimeService Method System.Object GetLifetimeService()
GetType Method type GetType()
InitializeLifetimeService Method System.Object InitializeLifetimeService()
ModifyOverflowPolicy Method void ModifyOverflowPolicy(System.Diagnostics.OverflowAction action, int retentionDays)
RegisterDisplayName Method void RegisterDisplayName(string resourceFile, long resourceId)
ToString Method string ToString()
WriteEntry Method void WriteEntry(string message), void WriteEntry(string message, System.Diagnostics.EventLogEntryType type), void...
WriteEvent Method void WriteEvent(System.Diagnostics.EventInstance instance, Params System.Object[] values), void WriteEvent(System...
Container Property System.ComponentModel.IContainer Container {get;}
EnableRaisingEvents Property bool EnableRaisingEvents {get;set;}
Entries Property System.Diagnostics.EventLogEntryCollection Entries {get;}
Log Property string Log {get;set;}
LogDisplayName Property string LogDisplayName {get;}
MachineName Property string MachineName {get;set;}
MaximumKilobytes Property long MaximumKilobytes {get;set;}
MinimumRetentionDays Property int MinimumRetentionDays {get;}
OverflowAction Property System.Diagnostics.OverflowAction OverflowAction {get;}
Site Property System.ComponentModel.ISite Site {get;set;}
Source Property string Source {get;set;}
SynchronizingObject Property System.ComponentModel.ISynchronizeInvoke SynchronizingObject {get;set;}
Then, I see 'LogDisplayName' and I piped again to make sure I get only string of event log names. Output looks fine. ;
Get-EventLog -ComputerName localhost -list | select logdisplayname
Then, I checked it's object type with Get-Member again and it's still EventLog object type!!!;
Get-EventLog -ComputerName localhost -list | select logdisplayname
OUtput:
TypeName: Selected.System.Diagnostics.EventLog
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
LogDisplayName NoteProperty System.String LogDisplayName=Active Directory Web Services
Then, I see LogDisplayName as note property of string type as one of its member. So I ran piping again and still an object with PSCustomObject type!!!:
Get-EventLog -ComputerName localhost -list | select logdisplayname | select logdisplayname | gm
TypeName: Selected.System.Management.Automation.PSCustomObject
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
LogDisplayName NoteProperty System.String LogDisplayName=Active Directory Web Services
Where is plain string of list of event log?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I found this article regarding using expandproperty against single string property. http://blogs.msdn.com/b/powershell/archive/2009/09/14/select-expandproperty-propertyname.aspx?CommentPosted=true#commentmessage
I'm still confused why the author had to use 'select -expandproperty name' to get the value of property of each pipelined object. Does this actually reduce the processing load going through all pipelined object, just looking into the property's value of object? I guess to get the value, the pipelined objects still have to be gone through to point the property name and value. What is the advantage of using 'select -expandproperty propertyname' instead of 'select propertyname' against a property which is not an object other than string?
I'm still confused why the author had to use 'select -expandproperty name' to get the value of property of each pipelined object. Does this actually reduce the processing load going through all pipelined object, just looking into the property's value of object? I guess to get the value, the pipelined objects still have to be gone through to point the property name and value. What is the advantage of using 'select -expandproperty propertyname' instead of 'select propertyname' against a property which is not an object other than string?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Get-EventLog -ComputerName localhost -list | select -Expand logdisplayname:
this returns "System.String"
Get-EventLog -ComputerName localhost -list | select logdisplayname:
this returns Selected.System.Diagnostic
And Get-EventLog -ComputerName localhost -list | Get-Member shows that 'LogDisplayName' is 'Property' and string {get;}, which means it's string, but why do I have to expland the property when it's not propertyset and it's string??