Stevenls
Stevenls

Reputation: 81

Can I not use a variable in with Get-ADUser

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.

Upvotes: 0

Views: 606

Answers (2)

Theo
Theo

Reputation: 61208

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

Upvotes: 1

hcm
hcm

Reputation: 1020

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
}

Upvotes: 1

Related Questions