0

I have a variable I pull from a form that I need to tie in with a matching display name to retrieve an existing samAccountName.

 If (Get-ADUser -Filter { (displayName -eq $user) -AND ($Returner -eq "Yes")} ) { 
    $Check = Get-ADUser -Filter{displayName -eq $user} -Properties SamAccountName
    $sam = $check.SamAccountName
    $sam
    }

As soon as I have the -AND ($Returner.....) part in there the check fails to execute.

I need that check in there as that is what is passed from the Cherwell form to flag that a user is a returner and then I am going to pull in the current samAccountName for that person.

Can someone assist on how I should be using a check of a parameter in with the Get-ADUser command.

Many thanks

S.

1
  • you are using the scriptblock format for your -Filter ... but that parameter requires a string. the "brilliant persons" who designed the AD module allow the scriptblock format [and use it for many examples], but it is not reliable for complex filters. you likely will need to switch to a string similar to >>> -Filter "(displayName -eq $user) -AND ($Returner -eq 'Yes')" <<< Commented Dec 17, 2019 at 9:18

2 Answers 2

1

I don't see why you would perform the same Get-ADUser command twice..

You can do this like below:

$adUser = Get-ADUser -Filter "DisplayName -eq '$user'" -Properties DisplayName, SamAccountName
$sam = if (($adUser) -and $Returner -eq "Yes" ) { $adUser.SamAccountName }
$sam

Hope that helps

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, that does look way neater - I will put it into dev and see how it hangs together.
is this $sam = if (($adUser) checking for Null? so basically if $adUser has a value and $returner = Yes then create the $sam variable?
1

You are using $Returner inside of the -filter of get-aduser. If I understand correctly, this is a variable created by a form.

You should check for $Returner inside of the if statement:

If ( (Get-ADUser -Filter { displayName -eq $user}) -AND ($Returner -eq "Yes")) { 
    $Check = Get-ADUser -Filter{displayName -eq $user} -Properties SamAccountName
    $sam = $check.SamAccountName
    $sam
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.