Powershell output file missing SamAccountName results

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 5 years, 7 months ago.

  • Author
    Posts
  • #6299

    by GMB1 at 2013-03-04 09:23:35

    New to powershell, and I can't figure out why the SamAccountName column in my output file is empty, while the IsDisabled column has the account status results.

    Import-Csv C:\useraccounts.csv | Foreach-Object{

    $user = ([ADSISEARCHER]"(samaccountname=$($_.SamAccountName))").FindOne()

    if($user)
    {
    New-Object -TypeName PSObject -Property @{
    SamAccountName = $user.SamAccountName
    IsDisabled = $user.GetDirectoryEntry().InvokeGet('AccountDisabled')
    }
    }
    else
    {
    Write-Warning "Can't find user '$($_.SamAccountName)'"
    }
    } | Export-Csv C:\useraccounts_results.csv

    by ArtB0514 at 2013-03-04 10:25:50

    If you do a Get-Member on the object returned by ADSISearcher, you will find that it returns two properties: Path and Properties. If you want to get the samAccountName from it, you need to specify $User.Properties.samAccountName

    if($user) {
    New-Object -TypeName PSObject -Property @{
    SamAccountName = $user.Properties.SamAccountName
    IsDisabled = $user.GetDirectoryEntry().InvokeGet('AccountDisabled')
    }
    } else {Write-Warning "Can't find user '$($_.SamAccountName)'"} | Export-Csv C]

    by GMB1 at 2013-03-04 11:13:22

    Thanks for the response. I added $user.Properties.SamAccountName, and the SamAccountName column is still empty.

    by ArtB0514 at 2013-03-04 11:49:46

    Well, you can always use the GetDirectoryEntry method as you did for the AccountDisabled property, or just use $_.SamAccountName, since the name in the csv file and the name in the directory must be the same.

    by GMB1 at 2013-03-04 12:04:22

    $_.SamAccountName worked! Thanks, ArtB0514!

You must be logged in to reply to this topic.