Cannot use variable as a get-adcomputer filter parameter

This topic contains 5 replies, has 3 voices, and was last updated by Profile photo of Biggerstaff Biggerstaff 3 years, 2 months ago.

  • Author
    Posts
  • #14961
    Profile photo of Biggerstaff
    Biggerstaff
    Participant

    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.

  • #14962
    Profile photo of Don Jones
    Don Jones
    Keymaster

    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.

  • #14963
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    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.

    • #14964
      Profile photo of Biggerstaff
      Biggerstaff
      Participant

      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

  • #14965
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    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.

    • #14966
      Profile photo of Biggerstaff
      Biggerstaff
      Participant

      The -append did it. Thank you so much!

You must be logged in to reply to this topic.