0

Its very basic Here is what I am trying to accomplish:

#Make a list of programs depending on Input

get-service  | Where-Object {$_.name -Like $Input}| select-object name,  DisplayName, ServiceType, Status

get-service  | Where-Object {$_.name -Like "Sys*"}| select-object name, DisplayName, ServiceType, Status

Now the second get-service works no problem. But the first one does not cooperate with me, it just throws all the services on the server.

I obtain the user input with this:

$Input = Read-Host -Prompt 'Input your regex'

So what is going on here. Where have I gone wrong? And no, I test the variable with a write-host, the string is there. Its not disappearing as far as I can tell.

3
  • no the variable is not empty, I tested for that. Commented Dec 4, 2016 at 22:20
  • 1
    It not about being empty, but about not using automatic variables. Commented Dec 4, 2016 at 22:26
  • 1
    @Jason091 $input is an automatic variable, it'll get overwritten for every iteration of Where-Object. Consider Get-Service | Where-Object {Write-Host ($_ -eq $input[0]) } Commented Dec 4, 2016 at 22:36

1 Answer 1

0

The problem is simple due to scope.

The solution is to create a filter like shown here:

Why doesn't PowerShell Where-Object work when passing a variable?

I just did this:

#Make a list of programs depending on Input
$Filter = $Input
get-service  | Where-Object {$_.name -Like $Filter}| select-object name, DisplayName, ServiceType, Status
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.