I am writing a very simple LDAP query to gather some information on a user based upon a logon ID. The problem is that when I assign a value to $accountid it fails (see below) but when I input the ID for XXXX in (sAMAccountName=XXXX), everything works as expected. In other words, I don’t use the variable $accountid but just put in the value directly in the filter statement. What do I need to do to cast the variable $accountid properly so as to be accepted in the filter statement?
$accountid=Read-Host -prompt “Enter the userID for which you want to find info”
$searcher=[ADSISearcher]‘(&(objectClass=User)(objectCategory=person)(sAMAccountName=$accountid))’
$hashtbl=$searcher.FindOne()|select -ExpandProperty Properties
$tele=$hashtbl.Get_Item(“telephonenumber”)
$fullname=$hashtbl.Get_Item(“displayname”)
$office=$hashtbl.Get_Item(“physicaldeliveryofficename”)
$email=$hashtbl.Get_Item(“mail”)
Write-Host “UserID $accountId belongs to $fullname and he/she resides at $office with phone $tele & e-mail $email”
The error rec’d is:
You cannot call a method on a null-valued expression.
At C:\documents and settings\usf48555\My Documents\scripts\UserQry.ps1:4 char:24
- $tele=$hashtbl.Get_Item <<<< (“telephonenumber”)
- CategoryInfo : InvalidOperation: (Get_Item:String) , RuntimeExcept
ion - FullyQualifiedErrorId : InvokeMethodOnNull
- CategoryInfo : InvalidOperation: (Get_Item:String) , RuntimeExcept
You cannot call a method on a null-valued expression.
At C:\documents and settings\usf48555\My Documents\scripts\UserQry.ps1:5 char:28
- $fullname=$hashtbl.Get_Item <<<< (“displayname”)
- CategoryInfo : InvalidOperation: (Get_Item:String) , RuntimeExcept
ion - FullyQualifiedErrorId : InvokeMethodOnNull
- CategoryInfo : InvalidOperation: (Get_Item:String) , RuntimeExcept
You cannot call a method on a null-valued expression.
At C:\documents and settings\usf48555\My Documents\scripts\UserQry.ps1:6 char:26
- $office=$hashtbl.Get_Item <<<< (“physicaldeliveryofficename”)
- CategoryInfo : InvalidOperation: (Get_Item:String) , RuntimeExcept
ion - FullyQualifiedErrorId : InvokeMethodOnNull
- CategoryInfo : InvalidOperation: (Get_Item:String) , RuntimeExcept
You cannot call a method on a null-valued expression.
At C:\documents and settings\usf48555\My Documents\scripts\UserQry.ps1:7 char:25
- $email=$hashtbl.Get_Item <<<< (“mail”)
- CategoryInfo : InvalidOperation: (Get_Item:String) , RuntimeExcept
ion - FullyQualifiedErrorId : InvokeMethodOnNull
- CategoryInfo : InvalidOperation: (Get_Item:String) , RuntimeExcept