5 Replies - 3229 Views - Last Post: 20 February 2009 - 12:07 PM Rate Topic: -----

#1 d4t4ch17d   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 31-January 08

File search in AD [Powershell]

Posted 19 February 2009 - 11:23 AM

Hi All,

I am stomped right now. I am trying to search computers in a specific OU that may have a set of files. Then send this list to a text file.
The problem occurs when the search is executed and the results are stored. They are stored in ADSPATH only, and since I have a variable holding the search results, it is only holding the ADSPATH.

I then call to that variable in a test-path but I get errors saying that the FDQN is too long. I know why, its because the whole ADSPATH is being stuck in the "\\RIGH-HERE\Folderpath\file". Of course it won't work :crazy:. What I need assistance with is how do I omit or only retrieve the "name" propertie of DirectorySearcher namespace?

I have done the code below, and I'm using the propertiesToLoad.Add("name"), but it feels like I am adding this to the results rather than sepcifying that only this be retrieved.

$objDomain = New-Object System.DirectoryServices.DirectoryEntry("LDAP://OU=Workstations,OU=Test,dc=xxx,dc=net")

$searcher = New-Object System.DirectoryServices.DirectorySearcher

$searcher.SearchRoot = $objDomain

$searcher.Filter = "(objectCategory=computer)"

$searcher.PageSize=1000

$searcher.SearchScope = "Subtree"

$searcher.propertiesToLoad.Add("name")

$computers = $searcher.findall()

$files = @("3fcv.ra", "ceg.sdr", "def.help", "fe.sp", "tfdse.xl")

$computers | foreach {

$TestPath = test-path "\\$computers\c$\Windows\system32\$files"

if ($TestPath -match "True") {out-file -filepath \\server\folder\virusPClist\list.txt}

}



If I comment out the $files array, and the $TestPath & if statement, and execute the $computers variable it lists only the ADSPATH in the results...Any help or guidance is very much appreciated. Thanks!! :ph34r:

This post has been edited by d4t4ch17d: 19 February 2009 - 11:24 AM


Is This A Good Question/Topic? 0
  • +

Replies To: File search in AD [Powershell]

#2 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7485
  • View blog
  • Posts: 15,514
  • Joined: 16-October 07

Re: File search in AD [Powershell]

Posted 19 February 2009 - 12:43 PM

Take it slow and steady. You can break down each step on it's own.

Let's take a walk through AD:
PS:\> $searcher = New-Object System.DirectoryServices.DirectorySearcher
PS:\> $searcher.SearchRoot = [ADSI]""
PS:\> $searcher.Filter = "(objectCategory=computer)"
PS:\> $computer = $searcher.findOne()
PS:\> $computer

Path														Properties
----														----------
LDAP://CN=DCA1,OU=Domain Controllers,DC=pvsc,DC=com		 {frscomputerreferencebl, operatingsystem, countrycode, c...

# Look, properties.  Let's ask:

PS:\> $computer.Properties

Name						   Value
----						   -----
frscomputerreferencebl		 {CN=DCA1,CN=Domain System Volume (SYSVOL share),CN=File Replication Service,CN=Sys...
operatingsystem				{Windows Server 2003}
countrycode					{0}
cn							 {DCA1}
...

# cn looks good.  Can we get it on it's own?

PS:\> $computer.Properties.cn
DCA1

# cool, what is it?
PS:\> $computer.Properties.cn.GetType()

IsPublic IsSerial Name									 BaseType
-------- -------- ----									 --------
True	 False	ResultPropertyValueCollection			System.Collections.ReadOnlyCollectionBase

# ick, can we do better?

PS:\> $computer.Properties.cn[0]
PV-DCA1

PS:\> $computer.Properties.cn[0].GetType()

IsPublic IsSerial Name									 BaseType
-------- -------- ----									 --------
True	 True	 String								   System.Object

#perfect



So, a list of computer names would look like:
$computers = $searcher.findAll() | %{ $_.properties.cn[0] }




This won't work:
$files = @("3fcv.ra", "ceg.sdr", "def.help", "fe.sp", "tfdse.xl")
$TestPath = test-path "\\$computers\c$\Windows\system32\$files"



Don't get hung up on pipes when procedural stuff will make life easier. Here's a snippet of code. With this and the prior stuff, you should be able to figure it out.

$computers = @("Larry", "Moe", "Curley")
$files = @("Knuckle", "Sandwich")

foreach($computer in $computers) {
	foreach($file in $files) {
		$path = "\\$computer\c$\Windows\system32\$file"
		Write-Host $path
	}
}


Was This Post Helpful? 1
  • +
  • -

#3 d4t4ch17d   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 31-January 08

Re: File search in AD [Powershell]

Posted 19 February 2009 - 03:11 PM

THANKS!! Alright, I got the collection to only list the computer name now. The next step, I know I am trying to get the computer names that do have the fie to be exported to a csv file.

Here is my revised code. Oh by the way, that information about the $computers.properties helped me plenty. The properties.cn did return correctly, but the properties.name did exactly what I am wanting. The script is working great it is returning a "Fils not found." if it doesnt exist. I have placed this file on one system out of 20, and that one system gives an error when I am tryingto export its name to a csv file...Any suggestions?

$searcher = New-Object System.DirectoryServices.DirectorySearcher

$searcher.SearchRoot = [ADSI]"LDAP://OU=Workstations, OU=test dc=fake, dc=net"

$searcher.Filter = "(objectCategory=computer)"

$searcher.PageSize=1000

$searcher.SearchScope = "Subtree"

$searcher.propertiesToLoad.Add("name")

$computers = $searcher.findall() | %{$_.properties.name}


foreach ($i in $computers){

	$path = test-path "\\$i\c$\Windows\system32\test.sp"
	if ($path -match $true){$_ | export-csv c:\users\me\desktop\list.csv}
	else {"Files not found." | out-host}
}


Was This Post Helpful? 0
  • +
  • -

#4 d4t4ch17d   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 31-January 08

Re: File search in AD [Powershell]

Posted 20 February 2009 - 08:59 AM

I just wanted to let baavgai I fixed my issue. Here is the code below;

$searcher = New-Object System.DirectoryServices.DirectorySearcher

$searcher.SearchRoot = [ADSI]"LDAP://OU=Workstations, OU=removed, dc=removed, dc=net"

$searcher.Filter = "(objectCategory=computer)"

$searcher.PageSize=1000

$searcher.SearchScope = "Subtree"

$searcher.propertiesToLoad.Add("name")

$computers = $searcher.findall() | %{$_.properties.name}

$file = @("3fcv.ra", "ceg.sdr", "def.help", "fe.sp", "tfdse.xl")

foreach ($i in $computers){
	foreach ($f in $file){

	$path = test-path "\\$i\c$\Windows\system32\$file"
	if ($path -match $true){Write-Output $i |Out-FIle -filepath "C:\users\removed\desktop\list.txt" -append}
	else {"$i Files not found." | out-host}
	}
}


Was This Post Helpful? 0
  • +
  • -

#5 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7485
  • View blog
  • Posts: 15,514
  • Joined: 16-October 07

Re: File search in AD [Powershell]

Posted 20 February 2009 - 10:12 AM

Looks good. Glad you got it. :^:
Was This Post Helpful? 0
  • +
  • -

#6 d4t4ch17d   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 31-January 08

Re: File search in AD [Powershell]

Posted 20 February 2009 - 12:07 PM

View Postbaavgai, on 20 Feb, 2009 - 09:12 AM, said:

Looks good. Glad you got it. :^:


Thanks so much for your help!! :P
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1