2011-06-08 38 views
3

我有一些代碼通過ADSI得到IIS6站點的列表:轉換ADSI對象Powershell的對象

([adsi]"IIS://localhost/W3SVC").psbase.children | select servercomment, serverstate | Where-Object {$_.serverstate -ne $null} 

servercomment            serverstate 
-------------            ----------- 
{Default Web Site}           {4} 
{SharePoint Web Services}         {4} 
{SharePoint Central Administration v4}      {4} 
{SharePoint - 80}           {4} 

當我用管道通過的ConvertTo的cmdlet或亂串,或者使用的toString通過對象循環()我得到這樣的

#TYPE Selected.System.DirectoryServices.DirectoryEntry 
"servercomment","serverstate" 
"System.DirectoryServices.PropertyValueCollection","System.DirectoryServices.PropertyValueCollection" 
"System.DirectoryServices.PropertyValueCollection","System.DirectoryServices.PropertyValueCollection" 
"System.DirectoryServices.PropertyValueCollection","System.DirectoryServices.PropertyValueCollection" 
"System.DirectoryServices.PropertyValueCollection","System.DirectoryServices.PropertyValueCollection" 

基本上,我只需要像PowerShell來進行治療的站點列表(servercomment)對象,所以我可以通過各種方式導出。但從我的理解來看,這些本身就是集合,並且具有更多屬性,但是當我更深入時,我沒有看到任何可以提取爲IIS站點名稱的東西。通過WMI獲取此信息更容易嗎?還是必須創建一個新的Powershell對象來包含這些信息?

回答

4

這將爲您提供一個帶有這兩個子項的自定義psobjects數組作爲noteproperties以及字符串值。

$x = ([adsi]"IIS://localhost/W3SVC").psbase.children | 
    select @{l="ServerComment";e={[string]$_.servercomment}}, 
    @{l="ServerState";e={[string]$_.Serverstate}} | 
    where {$_.serverstate} 
$x.count 
2 
$x[0] 

ServerComment            ServerState 
-------------            ----------- 
Default Web Site           2 


$x[0] | gm 


    TypeName: Selected.System.DirectoryServices.DirectoryEntry 

Name   MemberType Definition 
----   ---------- ---------- 
Equals  Method  bool Equals(System.Object obj) 
GetHashCode Method  int GetHashCode() 
GetType  Method  type GetType() 
ToString  Method  string ToString() 
ServerComment NoteProperty System.String ServerComment=Default Web Site 
ServerState NoteProperty System.String ServerState=2