Read from File - Query AD

This topic contains 5 replies, has 2 voices, and was last updated by Profile photo of Mike Kirkpatrick Mike Kirkpatrick 1 year, 9 months ago.

  • Author
    Posts
  • #21856
    Profile photo of Mike Kirkpatrick
    Mike Kirkpatrick
    Participant

    Hello,

    I'm struggling with reading content from a file and then using that content to act as the input to a query of Active Directory.
    Any help would be greatly appreciated, I am more than wet behind the ears.

    My input file is formatted as single entry and CRLF. yes there are spaces\blanks in the file (as seen below)

    ie:

    smithj
    johnsonb
    millerr

    collj

    [i]Get-Content "c:\temp\ps\input.txt" | Foreach-Object {Get-ADuser -sAMAcountName $PSItem -Property Description,samAccountName,AccountExpirationDate}[/i]

    ERROR

    [i]Get-ADUser : A parameter cannot be found that matches parameter name 'sAMAcountName'.
    At line:1 char:79
    + Get-Content "c:\temp\ps\input.txt" | Foreach-Object {Get-ADuser -sAMAcountName <<<< $PSItem -Property Description,samAccountName,AccountExpirationDa te} + CategoryInfo : InvalidArgument: [:] [Get-ADUser], ParameterBindingException + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.ActiveDirectory.Management.Commands.GetADUser[/i]

  • #21857
    Profile photo of Don Jones
    Don Jones
    Keymaster

    The blanks are going to cause a problem for you. If those are unavoidable, you're probably going to have to go with a more script-like approach that can test for that.

    foreach ($line in (Get-Content input.txt)) {
      if ($line.trim() -ne '') {
        Get-ADUser ...
      }
    }
    

    Your other problem (and the reason for the error) is that Get-ADUser doesn't have a -samAcountName parameter. It's -samAccountName – you missed a "c."

  • #21858
    Profile photo of Mike Kirkpatrick
    Mike Kirkpatrick
    Participant

    Thank you for the quick reply Don.

    I changed the property to sAMAccountName (typos, what a rookie) and I still error out as initially shown.
    Also, you've written Get-ADUser …. I'm not sure what that means, perhaps an escape character issue with posting to wordpress??

    [i]Get-ADUser : A parameter cannot be found that matches parameter name 'samAccountname'.
    At line:2 char:80
    + Get-Content "c:\temp\ps\input.txt" | Foreach-Object {Get-ADuser -samAccountname <<<< $PSItem -Property Description,samAccountName,AccountExpirationD ate} + CategoryInfo : InvalidArgument: [:] [Get-ADUser], ParameterBindingException [/i]

  • #21859
    Profile photo of Mike Kirkpatrick
    Mike Kirkpatrick
    Participant

    It seems four periods translate to this …
    Get-ADUser "…"

  • #21860
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Yes, that's just a WordPress thing.

    Check out the help for Get-ADUser: http://technet.microsoft.com/en-us/library/ee617241.aspx

    Turns out there's no -samAccountName parameter at all :). You're probably wanting to use -Identity, but you should review the help to be sure that's what you're after (since I don't know the contents of your text file).

  • #22751
    Profile photo of Mike Kirkpatrick
    Mike Kirkpatrick
    Participant

    Thank you Don.
    Following your lead I was able to get the script working (albeit painful but that's learning)

You must be logged in to reply to this topic.