Powershell : Using Get-Member to Explore Cmdlets


This article is in continuation of my previous article.

We know what cmdlet to use, but how to know that what properties and functions a cmdlet can access and perform, this is big question. Until you know the Attributes and Functions you can access through powerhshell, you can’t use them in your scripts.

Here is how to explore them –

I’ve the Get-Service cmdlet with me, which gives me the list of all Services running on my machine, I want to know what Actions or Functionalities I can exploit (~ Though exploit is a bit negative word here 😉 ) and Properties that are accessible with these Service objects.

I’ll simply feed the Get-Service output through a pipeline to a Get-Member cmdlet, which will give me the following output.

gm

Now into the above image please notice three things.

1. OBJECT TYPE  :  This denotes the cmdlet output passed through the pipeline to the Get-Member cmdlet is an object of type ServiceController

2. PROPERTIES  :  All objects have some properties, for example Status in above image. You can access any property using the Dot (‘.’) operator, like –

gm

3. METHODS : We can perform operations on objects using the methods associated to them, like we can use Start and Stop methods to Start\Stop state of a service. Please note that all methods must be called with a set of parenthesis at the end like $service.Start()  (Calling a method means Invoking or Using the Method)

gm

IMPORTANT :

People will wonder in the above example, after stopping the service, why I again executed the command –

$service = Get-Service wuauserv

Well, there is a very strong reason for that, If you remember on of my articles on Objects and Its Usage, that an object is an instance of a class. That means the $service variable has an old occurrence \ Instance or an Object of the service class when the service object was in Running status.

So, If We will check the Status of the $service variable just after the stopping the service, it still has old object where service was in running state.

gm

Hope you’ll find that Informative, Happy Reading 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s