I'm hoping someone can help me through this. I have a script that calls a number of functions and values should be passed from one function to another. In a simple test I can make it work as expected but i have this one function that refuses to pass the correct information. In the below example when I run the script the return code is the DN of the user that I passed to the script with the addition of "LDAP://". I have even tried hard setting $userSAM variable and it still wants to pass the DN back to me. If I rem out the foreach loop and assign the $userSAM everything works as expected. Either way, the write-host line with the $userSAM variable always works.
function GetUserSAM([string]$userDN) { Write-Host "User DN: "$userDN $objDomain1 = New-Object System.DirectoryServices.DirectoryEntry $objSearcher1 = New-Object System.DirectoryServices.DirectorySearcher $strFilter1 = "(&(objectCategory=User)(distinguishedName=" + $userDN + "))" $objSearcher1.SearchRoot = $objDomain1 $objSearcher1.PageSize = 1000 $objSearcher1.Filter = $strFilter1 $objSearcher1.SearchScope = "Subtree" $colResults1 = $objSearcher1.FindAll() foreach ($objResult1 in $colResults1) { $objUser1 = $objResult1.GetDirectoryEntry() $objUser1.adspath $userSAM = $objUser1.sAMAccountName } Write-Host "SAM Account: "$userSAM Return $userSAM }$returnValue = GetUserSAM "CN=Abedeen, Feisal,OU=Users,OU=WestChester,DC=Synthes-Dev,DC=local"Write-Host "Return Value: "$returnValue
Return values from functions
Forum rules
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Return values from functions
I'm hoping someone can help me through this. I have a script that calls a number of functions and values should be passed from one function to another. In a simple test I can make it work as expected but i have this one function that refuses to pass the correct information. In the below example when I run the script the return code is the DN of the user that I passed to the script with the addition of "LDAP://". I have even tried hard setting $userSAM variable and it still wants to pass the DN back to me. If I rem out the foreach loop and assign the $userSAM everything works as expected. Either way, the write-host line with the $userSAM variable always works.
function GetUserSAM([string]$userDN) { Write-Host "User DN: "$userDN $objDomain1 = New-Object System.DirectoryServices.DirectoryEntry $objSearcher1 = New-Object System.DirectoryServices.DirectorySearcher $strFilter1 = "(&(objectCategory=User)(distinguishedName=" + $userDN + "))" $objSearcher1.SearchRoot = $objDomain1 $objSearcher1.PageSize = 1000 $objSearcher1.Filter = $strFilter1 $objSearcher1.SearchScope = "Subtree" $colResults1 = $objSearcher1.FindAll() foreach ($objResult1 in $colResults1) { $objUser1 = $objResult1.GetDirectoryEntry() $objUser1.adspath $userSAM = $objUser1.sAMAccountName } Write-Host "SAM Account: "$userSAM Return $userSAM }$returnValue = GetUserSAM "CN=Abedeen, Feisal,OU=Users,OU=WestChester,DC=Synthes-Dev,DC=local"Write-Host "Return Value: "$returnValue
function GetUserSAM([string]$userDN) { Write-Host "User DN: "$userDN $objDomain1 = New-Object System.DirectoryServices.DirectoryEntry $objSearcher1 = New-Object System.DirectoryServices.DirectorySearcher $strFilter1 = "(&(objectCategory=User)(distinguishedName=" + $userDN + "))" $objSearcher1.SearchRoot = $objDomain1 $objSearcher1.PageSize = 1000 $objSearcher1.Filter = $strFilter1 $objSearcher1.SearchScope = "Subtree" $colResults1 = $objSearcher1.FindAll() foreach ($objResult1 in $colResults1) { $objUser1 = $objResult1.GetDirectoryEntry() $objUser1.adspath $userSAM = $objUser1.sAMAccountName } Write-Host "SAM Account: "$userSAM Return $userSAM }$returnValue = GetUserSAM "CN=Abedeen, Feisal,OU=Users,OU=WestChester,DC=Synthes-Dev,DC=local"Write-Host "Return Value: "$returnValue
Return values from functions
Lets start by simplifying. Let us throw away all of the nervous VBscript code and use POwerShell and ADSI as it was intended to be used.
Here is how to search.
$userCN='Abedeen, Feisal'
$searcher=[adsisearcher]"CN=$userCN"
($searcher.FindOne()).GetDirectoryEntry()
That is all you need for the searcher.
Since you have a DN then you can easily use adsi directly.
[adsi]LDAP://$userDN
That is all you need.
Everything else ou have done is to just reset teh defaults to their default values. This is jsut a nervous habit done by years of peple who copy scripts without every asking 'why'????
[adsisearcher] and [adsi] are called 'type accelerators' which menas they mke it easier to get a type. They both take a string that gets things done. this:[adsi]''
Will get the root of the current domain.
This:
[adsisearcher]''
Will get a searcher with a blank filter.
$searcher=[adsisearcher]'name=joe*'
$searcher.FindAll()
This will find all objects in AD that have a name starting with joe.
'CN=joe*'
all CNs starting with joe
'samaccountname=joe*'
all samaccountnames starting with joe
and so on.
'OU=MyBus*'
'operatingSystem=Windows Server*'
'
jvierra2011-07-21 16:16:04
Here is how to search.
$userCN='Abedeen, Feisal'
$searcher=[adsisearcher]"CN=$userCN"
($searcher.FindOne()).GetDirectoryEntry()
That is all you need for the searcher.
Since you have a DN then you can easily use adsi directly.
[adsi]LDAP://$userDN
That is all you need.
Everything else ou have done is to just reset teh defaults to their default values. This is jsut a nervous habit done by years of peple who copy scripts without every asking 'why'????
[adsisearcher] and [adsi] are called 'type accelerators' which menas they mke it easier to get a type. They both take a string that gets things done. this:[adsi]''
Will get the root of the current domain.
This:
[adsisearcher]''
Will get a searcher with a blank filter.
$searcher=[adsisearcher]'name=joe*'
$searcher.FindAll()
This will find all objects in AD that have a name starting with joe.
'CN=joe*'
all CNs starting with joe
'samaccountname=joe*'
all samaccountnames starting with joe
and so on.
'OU=MyBus*'
'operatingSystem=Windows Server*'
'
jvierra2011-07-21 16:16:04
Return values from functions
Now we do a function
function GetUserSam( $userCN ){
$searcher=[adsisearcher]"CN=$userCN"
($searcher.FindOne()).GetDirectoryEntry().samaccountname
}
And we call it like this
$sam=GetUserSam 'Abedeen, Feisal'
function GetUserSam( $userCN ){
$searcher=[adsisearcher]"CN=$userCN"
($searcher.FindOne()).GetDirectoryEntry().samaccountname
}
And we call it like this
$sam=GetUserSam 'Abedeen, Feisal'
Return values from functions
OK, I got it figured out. My original post was passing the distinguished name to the function we switched it to the CN which won't work. I changed the function and it works. Now i just need to incorporate it into the rest of the script and get moving forward. Thank you for all of your help. One last question, what book(s) would you recommend for powershell info?function GetUserSam( $userCN ){ $searcher=[adsisearcher]"distinguishedName=$userCN" try{ ($searcher.FindOne()).GetDirectoryEntry().samaccountname } catch{ Write-Host "User not found in AD -> $userCN" -fore red -back white }}clear$userDN = "CN=Alesana, Kase,OU=Users,OU=Monument,DC=Synthes-Dev,DC=local"GetUserSam $userDN
Return values from functions
you're the man.. I actually focused on the wrong thing, thanks for setting me straight. I'll look up those books.