PowerShell v3 in a Year Day 11 Get Item

Topic: Get-Item
Alias: gi

Note: I had already touched on the Get-Item cmdlet, but, felt there were a few items I had left out, so, I revisited it to complete the task. The original post is http://learningpcs.blogspot.com/2012/09/powershell-v3-in-year-day-5-get-item.html.

As touched on briefly in my post about core commands PowerShell providers are a major part of the technology. To help deal with data in various stores there are a variety of cmdlets you need to become proficient with in order to truly master PowerShell. One of those cmdlets is Get-Item. At first glance, the help does not indicate just how useful this cmdlet is. In fact, it wasnt until I was working with a co-worker that I saw a glimpse of the real usefulness of this cmdlet. As noted in the help, "Get-Item cmdlet gets the item at the specified location." The help goes on to specify that Get-Item only gets the container object, not the content. So, if we have a file (in a file system provider) Get-Item returns a FileInfo object, not, the content of the file.

For example, the following command demonstrates a simple usage of Get-Item with the C:WindowsSystem32cmd.exe file:
get-item C:WindowsSystem32cmd.exe
The result of this command is:
     Directory: C:WindowsSystem32


Mode                LastWriteTime     LengthName                                                                                                                                 
----                -------------     ----------                                                                                                                                
-a---        11/20/2010   9:23 PM     345088 cmd.exe 
To be more explicit, and, get feedback on the object type, lets tack on a pipelined call to Get-Member. For the sake of brevity I have truncated the output a good bit:
    TypeName: System.IO.FileInfo

Name                      MemberType     Definition
----                      ----------     ----------
Mode                      CodeProperty   System.StringMode{get=Mode;}
AppendText                Method         System.IO.StreamWriterAppendText()
CopyTo                    Method         System.IO.FileInfoCopyTo(string destFileName),System.IO.FileInfo CopyTo(string destFileName,bool overwrite)
Create                    Method         System.IO.FileStreamCreate()
CreateObjRef              Method         System.Runtime.Remoting.ObjRefCreateObjRef(typerequestedType) 
As indicated in the help, we do not get file content, but, rather, a FileInfo object. Using Get-Item is a way to reference object containers not contents unless you wildcard the cmdlet.

The one noteworthy exception I ran across has to do with IIS. There are undoubtedly many other uses (that is, providers) to provide similar examples of how using a wild card will return a collection of properties. In this case, I am looking at an instance of 2012 Enterprises IIS default app pool. After importing the module I ran this command to see what all was on the box:
Get-Item IIS:AppPools*

Name                     State        Applications
----                     -----        ------------
.NET v2.0                Started
.NET v2.0Classic        Started
.NET v4.5                Started
.NET v4.5Classic        Started
Classic .NETAppPool     Started
DefaultAppPool           Started      DefaultWeb Site
Next, I zeroed in on a specific app pool (.NET v 2.0) and stashed it in a variable $2.
$2 = Get-Item IIS:AppPools.NET v2.0
When I output the objects contents, I saw this:
$2

Name                     State        Applications
----                     -----        ------------
.NET v2.0                Started
To get a better picture, I ran this command:
$2 | select *


name                        : .NET v2.0
queueLength                 :1000
autoStart                   :True
enable32BitAppOnWin64       : False
managedRuntimeVersion       : v2.0
managedRuntimeLoader        :webengine4.dll
enableConfigurationOverride : True
managedPipelineMode         :Integrated
CLRConfigFile               :
passAnonymousToken          :True
startMode                   :OnDemand
state                       :Started
applicationPoolSid         

Related Posts by Categories

0 comments:

Post a Comment