簡體   English   中英

PowerShell cmdlet 顯示屬性,但無法通過“選擇”顯示它

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

我正在嘗試執行以下語句。

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

所以我查看了這個 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-webpplication 怎么能顯示 name 屬性,但我們不能 select 呢?

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"}

這對我有用:

get-webapplication | forEach { write-host $_.Attributes[0].Value }

資料來源: https://stackoverflow.com/a/11357353/1158313

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM