PowerShell cmdlet 显示属性,但无法通过“选择"显示它 [英] PowerShell cmdlet shows property, but it can't display it through 'select'
问题描述
我正在尝试执行以下语句.
I am trying to execute the following statement.
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
但是当我执行下面的结果是空的
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
所以没有名称"属性.get-webapplication 可以显示name属性,但是我们不能选择它是怎么回事?
So no 'Name' property. How is it that the get-webpplication can show the name property, but we cant select it?
推荐答案
WebAdministration 模块定义相关类型的默认格式.在这种情况下,您获得的 WebApplication 类型为 Microsoft.IIs.PowerShell.Framework.ConfigurationElement#site#application
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
如果查看模块下的文件iisprovider.format.ps1xml
(通常位于C:\Windows\System32\WindowsPowerShell\v1.0\Modules\WebAdministration
),你会看到为该类型的Name指定的格式如下:
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>
...
因此该名称实际上是从 $_.Path.Trim('/')
获得的,因此您可以根据需要执行相同的操作:
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"}
这篇关于PowerShell cmdlet 显示属性,但无法通过“选择"显示它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!