?
Solved

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

Posted on 2015-01-26
6
Medium Priority
?
63 Views
Last Modified: 2015-01-31
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.
0
Comment
Question by:toshi_
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
6 Comments
 
LVL 46

Expert Comment

by:aikimark
ID: 40570518
Try this
PS > Get-ADComputer -Filter {Name -like "*A0001*"} | select -expand name

Open in new window

0
 

Author Comment

by:toshi_
ID: 40570589
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
0
 

Author Comment

by:toshi_
ID: 40570591
I also try this  without success:

function GetComputerListForAService([string] $serviceIdentifier = "A0001")
{
    $computerlist = Get-ADComputer -Filter {Name -like "*$($serviceIdentifier)*"} | select -expand name
    return $computerlist
}
0
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

Accepted Solution

by:
toshi_ earned 0 total points
ID: 40570654
Hello, i only could make it work cumulating in a string as follow.

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

Expert Comment

by:footech
ID: 40570908
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.
http://www.experts-exchange.com/Programming/Languages/Scripting/Powershell/Q_28514109.html
0
 

Author Closing Comment

by:toshi_
ID: 40581248
my question was : how can i use such selection in a function.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question