Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How do I Pipe Value of PowerShell Array Value to Export-Csv?

Posted on 2009-05-01
5
Medium Priority
?
7,855 Views
Last Modified: 2012-05-06
I use the script posted in the code section of this post to return information from Active Directory.

What I would like to know is how I can pipe the results of the $colResults variable to a csv without it giving me the LDAP and properties of the System.DirectoryServices.DirectoryEntry object. What I'm after is a comma separated values machine list. The last 2 lines of code will give me exactly what I want, but it writes it to the host.

I have tried repositioning the pipe line for the Export-csv call, but it either prompts me for the input object for each machine, or else if I put it at the end, it returns an empty file with just a length header and a numeric value representing the length of the last object passed, which I assume to be a machine name.

Any idea what I'm doing wrong?
$strFilter = "(&(objectCategory=Computer)(Name=FBX*))"
 
$objDomain = New-Object System.DirectoryServices.DirectoryEntry
 
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.PageSize = 1000
$objSearcher.Filter = $strFilter
$objSearcher.SearchScope = "Subtree"
 
$colProplist = "name"
foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)}
 
$colResults = $objSearcher.FindAll()|Export-Csv -NoTypeInformation "C:\Documents and Settings\tlafferty\Desktop\comps.csv"
#foreach ($objResult in $colResults)
 #   {$objItem = $objResult.Properties; $objItem.name}
 cls;Write-Host "Finished..."

Open in new window

0
Comment
Question by:goneal
  • 2
  • 2
5 Comments
 
LVL 4

Accepted Solution

by:
peter_field earned 200 total points
ID: 24288165
That was harder than I expected.. try this:

$strFilter = "(&(objectCategory=Computer)(Name=FBX*))"
 
$objDomain = New-Object System.DirectoryServices.DirectoryEntry
 
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.PageSize = 1000
$objSearcher.Filter = $strFilter
$objSearcher.SearchScope = "Subtree"
 
$colProplist = "name"
foreach ($i in $colPropList){[void]$objSearcher.PropertiesToLoad.Add($i)}
 
$colResults = $objSearcher.FindAll()
 
$colResults | %{ $a = New-Object PSObject ; $a | Add-Member -MemberType NoteProperty -Name Name -Value $_.Properties.Item("Name")[0] ; $a } | Export-Csv -NoTypeInformation "C:\Documents and Settings\tlafferty\Desktop\comps.csv"

Open in new window

0
 
LVL 1

Author Closing Comment

by:goneal
ID: 31577099
This was right on the money first try! Thanks so much for your help.
0
 
LVL 1

Author Comment

by:goneal
ID: 24297074
Very useful! One thing that was not clear is what the [void] on line 12 accomplishes aside from the obvious (voiding...) or why it was needed. Could you explain? I'm a bit of a PowerShell novice.

Also, could you explain the last line of code? I understand that you are establishing a new variable $a as an object to contain the output of $colResults, but I'm not sure what else is happening. Would you mind a blow-by-blow?

In the meantime, thanks again. It was right on the first attempt.
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 24302556

PropertiesToLoad.Add returns a value, a count of properties. It's not very interesting, Voiding that is one way of dropping the value so it doesn't appear when the code runs. Otherways include redirecting the output to Null, or assigning it to a variable (say $Return) and ignoring it.

For the sake of discussion, I would take a few short cuts in the code above :)

Using $Null in place of the directory entry ($objDomain) will have it use the default value, the current domain.

The Filter can be passed in as a parameter when creating the Directory Searcher.

The SearchScope defaults to subtree so has been removed from the code (if you want to see that, just run "$DirSearcher" on its own with the example below, it'll show you the values set).

Using PropertiesToLoad.AddRange saves us a loop through each properties. AddRange doesn't return a value either so there's no need to Void it or deal with the returned value. It needs an array as the parameter, which is why $PropList is @("value1", "value2") now.

Using Select-Object with a custom value allows us to skip creation of a new object and separate addition of a property to that object. It also loses the other loop.

Finally, you may also consider grabbing these:

http://www.quest.com/powershell/activeroles-server.aspx

Because those will shorten your code to this:

Get-QADComputer -Name "FBX*" | Select-Object Name | Export-CSV "somefile.csv"

PowerShell is fun ;)

Chris
$LdapFilter = "(&(objectCategory=Computer)(name=FBX*))"
 
$DirSearcher = New-Object System.DirectoryServices.DirectorySearcher($Null, $LdapFilter)
$DirSearcher.PageSize = 1000
 
$PropList = @("name")
$DirSearcher.PropertiesToLoad.AddRange($PropList)
 
$DirSearcher.FindAll() | `
  Select-Object @{n='Name';e={ $_.Properties["name"] }} | `
  Export-Csv -NoTypeInformation "C:\Documents and Settings\tlafferty\Desktop\comps.csv"

Open in new window

0
 
LVL 4

Expert Comment

by:peter_field
ID: 24302919
Chris is correct on the use of void.

$a = New-Object PSObject
- this creates a new object, the idea is to create a rich object with properties, and this provides the base object for us to add properties to.

$a | Add-Member -MemberType NoteProperty -Name Name -Value $_.Properties.Item("Name")[0]
- this adds a property to $a called Name with the value of $_.Properties.Item("Name")[0]

$a
- this outputs the object we just created to the pipeline so it is passed to Export-Csv

I'm probably not explaining this great, and I didn't find any really good references out there either. Have a look at this and see if you can make sense of it:

http://thepowershellguy.com/blogs/posh/archive/2007/03/24/hey-powershell-guy-how-can-i-parse-a-tab-delimited-file-and-then-save-that-as-a-comma-separated-values-file.aspx
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Let's recap what we learned from yesterday's Skyport Systems webinar.
I’m willing to make a bet that your organization stores sensitive data in your Windows File Servers; files and folders that you really don’t want making it into the wrong hands.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Screencast - Getting to Know the Pipeline
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question