Link to home
Start Free TrialLog in
Avatar of Erwin Pombett
Erwin PombettFlag for Switzerland

asked on

I have a PS command that respond as expected, Same command into a function (with a variable) things wont work, what's wrong ?

helllo all of you,


I dont understand how a variable is replaced in filter used with Get-ADcomputer.

the following function is generating an error :

function GetComputerListForAService([string] $serviceIdentifier = "A0001", [string] $machineType )
{
    $computerlist = Get-ADComputer -Filter "Name -like '*$serviceIdentifier*'" | select -expand name
    return $computerlist
}

Open in new window



but the following line is giving the expected answer

PS > Get-ADComputer -Filter "Name -like '*A0001*'" | select -expand name

Open in new window


what am i missing with my function ?



thank you in advance for your help.
Avatar of aikimark
aikimark
Flag of United States of America image

Try this
PS > Get-ADComputer -Filter {Name -like "*A0001*"} | select -expand name

Open in new window

Avatar of Erwin Pombett

ASKER

hello aikimark,

thank you for your reply.

i dont get any output with curly around the filter.

i'm looking to pass the "A0001" as a parameter and have it replaced in the powershell line.

Thank you for further help
I also try this  without success:

function GetComputerListForAService([string] $serviceIdentifier = "A0001")
{
    $computerlist = Get-ADComputer -Filter {Name -like "*$($serviceIdentifier)*"} | select -expand name
    return $computerlist
}
ASKER CERTIFIED SOLUTION
Avatar of Erwin Pombett
Erwin Pombett
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Odd that you're running into a problem with what you posted originally.  It works for me (using PS 3.0).
You may want to read this question, as it explains the variations in behavior seen with filter syntax.
https://www.experts-exchange.com/questions/28514109/How-do-I-reference-this-object-variable.html
my question was : how can i use such selection in a function.