【问题标题】:PowerShell cmdlet shows property, but it can't display it through 'select'PowerShell cmdlet 显示属性,但无法通过“选择”显示
【发布时间】:2014-03-09 19:54:57
【问题描述】:

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

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-webappplication可以显示name属性,但是我们不能选择呢?

【问题讨论】:

标签: powershell


【解决方案1】:

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

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

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

因此该名称实际上来自$_.Path.Trim('/'),因此您可以根据需要执行相同操作:

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

【讨论】:

  • 不要讨厌 PowerShell。讨厌这个特定的 PowerShell 模块的作者。如果他们暴露了一个名为 Name 的 NoteProperty,那么这个问题甚至不会被提出。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-15
  • 2017-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多