• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1304
  • Last Modified:

Powershell, working with if statements

I am working on learning Powershell and I'm working through a booklet I found on technet.  I'm having difficulty with the if statements.  The task is to return a list of services on the system along with their status, then color on screen report text red for stopped and green for running.

I've attached the code that gives me the following error ......

PS C:\Documents and Settings\user> C:\Documents and Settings\user\Desktop\test.ps1
You must provide a value expression on the right-hand side of the '-match' operator.
At C:\Documents and Settings\user\Desktop\test.ps1:1 char:71
+ get-service | Sort-Object Status | ForEach-Object if ($_.Status -match <<<<  Running) {write-host $_.ServiceName $_.Status -foregroundcolor green}
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : ExpectedValueExpression


...what am I doing wrong?
get-service | Sort-Object Status | ForEach-Object if ($_.Status -match Running) {write-host $_.ServiceName $_.Status -foregroundcolor green}

Open in new window

0
alexianit
Asked:
alexianit
  • 5
  • 5
1 Solution
 
Chris DentPowerShell DeveloperCommented:

Hey there :)

It's missing a couple pair of curly braces and pair of quotes.

If we take this and break it up into separate lines:

Get-Service | `
  Sort-Object Status | `
  ForEach-Object
    if ($_.Status -match Running)
    {
      Write-Host $_.ServiceName $_.Status -foregroundcolor green
    }

We have braces to enclose the if statement, nothing wrong there, but none to enclose the ForEach-Object statement. Those are what we need to add, turning it into this:

Get-Service | `
  Sort-Object Status | `
  ForEach-Object
  {
    if ($_.Status -match Running)
    {
      Write-Host $_.ServiceName $_.Status -foregroundcolor green
    }
  }

So far so good, it raises another problem though, a complaint about the usage of the match operator. This is where the quotes come in.

Get-Service | `
  Sort-Object Status | `
  ForEach-Object
  {
    if ($_.Status -match "Running")
    {
      Write-Host $_.ServiceName $_.Status -foregroundcolor green
    }
  }

Now $_.Status is tested against the string "Running" and the code should finally work.

HTH

Chris
0
 
alexianitAuthor Commented:
Ok I think I see .. so if on 1 line then:
Get-Service | Sort-Object Status | ForEach-Object {if ($_.Status -match "Running"){Write-Host $_.ServiceName $_.Status -foregroundcolor green}}

breaking this into several lines does point out some things, what it looks liek on a single line however is that I needed to include a curly-brace before 'if', quotes around the -match Operator and a second curly-brace at the end.
How do I add and else statement to return the other status text in red?

Once I can see the structure I think I'll have a better handle on it.
0
 
Chris DentPowerShell DeveloperCommented:

Do remember that in PowerShell you don't have to stuff everything onto one line as you might have done with batch files.

If, elseif and else work have this format in PowerShell:

if (Condition)
{
  # Code
}
elseif (SecondCondition)
{
  # Code
}
else
{
  # Code
}

You only wanted Else, so we have this modification to the code above:

Get-Service | `
  Sort-Object Status | `
  ForEach-Object
  {
    if ($_.Status -match "Running")
    {
      Write-Host $_.ServiceName $_.Status -foregroundcolor green
    }
    else
    {
      Write-Host $_.ServiceName $_.Status -foregroundcolor red
    }
  }

As before, you can compress that onto a single line, or leave it spread out.

Chris
0
Protect Your Employees from Wi-Fi Threats

As Wi-Fi growth and popularity continues to climb, not everyone understands the risks that come with connecting to public Wi-Fi or even offering Wi-Fi to employees, visitors and guests. Download the resource kit to make sure your safe wherever business takes you!

 
Chris DentPowerShell DeveloperCommented:

While we're looking at these, you might find this little document quite helpful:

PowerShell Quick Reference:

http://www.microsoft.com/downloads/details.aspx?FamilyId=DF8ED469-9007-401C-85E7-46649A32D0E0&displaylang=en

It has examples of statement usage in PS, far easier than trawling through lots of documentation if you either want to do something fairly simple or just refresh your memory.

Chris
0
 
alexianitAuthor Commented:
Ok I think I get it now.  Thank you for your valuable assistance, it is much appreciated!
0
 
alexianitAuthor Commented:
oops

when I run this I get:

cmdlet ForEach-Object at command pipeline position 3
Supply values for the following parameters:
Process[0]:
0
 
Chris DentPowerShell DeveloperCommented:

ForEach-Object needs an input pipeline. Can you show me your current code?

Chris
0
 
alexianitAuthor Commented:
Just as you had listed above
Get-Service | `
  Sort-Object Status | `
  ForEach-Object
  {
    if ($_.Status -match "Running")
    {
      Write-Host $_.ServiceName $_.Status -foregroundcolor green
    }
    else
    {
      Write-Host $_.ServiceName $_.Status -foregroundcolor red
    }
  }

Open in new window

0
 
Chris DentPowerShell DeveloperCommented:

Ah okay, it either needs ` or a line break removing. Examples of both below :)

Chris
# With ` to escape the line-break

Get-Service | `
  Sort-Object Status | `
  ForEach-Object `
  {
    if ($_.Status -match "Running")
    {
      Write-Host $_.ServiceName $_.Status -foregroundcolor green
    }
    else
    {
      Write-Host $_.ServiceName $_.Status -foregroundcolor red
    }
  }


# Without the line break

Get-Service | `
  Sort-Object Status | `
  ForEach-Object {
    if ($_.Status -match "Running")
    {
      Write-Host $_.ServiceName $_.Status -foregroundcolor green
    }
    else
    {
      Write-Host $_.ServiceName $_.Status -foregroundcolor red
    }
  }

Open in new window

0
 
alexianitAuthor Commented:
OH!  So that's what that accent mark is for.  Thanks again!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Improved Protection from Phishing Attacks

WatchGuard DNSWatch reduces malware infections by detecting and blocking malicious DNS requests, improving your ability to protect employees from phishing attacks. Learn more about our newest service included in Total Security Suite today!

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now