Not Getting Error Variable Name

This topic contains 3 replies, has 3 voices, and was last updated by Profile photo of Partho Sankar Roy Partho Sankar Roy 10 months, 3 weeks ago.

  • Author
    Posts
  • #34105
    Profile photo of Partho Sankar Roy
    Partho Sankar Roy
    Participant

    Hi Guys,

    I am trying to fetch the user details via below script, When I run it without exporting to csv, I see there are some users id which cant be found although not the email id's I can see, so tried to use try catch loop, but still it only shows not found not the email address. Could you please advise ?

    $users=Get-Content 'xxxxxxxx:h\Desktop\user.txt'

    foreach ($u in $users)

    {

    try {

    Get-ADUser -Filter {Emailaddress -eq $u} -Properties * |select Emailaddress, @{n='User Location';e='co'} |Export-Csv xxxxxxxx\user.csv -Append -NoTypeInformation

    }

    catch { Write-Output "Notfound $u" }
    }

  • #34106
    Profile photo of Tim Curwick
    Tim Curwick
    Participant

    Partho,

    One possibility is that you have a blank line in the text file.

  • #34107
    Profile photo of tommymaynard
    tommymaynard
    Member

    I think Tim's possibility is valid. Gut your function momentarily, and just put Write-Output $U. This will allow you to first determine that you iterating thought the file's contents properly. It's after this, that you start adding the try-catch blocks and Get-ADUser cmdlets.

    Another concern, for me at least, is your calculated property — that's busted. One, you need to reference the property in the Expression portion with $_, or in PowerShell 3.0 and greater, as $PSItem. ...E=$_.co... Second, and quite important, is that there isn't a "co" property. Run this:

    Get-ADUser -Identity auser -Properties *

    and look though the properties (replacing auser with a valid SamAccountName). You'll see that there's no property called "co." Replace it with something that actually exists. Did you mean Country? If so, your calculated property would be:

    @{N='Location';E={$_.Country}}
    
  • #34108
    Profile photo of Partho Sankar Roy
    Partho Sankar Roy
    Participant

    Oh man Tim, You are absolutely right, I dint noticed that as the text file contains more than 2000 lines. Thanks dude 🙂

You must be logged in to reply to this topic.