Return values from functions

Ask your PowerShell-related questions, including questions on cmdlet development!
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.
This topic is 12 years and 8 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
boyddt_co
Posts: 89
Last visit: Mon Sep 25, 2023 9:36 pm

Return values from functions

Post by boyddt_co »

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
User avatar
boyddt_co
Posts: 89
Last visit: Mon Sep 25, 2023 9:36 pm

Return values from functions

Post by boyddt_co »

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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Return values from functions

Post by jvierra »

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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Return values from functions

Post by jvierra »

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'

User avatar
boyddt_co
Posts: 89
Last visit: Mon Sep 25, 2023 9:36 pm

Return values from functions

Post by boyddt_co »

line 16 would be ($searcher.findone()).Get....
User avatar
boyddt_co
Posts: 89
Last visit: Mon Sep 25, 2023 9:36 pm

Return values from functions

Post by boyddt_co »



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
User avatar
boyddt_co
Posts: 89
Last visit: Mon Sep 25, 2023 9:36 pm

Return values from functions

Post by boyddt_co »

you're the man.. I actually focused on the wrong thing, thanks for setting me straight. I'll look up those books.
User avatar
jhicks
Posts: 1789
Last visit: Mon Oct 19, 2015 9:21 am

Return values from functions

Post by jhicks »

It took me a while to find it as well.
This topic is 12 years and 8 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked