Author Posts

January 1, 2012 at 12:00 am

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!