Getting away from filter *

This topic contains 4 replies, has 3 voices, and was last updated by Profile photo of Richard Diphoorn Richard Diphoorn 5 months, 2 weeks ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #37738
    Profile photo of Wei-Yen Tan
    Wei-Yen Tan
    Participant

    I know the need to filter but this has always stumped me with a query like this where the properties does not exist in the default view. ie:

    Get-Adcomputer -filter {$_.operatingsystem -like "*2012 R2*"} -properties operatingsystem 
    

    This gives an error and I can understand why because properties is outside the filter scriptblock.

    If I try :

    Get-Adcomputer -filter {$_.operatingsystem -like "*2012 R2*"  -properties operatingsystem }
    

    So now i can only resort to putting that in where-object.

    Can I do this better and then comply with best practices?

    #37741
    Profile photo of Richard Diphoorn
    Richard Diphoorn
    Participant

    These AD filters are always a challenge to get right. You are using the filter in the same way as with Where-Object. This doesn't work, because the AD Cmdlets take in a other filter format. Try this:

    Get-ADComputer -Filter "OperatingSystem -like '*2012 R2*'" -Properties OperatingSystem
    
    #37746
    Profile photo of Wei-Yen Tan
    Wei-Yen Tan
    Participant

    Thanks Richard!

    #37749

    This works for me

    Get-ADComputer -Filter {OperatingSystem -eq 'Windows Server 2012 R2 Standard' } -Properties OperatingSystem | select Name, OperatingSystem

    #37751
    Profile photo of Richard Diphoorn
    Richard Diphoorn
    Participant

    @Richard: What is the reason you use a scriptblock instead of a string for the filter parameter?

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.