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

Afr*_*roz 15 powershell

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

dir IIS:\Sites| foreach{ get-webapplication -site $_.Name} | select -first 1
Run Code Online (Sandbox Code Playgroud)

这导致了

Name             Application pool   Protocols    Physical Path
----             ----------------   ---------    -------------
i1               DefaultWebSite     http         C:\inetpub\hosts\DefaultWebSite\i1
Run Code Online (Sandbox Code Playgroud)

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

dir IIS:\Sites| foreach{ get-webapplication -site $_.Name} | select -first 1 name
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

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

man*_*lds 22

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>
...
Run Code Online (Sandbox Code Playgroud)

因此,名称实际上来自$_.Path.Trim('/'),所以你可以做同样的事情,如果你想:

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