Get-Member. As you may or may not know (probably not if you are reading this blog), Powershell is an object-oriented language. In this sense, we work with cmdlets, which contains objects, wether it be a method, or a property, or a set of properties. Since here on Powershell Pancakes I only believe in the lightest of rubbish and nonsense, I have decided to go over this very briefly, and how it should make your life immensely easier (theoretically).
Every cmdlet in Powershell has a vast group of members associated with it, a lot of these are standard across all objects, especially when it concerns strings. For the experienced, usually we are aware of how to compare strings and the various operators associated with doing such tasks. But for objects that contain useful stuff, such as Get-Process, any of the WMI classes, objects returned from Active Directory - how would you know, exactly what kind of properties and methods you can perform related to that object? Unless you are like a close friend of mine, who attempted to memorize a book of part numbers for ball bearings (he failed, by the way), you wouldn't possibly be able to recollect everything about every object. Seldom do we write scripts each and every day that touch on all of our knowledge of everything we know about Powershell.
So let's get down to it, then;
Pancake:
Two things to note - GM is an alias for Get-Member (who would have guessed?), and you need to pipe Get-Member after a cmdlet to retrieve it's properties and methods. Some cmdlets operate with a -Properties switch, which you will usually find on cmdlets from the VIM modules or from the Active Directory modules, as well as others for Exchange and what not. The ISE will help you out in determining what switches are available to you in each cmdlet. Also your trusty Clippy equivalent is Get-Help
For our Get-Process example above, it returned quite a large number of members. But how do we actually use these? The syntax below is incorrect;
With that, you are just going to retrieve what that member actually is, not what it contains. As you see, MainModule is a MemberType that is a Property. To utilize this, try the below;
Now we have just listed the executables associated with a selection of processes. Let's try another one, referring to our Get-Process | gm list of members.
Aha! Now we are getting somewhere, arn't we? It should be becoming apparant, that to select the Property of an object, you need to pipe | Select Property1,Property2,... and so on. As many properties as you want. In the above example, we found out the process name as listed in the services mmc.
Ah yes but wait, when I ran Get-Process | Get-Member, I saw a lot of other rubbish as well, like - a method?! Yes. A member does not mean it is a Property, a member also denotes Methods, which can be used to work with objects in that cmdlet. Let's look at the rather conspicious names of some of the methods associated with Get-Process. We have, Kill, Refresh, Start, Close, and a bunch of other ones I don't really know how to use. But you use them all in essentially the same way. Let's give you an example, using Kill.
Oh bugger, you just closed your Powershell window. But do you see? Using the Where {$_. } filter, we were able to select a process based on it's ProcessName. You can use any of the properties available to you to search for a process, and you can find (here it comes) all of those properties available to you by using Get-Member. Though you can use it in other ways too, you don't need to just kill things. Maybe I just want to get the process ID for anything with a process name of Powershell;
Hopefully this is rather apparant now, on how to better utilize different membertypes.
Every cmdlet in Powershell has a vast group of members associated with it, a lot of these are standard across all objects, especially when it concerns strings. For the experienced, usually we are aware of how to compare strings and the various operators associated with doing such tasks. But for objects that contain useful stuff, such as Get-Process, any of the WMI classes, objects returned from Active Directory - how would you know, exactly what kind of properties and methods you can perform related to that object? Unless you are like a close friend of mine, who attempted to memorize a book of part numbers for ball bearings (he failed, by the way), you wouldn't possibly be able to recollect everything about every object. Seldom do we write scripts each and every day that touch on all of our knowledge of everything we know about Powershell.
So let's get down to it, then;
Pancake:
Get-Process | gm
Two things to note - GM is an alias for Get-Member (who would have guessed?), and you need to pipe Get-Member after a cmdlet to retrieve it's properties and methods. Some cmdlets operate with a -Properties switch, which you will usually find on cmdlets from the VIM modules or from the Active Directory modules, as well as others for Exchange and what not. The ISE will help you out in determining what switches are available to you in each cmdlet. Also your trusty Clippy equivalent is Get-Help
For our Get-Process example above, it returned quite a large number of members. But how do we actually use these? The syntax below is incorrect;
Get-Process | Get-Member mainmodule
With that, you are just going to retrieve what that member actually is, not what it contains. As you see, MainModule is a MemberType that is a Property. To utilize this, try the below;
Get-Process | select MainModule
Now we have just listed the executables associated with a selection of processes. Let's try another one, referring to our Get-Process | gm list of members.
Get-Process | select processname,mainmodule
Aha! Now we are getting somewhere, arn't we? It should be becoming apparant, that to select the Property of an object, you need to pipe | Select Property1,Property2,... and so on. As many properties as you want. In the above example, we found out the process name as listed in the services mmc.
Ah yes but wait, when I ran Get-Process | Get-Member, I saw a lot of other rubbish as well, like - a method?! Yes. A member does not mean it is a Property, a member also denotes Methods, which can be used to work with objects in that cmdlet. Let's look at the rather conspicious names of some of the methods associated with Get-Process. We have, Kill, Refresh, Start, Close, and a bunch of other ones I don't really know how to use. But you use them all in essentially the same way. Let's give you an example, using Kill.
Get-Process | Where {$_.ProcessName -like "*powershell*"} | kill
Oh bugger, you just closed your Powershell window. But do you see? Using the Where {$_. } filter, we were able to select a process based on it's ProcessName. You can use any of the properties available to you to search for a process, and you can find (here it comes) all of those properties available to you by using Get-Member. Though you can use it in other ways too, you don't need to just kill things. Maybe I just want to get the process ID for anything with a process name of Powershell;
Get-Process | Where {$_.ProcessName -like "*Powershell*"} | Select ID
Hopefully this is rather apparant now, on how to better utilize different membertypes.
No comments:
Post a Comment