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
56 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
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 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

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

Question has a verified solution.

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

Suggested Solutions

This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

770 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