Welcome to Sign in | Join | Help
in
Home Blog Forums

The Lazy Admin

PowerShell: Hello Object

Sponsor


Our First Stop on the road to PowerShell “gurudom” is to learn about the foundation of Powershell.
One of the key elements of PowerShell that makes it so Powerfull is the way you can interact with objects using the command line. An object is an entity that can describes itself, and what it does. The object contains data structures or Properties, and methods to read, write and manipulate those properties.

An analogy to better understand an object would be to think of a washing machine. Imagine for a second that you don’t know how to use a washing machine and it’s the first time you have ever seen one. Written on the lid of the washing machine are instructions on how to use the washing machine’s functions. E.g: Lifting the lid gives you access to the clothes basin. Turning the dial to normal clean cycle causes the washing machine to start performing the built in function of washing clothes. We can equate the instructions on the lid as the object being able to list the variables and methods it is capable of operating on and with. Turning the dial to start the machine after loading clothes into the basin can be equated to loading an object with data, then calling a method to operate on that data. So now you can think of PowerShell as a way to operate all your cool new objects, only PowerShell has the added advantage of not needing quarters! Now that we have the basic understanding of what an object is, let’s see how we can interact with the more common object that we find in PowerShell: A common task that administrators have to do is read and change file permissions. At the PowerShell command line type: PS C:\> Get-Acl c:\testFolder You will receive an output like this:

This command seems obvious enough. However we can get more information about the Get-Acl command by querying it.

PS C:\> Get-Acl | Get-Member

Now this list may look daunting, but remember we will only need to use this list when we need something specific. The default Method for the Get-Acl object is to output permissions for the directory we ran it against. To continue are example: I want to find out just one piece of information, who is the owner of the directory, rather than calling the default method, I ask the object to display just one piece of data:

PS C:\> (get-acl c:\).Owner

 

This will output only the owner of the folder. Notice the brackets around the command, this is letting the PowerShell interpreter know that we want to list a specific property from the object we are querying. Getting familiar with querying objects is the first step to being able to take full advantage of PowerShell. Just remember if you want to pull values from the objects you must give the object context. That means if you don't make reference to a folder in the ACL command the object won't have a structure to load and output values to the command line. Take a look at the other commands PowerShell has built in. In the next post I'll talk about Get and Set operators and how they relate with objects.





Published Thursday, November 23, 2006 10:23 PM by daniel.nerenberg
Filed under:

Comments

No Comments
Anonymous comments are disabled

About daniel.nerenberg

I am an MCT, Consultant based out of Montreal Quebec Canada. As the "new" Lazy Admin on the block I am working to make TheLazyAdmin.com the best website for MS Software tips and tricks out there!


All postings are provided "AS IS" with no warranties, and confer no rights.
Microsoft product screen shot(s) reprinted with permission from Microsoft Corporation.