overbyte
overbyte

Reputation: 77

Convert ADSI objects to Powershell objects

I have some code to get a list of IIS6 sites through ADSI:

([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}

When I pipe it through the convertto cmdlets or out-string or looping through objects using tostring() I get something like this

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

Basically I just need the list of sites (servercomment) to be treated like Powershell objects so I can export them through various means. But from my understanding these are collections in themselves and do have more properties, but when I go deeper I don't see anything that can be extracted as the name of an IIS site. Is getting this info through WMI easier or do I have to create a new Powershell object to contain these?

Upvotes: 3

Views: 3207

Answers (1)

mjolinor
mjolinor

Reputation: 68273

This will give you an array of custom psobjects with those two child items as noteproperties, along with the string values.

 $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

Upvotes: 4

Related Questions