Author Posts

May 3, 2018 at 8:28 pm

Hello..... the sccript below is working, however I woull like to add all the information from each object in variable in order to organize the results with "| select " command. Could please somebody help me out?

___________________________________________________________________
cls
$DomainName = 'LDAP://CN=Computers,DC=london,DC=com'
$Root = New-Object DirectoryServices.DirectoryEntry $DomainName
$objSearcher = New-Object DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $Root
$objSearcher.SearchScope = "SubTree"

$colResults = $objSearcher.FindAll()
foreach ($objResult in $colResults)
{

$computer = $objResult.GetDirectoryEntry()
$computer.name
$computer.ObjectSecurity.Owner

}

May 3, 2018 at 10:11 pm

There are a couple of ways to accomplish this. The easiest would be to create calculated properties of $computer. Calculated properties are just one-off hashtables you can use as custom or replacement properties.

$computer = $objResult.GetDirectoryEntry()
$computer | Select-Object -Property name,@{Name='Owner';Expression={$_.ObjectSecurity.Owner}}

In that example, you actually would not need to create the $computer variable. You could pipe $objResult.GetDirectoryEntry() directly to Select-Object.

May 4, 2018 at 1:12 pm

Is there a reason you are not using the ActiveDirectory module installed with RSAT tools? The code is as simple as:

Import-Module ActiveDirectory

$computers = Get-ADComputer -Filter * -Properties ManagedBy

May 4, 2018 at 1:59 pm

Also, you can't pipe from statements like "foreach($i in $list)" in powershell. That's one of the big gotchas.

May 5, 2018 at 9:53 pm

Very appreciate your help.. so I am very beginner with powershel...How can I get the pthers proprieties that I could get from the computer? Do you advice any how to with firts steps?

Thanks so much.