Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell cmdlet shows property, but it can't display it through 'select'

Tags:

powershell

I am trying to execute the following statement.

dir IIS:\Sites| foreach{ get-webapplication -site $_.Name} | select -first 1

This results in

Name             Application pool   Protocols    Physical Path
----             ----------------   ---------    -------------
i1               DefaultWebSite     http         C:\inetpub\hosts\DefaultWebSite\i1

But when I execute the following the result is empty

dir IIS:\Sites| foreach{ get-webapplication -site $_.Name} | select -first 1 name

So I looked into the properties for this object

dir IIS:\Sites| foreach{ get-webapplication -site $_.Name} | select -first 1 | get-member | sort
Name | select Name, MemberType | format-table -auto

Name                                MemberType
----                                ----------
applicationPool                   NoteProperty
Attributes                            Property
ChildElements                         Property
ClearLocalData                          Method
Collection                        NoteProperty
ConfigurationPathType             NoteProperty
Copy                                    Method
Delete                                  Method
ElementTagName                        Property
enabledProtocols                  NoteProperty
Equals                                  Method
GetAttribute                            Method
GetAttributeValue                       Method
GetChildElement                         Method
GetCollection                           Method
GetHashCode                             Method
GetMetadata                             Method
GetParentElement                        Method
GetType                                 Method
Item                     ParameterizedProperty
ItemXPath                         NoteProperty
LoadProperties                          Method
Location                          NoteProperty
Methods                               Property
path                              NoteProperty
PhysicalPath                    ScriptProperty
PSPath                            NoteProperty
Schema                                Property
SetAttributeValue                       Method
SetMetadata                             Method
ToPSObject                              Method
ToString                                Method
Update                                  Method
UpdateCollection                        Method
virtualDirectoryDefaults          NoteProperty

So no 'Name' property. How is it that the get-webpplication can show the name property, but we cant select it?

like image 272
Afroz Avatar asked Dec 20 '11 04:12

Afroz


1 Answers

The WebAdministration module defines default format for the concerned type. In this case, the WebApplication that you get is of type Microsoft.IIs.PowerShell.Framework.ConfigurationElement#site#application

If you look at the file iisprovider.format.ps1xml under the module ( usually located at C:\Windows\System32\WindowsPowerShell\v1.0\Modules\WebAdministration), you will see that the format specified for the Name of this type is as below:

...
<TableColumnItem>
   <ScriptBlock>
        $name = $_.Path.Trim('/')
        $name
   </ScriptBlock>
</TableColumnItem>
...

Thus the name is actually got from $_.Path.Trim('/'), so you can do the same if you want:

get-webapplication -site "test" | select @{e={$_.Path.Trim('/')};l="Name"}
like image 70
manojlds Avatar answered Oct 18 '22 09:10

manojlds