locked
Newb with PowerShell trying to query Active Directory RRS feed

  • Question

  • Hello!

    I'm so green at this and I've gotten frustrated as well!

    I'm just trying to query Active Directory and get a list of Name that has W7NB or W10NB and the description.

    $TestQADSnapin = get-pssnapin | where { $_.Name -eq "Quest.ActiveRoles.ADManagement"} 
    if($TestQADSnapin -eq $null)
    { 
        add-pssnapin -Name Quest.ActiveRoles.ADManagement -ErrorAction SilentlyContinue 
    }
    
    import-module ActiveDirectory
    
    $global:date = get-date -Format yyyyMMdd
    $global:ReportsFolder = "C:\Temp"  
    
    $searchString ="w7nb"
    
    $computers = Get-ADComputer -Filter "$searchString" -Property Name,Description | Select -Property Name,Description
    $computers | out-file $global:ReportsFolder\Laptops_$global:date.txt

    Well, running that doesn't work so hot!  I've tried putting a * after so it is like this w7nb* since there can be characters past the nb but the beginning of the name will always be w7 or w10.

    Get-ADComputer : Error parsing query: 'w7nb' Error Message: 'syntax error' at position: '1'.
    At C:\Temp\Laptops2.ps1:14 char:14
    + $computers = Get-ADComputer -Filter "$searchString" -Property Name,Description | ...
    +              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ParserError: (:) [Get-ADComputer], ADFilterParsingException
        + FullyQualifiedErrorId : Error parsing query: 'w7nb' Error Message: 'syntax error' at position: '1'.,Microsoft.ActiveDirectory.Management.Commands.GetADComputer

    So I need a little schooling here! What I really need to do is buy a book!  Would have been nice if Santa brought one but my wife wouldn't have a clue as to what and/or where to get it. /sigh

    So any help would be most appreciated!

    Cheers!

    Dave

    Thursday, December 27, 2018 12:01 AM

Answers

  • Forget about Quest.  It is obsolete and cannot replace the AD components.

    Get-AdComputer -Filter "Name -like 'W7NB*' -or Name -like 'W10NB*'" -Properties Description |
          Select Name, Description

    That is the only line you need.

    You would not have these issues if you took the time to learn basic PowerShell.

    Microsoft Virtual Academy - Getting Started with Microsoft PowerShell


    \_(ツ)_/


    • Edited by jrv Thursday, December 27, 2018 12:38 AM
    • Marked as answer by Dave.Handler Thursday, December 27, 2018 2:34 AM
    Thursday, December 27, 2018 12:38 AM

All replies

  • Forget about Quest.  It is obsolete and cannot replace the AD components.

    Get-AdComputer -Filter "Name -like 'W7NB*' -or Name -like 'W10NB*'" -Properties Description |
          Select Name, Description

    That is the only line you need.

    You would not have these issues if you took the time to learn basic PowerShell.

    Microsoft Virtual Academy - Getting Started with Microsoft PowerShell


    \_(ツ)_/


    • Edited by jrv Thursday, December 27, 2018 12:38 AM
    • Marked as answer by Dave.Handler Thursday, December 27, 2018 2:34 AM
    Thursday, December 27, 2018 12:38 AM
  • A basic filter must specify a property exposed by the cmdlet, an operator, and a value. For example.

    -Filter {Name -eq "mycomputer"}

    where Name is the property.


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)


    Thursday, December 27, 2018 12:39 AM
  • I agree on learning.  Only problem is I really don't have that much need for it, once in a blue moon.  

    Not sure if there would be much practical application for a home setting.  Perhaps I'll check out that virtual academy.  Can't hurt!

    Thanks a bunch for that!

    Thursday, December 27, 2018 2:37 AM