Author Posts

May 2, 2014 at 10:13 am

I'll omit the bulk of my script and just focus on this piece that is not working. Maybe there is an easier way to accomplish this.

I had to attach my code as this post was not reading it correctly.

Error: Get-ADComputer : Cannot find an object with identity: '-filter 'name -like "someHostName"" under: 'DC=xx,DC=xxx,DC=org'.

However, I can echo $filter, take it's exact output, append it to get-adcomputer and it works.

I'm confused.

May 2, 2014 at 10:19 am

That's because you're putting the "-filter" parameter name into the quotes. Doesn't work like that.

Get-ADComputer -filter $filter

Would be more correct, if you took "-filter" out of $filter. As-is, your $filter variable is being passed to the -Identity parameter.

May 2, 2014 at 10:40 am

The post is a bit malformed with HTML stuff, but here's how it should look:

Get-ADComputer -Filter "Name -like '$computername'"

You can sometimes get away with using curly braces and script block syntax (and this is all over the examples), but the -Filter parameter actually expects a string, and this is the most consistent way of getting it to work. In your case, though, the curly brace syntax would probably also work fine:

Get-ADComputer -Filter { Name -like $computername }

It's mostly when you need to filter based on object properties (such as Name -like $computer.Name ) where the script block example syntax starts to fall apart.

May 2, 2014 at 11:02 am

Thank you so much for your help. I got the -filter variable working based on your suggestions. Now I've run into one more problem. At the end when I out my $obj, it outputs fine on screen but when I check the csv file it only contains the last object that $obj held. How do I get it to write everything to the csv?


#Retrieves computer name and serial number and exports it to csv

function Get-ComputerInfo {
param(
$computername = $(Read-Host "Please enter the computer search parameters (ex: bhs72*)"),
$filename = $(Read-Host "Please enter the csv path and filename (ex: c:\test.csv)")
)

$comps = Get-ADComputer -filter { name -like $computername } | select -ExpandProperty name
foreach ($comp in $comps) {
$os = gwmi win32_operatingsystem -ComputerName $comp
$bios = gwmi win32_bios -ComputerName $comp

$properties = @{'Computername'=$comp;
'Serial Number'=$bios.serialnumber}
$obj = New-Object -TypeName psobject -Property $properties
Write-Output $obj
$obj | Export-Csv $filename -NoTypeInformation
}
}

Get-ComputerInfo

May 2, 2014 at 11:12 am

You're exporting to the CSV file inside a loop (and without the -Append parameter, which was added in PowerShell 3.0). Typically, what you'd want to do in PowerShell is have your Get-ComputerInfo function just output objects, and then the caller can choose to pipe that to Export-Csv or whatever they need to do. But if you do choose to create the CSV inside the function, you either need to move the call to Export-Csv outside the loop body (ideally using a ForEach-Object pipeline instead of a foreach loop, so you can pipe the results straight to Export-Csv), or just add the -Append parameter to the Export-Csv call inside the loop.

May 2, 2014 at 11:32 am

The -append did it. Thank you so much!