ForEach-Object not getting items, $_ is the collection


first run line:

([adsi]'winnt://mycomputer').psbase.properties

cool, returns:

propertyname                                      value                                                                                    capacity                                            count
------------                                      -----                                                                                    --------                                            -----
operatingsystem                                   windows nt                                                                                      4                                                1
operatingsystemversion                            6.1                                                                                             4                                                1
owner                                             [redacted]                                                                                           4                                                1
division                                          microsoft                                                                                       4                                                1
processorcount                                    multiprocessor free                                                                             4                                                1
processor                                         intel64 family 6 model 42 stepping 7                                                            4                                                1
name                                              [redacted]                                                                                            4                                                1

now want objects...

([adsi]'winnt://mycomputer').psbase.properties | % {$_.value}

the above line returns nothing because...

([adsi]'winnt://mycomputer').psbase.properties | % {$_.gettype()}

tells me $_ system.directoryservices.propertycollection... what? why not directoryentryproperty object or something? why collection itself? bug? powershell can't handle collection implements non-generic idictionary, icollection and ienumerable? mean $_ should item, not collection through i'm enumerating, right?

i'm loosing mind because of this. please help.

this code "unpack" properties, , populate array of psobjects, can manipulated:

$results = @() $props = ([adsi]'winnt://grant-pc').psbase.properties $propnames = $props.propertynames foreach ($prop in $propnames) {     $results += new-object psobject -property @{'property'=$prop;'value'=($props.item($prop) | out-string).trim()}     } $results | select property, value


grant ward, a.k.a. bigteddy



Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

Error: 0x80073701 when trying to add Print Services Role in Windows 2012 Standard

Disconnecting from a Windows Server 2012 R2 file sharing session on a Windows 7,8,10 machine

Windows 2016 RDS event 1306 Connection Broker Client failed to redirect the user... Error: NULL