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
59 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 45

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
Synchronize a new Active Directory domain with an existing Office 365 tenant
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
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…

749 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