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
54 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_
  • 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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 39

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Utilizing an array to gracefully append to a list of EmailAddresses
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now