2011-06-08 87 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