PowerShell cmdlet显示属性,但无法通过“select”显示它

时间:2011-12-20 04:28:44

标签: powershell

我正在尝试执行以下语句。

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

这导致

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

但是当我执行以下操作时,结果为空

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

所以我查看了这个对象的属性

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

所以没有'姓名'属性。 get-webpplication如何显示name属性,但我们无法选择它?

1 个答案:

答案 0 :(得分:19)

WebAdministration模块定义相关类型的默认格式。在这种情况下,您获得的WebApplication类型为Microsoft.IIs.PowerShell.Framework.ConfigurationElement#site#application

如果您查看模块下的文件iisprovider.format.ps1xml(通常位于C:\Windows\System32\WindowsPowerShell\v1.0\Modules\WebAdministration),您会看到为此类型的名称指定的格式如下:

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

因此该名称实际上来自$_.Path.Trim('/'),因此如果您愿意,您也可以这样做:

get-webapplication -site "test" | select @{e={$_.Path.Trim('/')};l="Name"}
相关问题