Solved

powershell, identifying object type using get-member

Posted on 2014-01-24
4
801 Views
Last Modified: 2014-01-27
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:
 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;}    

Open in new window


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

Open in new window


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

Open in new window


Where is plain string of list of event log?
0
Comment
Question by:crcsupport
  • 2
  • 2
4 Comments
 
LVL 40

Assisted Solution

by:Subsun
Subsun earned 500 total points
ID: 39807746
If you just want to list the logdisplayname values, then you can try by expanding the property... For example..

Get-EventLog -ComputerName localhost -list | select -Expand logdisplayname

Open in new window

0
 
LVL 1

Author Comment

by:crcsupport
ID: 39809301
I'm confused about the object type it returns by select and select -expandproperty.

Get-EventLog -ComputerName localhost -list | select -Expand logdisplayname:
this returns "System.String"

Get-EventLog -ComputerName localhost -list | select logdisplayname:
this returns Selected.System.Diagnostics.EventLog

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??
0
 
LVL 1

Author Comment

by:crcsupport
ID: 39809403
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?
0
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
ID: 39809608
I would say using expandproperty is depends on your requirement. In your case you want to get the property value to process hence we used Select -expandproperty..

You can also use he following code to achieve the same..
Get-EventLog -ComputerName localhost -list | %{$_.logdisplayname}

Open in new window


Regarding the object type : When you select properties, Select-Object returns new objects that have only the specified properties. 'Select-object logdisplayname' pass the same object type to pipeline, hence it displays System.Diagnostics.EventLog.

Select -expandproperty LogDisplayName gives you the Property value, and the object type of the value is string. Hence it displays System.String as object type..

following article probably explains it better..
http://blogs.msdn.com/b/vishinde/archive/2012/08/27/expandproperty-in-select-object.aspx
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Utilizing an array to gracefully append to a list of EmailAddresses
A brief introduction to what I consider to be the best editor for PowerShell.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question