header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
Search AD for certain servers
Last Post 09 Dec 2009 07:24 AM by mks. 2 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
mksUser is Offline
New Member
New Member
Posts:2
Avatar

--
08 Dec 2009 12:32 PM
    I new to powershell and trying to learn.  I have a script that is not working right.  I want to be able to search the entire AD domain for servers that start with MC.  I have an OU called servers and under that OU are other containers and they all have servers in them.  Some of the servers start with MC and I want to search for all of them.  The script below only seems to show me a few servers but not all of them.  Is there a problem with the script?

    $dom = [System.DirectoryServices.ActiveDirectory.domain]::GetCurrentdomain()
    $root = [ADSI] "GC://$($dom.Name)"
    $searcher = New-Object System.DirectoryServices.DirectorySearcher($root, "(objectCategory=computer)")

    $searcher.FindAll() | where { $($_.Properties.cn).StartsWith('mc') } | % { $_.Properties.name }

    Thanks for the help.
    Karl MitschkeUser is Offline
    Basic Member
    Basic Member
    Posts:451
    Avatar

    --
    08 Dec 2009 12:39 PM
    Hi;

    i'd do my search like this:

    $dom = [System.DirectoryServices.ActiveDirectory.domain]::GetCurrentdomain()
    $root = [ADSI] "GC://$($dom.Name)"
    $searcher = New-Object System.DirectoryServices.DirectorySearcher($root, "(&(objectCategory=computer)(name=wc*))")
    $searcher.FindAll()

    Karl
    http://unlockpowershell.wordpress.com
    Co-Author, Windows PowerShell 2.0 Bible
    -join("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"})
    mksUser is Offline
    New Member
    New Member
    Posts:2
    Avatar

    --
    09 Dec 2009 07:24 AM
    Thanks Karl.  Being new to scripting and PS it is a big help.  Now that is working I need to loop through the servers and delete some folders.  Would you have some code I could look at that does something like this?  Thanks for your help.
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer