1

When using Visual Studio Code and PSverion 7.2.6 I am no longer able to use a variable in Get-ADUser Filter. Command:

Get-ADUser -server $DC  -Filter 'sAMAccountName -eq $Input'  -Properties $sProperties  | Select $sProperties

getting this error: Get-ADUser: Variable: 'Input' found in expression: $Input is not defined.

That works fine in PowerShell ISE ver 5.1.

  • Try double quotes so the variable gets interpreted. I doubt it works in powershell 5. You might need single quotes around the variable too. – js2010 Oct 07 '22 at 17:32
  • 2
    Even tho using `$input` may work, it's definitely not recommend as this is an [automatic variable](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7.2) – Santiago Squarzon Oct 07 '22 at 17:45
  • 1
    @SantiagoSquarzon, good point! thank you – Tihomir Buncic Oct 07 '22 at 18:18
  • @js2010, thank you. That double quotes with single quotes around the variable works in PS 7. – Tihomir Buncic Oct 07 '22 at 18:20

2 Answers2

1

I played with the quotation marks ("sAMAccountName -eq 'Input'") and found the solution:

Get-ADUser -server $DC  -Filter "sAMAccountName -eq '$ReadInput'"  -Properties $sProperties  | Select $sProperties
  • 2
    While that is a viable solution, your original attempt would likely have worked if you hadn't accidentally tried to use the [automatic `$input` variable](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Automatic_Variables#input) for custom purposes. For background information, see [this answer](https://stackoverflow.com/a/44184818/45375). – mklement0 Oct 07 '22 at 18:32
  • @mklement0, Thanks for sharing. Actually, in the script my variable was called $readinput and was producing this error: Get-ADUser: Variable: 'ReadInput' found in expression: $ReadInput is not defined. When I was troubleshooting it, I used $input. I'm still wondering if newer PS version is handling quotation marks differently. – Tihomir Buncic Oct 07 '22 at 20:49
  • With a single-quoted string (`'...'`), it is the AD provider that resolves the variable reference, not PowerShell, which only works with _stand-alone_ variable references, not also _expressions_ (e.g., `$readinput.SomeProperty`), which is what makes it tricky. However, even stand-alone variable references don't work if you'r accessing AD via _implicit remoting_. The linked answer (hopefully) tells the full story. With _string_ operands in your filter, _up-front_ expansion by PowerShell via expandable strings (`"..."`) is a safe choice, save for the potential need to escape embedded quote chars – mklement0 Oct 07 '22 at 21:28
  • @mklement0, Would you support your statement with an example. I used my original script, ran it again this time using Windows Powershell that is version 5.1. and the script completed without errors, then ran the same script using pwsh.exe (version 7) and it failed with the same error. This tests were done on the same Windows 11 laptop. – Tihomir Buncic Oct 10 '22 at 02:43
  • Oh, I see that I missed the cross-PowerShell-edition angle in your question. I can't speak to that, because I do not have access to AD any longer (my findings stem from using Windows PowerShell (5.1)). However, the error message you're getting suggests that the AD provide _is_ still trying to resolve variables, so perhaps there is a bug (assuming you're not using implicit remoting)? – mklement0 Oct 10 '22 at 02:47
  • Thank you. Correct, I am not using implicit remoting. – Tihomir Buncic Oct 10 '22 at 18:23
-1

try this

Get-ADUser -server $DC  -Filter "sAMAccountName -eq '$($Input)'"  -Properties $sProperties  | Select $sProperties
victorR
  • 129
  • 1
  • 13
  • Your answer in effect duplicates [Tihomir Buncic's own](https://stackoverflow.com/a/73994396/45375) (for simple variable references, `"$var"` is fine, no need for `"$($var)"`), except that the linked answer commendably avoids use of the automatic (reserved) `$Input` variable. In short: your answer adds nothing new and shows a practice that should be avoided. – mklement0 Oct 10 '22 at 18:29