none
Get-ADUser with properties variable RRS feed

  • Question

  • Using Get-ADUser with variables works except for the properties variable.  If I type in the attrbutes directly it works.  If I use a variable it fails with the error below:

    Import-Module ActiveDirectory
    $strServer = "myserver:389"
    $strBase = "ou=staff,dc=mydomain,dc=com"
    $strAttributes = "sAMAccountName, description"
    get-ADUser -LDAPFilter "(ObjectClass=user)" -server $strServer -searchbase $strBase -searchscope subtree -ResultSetSize $null -properties $strAttributes

    Get-ADUser : One or more properties are invalid.
    Parameter name: sAMAccountName, description
    At line:8 char:11
    + get-ADUser <<<<  -LDAPFilter "(ObjectClass=user)" -server $strServer -searchbase $strBase -searchscope subtree -ResultSetSize
    $null -properties $strAttributes
        + CategoryInfo          : InvalidArgument: (:) [Get-ADUser], ArgumentException
        + FullyQualifiedErrorId : One or more properties are invalid.
    Parameter name: sAMAccountName, description,Microsoft.ActiveDirectory.Management.Commands.GetADUser


    • Edited by olsonta Monday, February 20, 2012 1:57 PM
    Monday, February 20, 2012 1:56 PM

Answers

  • If I set a variable like this: $strAttributes = "sAMAccountName", "description"

    The result of using the variable $strAttributes is this:

    sAMAccountName
    description

    Any ideas how I could get each item in an array to display on one line divided by a comma the way the -properties switch needs it?

    • Marked as answer by olsonta Tuesday, February 21, 2012 5:54 PM
    Tuesday, February 21, 2012 5:37 PM

All replies

  • Hi Olsonta,

    The issue here is with $strAttributes variable. The intention is to pass two values (SAMAccountName and description) and not a single value "sAMAAccountName, Description". In this scenario, the comma is part of what you are looking for and of course this isn't valid. Just try to set $strAttributes like this:

    $strAttributes = "sAMAccountName", "description"


    Thanks, Wilson Souza - MSFT This posting is provided "AS IS" with no warranties, and confers no rights

    Monday, February 20, 2012 5:15 PM
    Moderator
  • If I set a variable like this: $strAttributes = "sAMAccountName", "description"

    The result of using the variable $strAttributes is this:

    sAMAccountName
    description

    Any ideas how I could get each item in an array to display on one line divided by a comma the way the -properties switch needs it?

    • Marked as answer by olsonta Tuesday, February 21, 2012 5:54 PM
    Tuesday, February 21, 2012 5:37 PM
  • OK, I was getting hung up on converting my variable (which was an array actually) to ouput on one line separated by a comma like you'd type it into Get-ADUser.  Taking your explanation, Wilson, that I needed to pass two values instead of one single value, I tried this and it worked.

    -properties (foreach{$srchAttributes})

    Tuesday, February 21, 2012 5:57 PM