Link to home
Start Free TrialLog in
Avatar of kontrariankid
kontrariankidFlag for United States of America

asked on

Get-Content returns "sn" but not "givenName"

Hello. The following works great to pull the sAMAccountName, profilepath, and sn, but stops there. I cannot get it to pull the givenName. I tested to see if displayName would work but it did not. Are there different codes to pull those attributes using this method?

(I saved this to a script.ps1 and exported to txt using > output.txt)

$UserList = Get-Content “Users_sAMAccountName.txt”
$domain = [ADSI]“”
foreach($UserName in $UserList)
{ $searcher = new-object DirectoryServices.DirectorySearcher($domain)
$searcher.filter = “(&(objectClass=user)(sAMAccountName= $UserName))”
$SearchResult = $searcher.findall() | select Properties
$ProfilePath = $SearchResult.properties.profilepath
$sn = $SearchResult.properties.sn
$givenName = $SearchResult.properties.givenName
$username + ",” + $ProfilePath + ",” + $sn + ",” + $givenName
}

This is my output as it stands currently:
sAMAccountName,\\servername\profile$\userfolder,sn,
Avatar of YZlat
YZlat
Flag of United States of America image

what if there is no data for a givenName for that particular record?
ASKER CERTIFIED SOLUTION
Avatar of SubSun
SubSun
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kontrariankid

ASKER

PADOW!!! Sweet! It works. Thank you Subsun.

But seriously? All needs to be lowercase? I have always been careful to use the right case as I believed necessary. Actually the attribute name in ADSIEDIT is givenName as I had specified. Why must it be lowercase for this to work?!# Wow.

So this did not work:
$displayName = $SearchResult.properties.displayName

But this DID work:
$displayName = $SearchResult.properties.displayname

And so did this which I added to be sure:
$displayName = $SearchResult.properties.displayname

Final code change:
$givenName = $SearchResult.properties.givenname
$displayName = $SearchResult.properties.displayname
$username + ",” + $ProfilePath + ",” + $sn + ",” + $givenName + ",” + $displayName
}

Subsun, can you tell me then what exactly I am looking at in terms of this "phrase"? What is the name of this when used with the $ and dot?
$SearchResult.properties.givenname

And what is all the above code in general? Is it classified as .Net even though it works when executing in Powershell?
thank you Subsun! i just hope to get a little clarification as to names, classification, terminology, etc., related to above comments. i will be sure to assign points if i can just get this. i hate to get an answer without understanding the methodology so i can better improve my own critical thinking. i am still learning scripting and focusing now heavily on Powershell. thank you for your help.

i really feel like i learned a lot today because of your feedback + my brain wracking + my search for an alternative method which also worked...

i achieved the goal earlier today using Get-QADUser, but not until finding a server that i could use to install PowerGUI and the ActiveRoles Server and related cmdlets.. anyhow, i needed to do that in order to push my learning rate. Quest never ceases to amaze me. PowerGUI and cmdlets are just so much fun to play with, and the interface is very pleasing to look at.. which is important when spending hours at it.

gc user_input.txt | foreach{Get-QADuser $_ | select LastName,FirstName,LogonName,homeDirectory} | export-csv user_output.csv" -notype
$domain is a standard ADSI object that connects to your domain. Following code assign your domain's distinguishedName to the $domain variable.
$domain = [ADSI]“”

$searcher is a DirectorySearcher object, and we pass the ADSI object $domain in as a constructor. In short we’re saying search in the domain mentioned in variable $domain.
$searcher = new-object DirectoryServices.DirectorySearcher($domain)

After creating a DirectorySearcher object, we need to set the search criteria. we need to use the filter property of $searcher for this. LDAP Search Strings used is (&(objectClass=user)(sAMAccountName= $UserName)) .  
 
$searcher.filter = “(&(objectClass=user)(sAMAccountName= $UserName))”

After setting the criteria, we can use the findall() method to find the object that match the filter in the domain. Following code will search and select the property named 'Properties'. The result is saved in to a variable $SearchResult
$SearchResult = $searcher.findall() | select Properties

Now the variable $SearchResult contain a Multi-Valued Property. Following commands will retrieve the desired valued from the multi-valued property and assign to different variables.
'User generated image'

$ProfilePath = $SearchResult.properties.profilepath
$sn = $SearchResult.properties.sn
$givenName = $SearchResult.properties.givenName


PS: There are many articles available in internet from which you can get more details on using ADSI adapter with PowerShell. I have copied few links for your reference.

Ref :
http://social.technet.microsoft.com/wiki/contents/articles/4231.working-with-active-directory-using-powershell-adsi-adapter-en-us.aspx

http://technet.microsoft.com/hi-in/magazine/2007.06.powershell%28en-us%29.aspx
Excellent. Thank you Subsun. Strange that $SearchResult does not bring properties for me when typing that by itself. It seems to execute but brings nothing. Maybe you are using 3.0? Interesting that the variable does work for me on 2.0 inside the script however. Thank you for the extra information!
comments provided within. thank you.
It should work for you too.. I have manually added a user name and searched to show you how it looks..
User generated image
hmmm.

"make sure the assembly containing this type is loaded."?

PS C:\Users\user\Desktop> $SearchResult
PS C:\Users\user\Desktop> $SearchResult | select -ExpandProperty
Select-Object : Missing an argument for parameter 'ExpandProperty'. Specify a parameter of type 'System.String' and try
 again.
At line:1 char:39
+ $SearchResult | select -ExpandProperty <<<<
    + CategoryInfo          : InvalidArgument: (:) [Select-Object], ParameterBindingException
    + FullyQualifiedErrorId : MissingArgument,Microsoft.PowerShell.Commands.SelectObjectCommand

PS C:\Users\user\Desktop> $SearchResult | select -Expand properties
PS C:\Users\user\Desktop> $SearchResult | Select -Expand Properties
PS C:\Users\user\Desktop> $SearchResult | Select -Expand Properties > searchresult.txt
PS C:\Users\user\Desktop> notepad .\searchresult.txt
PS C:\Users\user\Desktop> .\Get-HomeDir_test.ps1 > .\test.csv
PS C:\Users\user\Desktop> $SearchResult
PS C:\Users\user\Desktop> $domain = [ADSI]""
PS C:\Users\user\Desktop> $searcher = new "(&(objectClass=user)(sAMAccountName=testuser1))"
The term 'new' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spellin
g of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:16
+ $searcher = new <<<<  "(&(objectClass=user)(sAMAccountName=testuser1))"
    + CategoryInfo          : ObjectNotFound: (new:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

PS C:\Users\user\Desktop> $searcher = new-object "(&(objectClass=user)(sAMAccountName=testuser1))"
New-Object : Cannot find type [(&(objectClass=user)(sAMAccountName=testuser1))]: make sure the assembly containing this
 type is loaded.
At line:1 char:23
+ $searcher = new-object <<<<  "(&(objectClass=user)(sAMAccountName=testuser1))"
    + CategoryInfo          : InvalidType: (:) [New-Object], PSArgumentException
    + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand

PS C:\Users\user\Desktop> $searcher.filter = "(&(objectClass=user)(sAMAccountName=crockerb))"
Property 'filter' cannot be found on this object; make sure it exists and is settable.
At line:1 char:11
+ $searcher. <<<< filter = "(&(objectClass=user)(sAMAccountName=crockerb))"
    + CategoryInfo          : InvalidOperation: (filter:String) [], RuntimeException
    + FullyQualifiedErrorId : PropertyNotFound

PS C:\Users\user\Desktop> $UserList = Get-Content "Users_SBCC_sAMAccountName.txt"
PS C:\Users\user\Desktop> $domain = [ADSI]""
PS C:\Users\user\Desktop> foreach($UserName in $UserList)
>> { $searcher = new-object DirectoryServices.DirectorySearcher($domain)
>> $searcher.filter = "(&(objectClass=user)(sAMAccountName= $UserName))"
>> $SearchResult
>> $SearchResult | Select -Expand Properties
>> }
>> $SearchResult
>> $SearchResult | Select -Expand Properties
>>
PS C:\Users\user\Desktop> $searcher = new-object "(&(objectClass=user)(sAMAccountName=testuser1))"
New-Object : Cannot find type [(&(objectClass=user)(sAMAccountName=testuser1))]: make sure the assembly containing this type is loaded.

Following is the correct code..
$domain = [ADSI]""
$searcher = new-object DirectoryServices.DirectorySearcher($domain)
thank you Subsun. looking back, it looks like i did include that line. here it the latest i ran.. on the last line, you will see that i typed "$SearchResult" and hit ENTER, then it just takes me to the next line. no output.

PS C:\Users\user\Desktop> $domain = [ADSI]""
PS C:\Users\user\Desktop> $searcher = New-Object DirectoryServices.DirectorySearcher($domain)
PS C:\Users\user\Desktop> $searcher.filter = "(&(objectClass=user)(sAMAccountName=testuser1))"
PS C:\Users\user\Desktop> $SearchResult = $searcher.findall() | select Properties
PS C:\Users\user\Desktop> $SearchResult
PS C:\Users\user\Desktop>
Do you have a user with sAMAccountName testuser1?
ah! no, i did not. i saw "new-object" and imagined that you were having me create a new user account and then pull properties of that..

so yes it did work now that input the line below using "operator6" which is a valid sAMAccountName. and then it presented me also with the details for several variables as you demonstrated earlier. so this is apparently and efficient substitue for any ADSIEDIT query without needing to drill down the hierarchy in the GUI?!

it appears this pulls most of the attributes that have values; perhaps all the important attributes.

$searcher.filter = "(&(objectClass=user)(sAMAccountName=operator6))"
$SearchResult | Select -Expand Properties

if one needs to query for an attribute that is not listed, then they can just type lines such as these, substituting the desired attribute name, unless you have a better method.

PS C:\Users\user\Desktop> $SearchResult.properties.displayname
OPERATOR6

PS C:\Users\user\Desktop> $SearchResult.properties.accountexpires
9223372036854775807

thank you Subsun.