• No results found

If you were tempted to call this the for … each … next statement (as it was called in VBScript), then I have good news for you. In Windows PowerShell, there is no “next” required for the

ForEach-Object cmdlet. Therefore, you will never forget to close out a for … each … next state-

ment by leaving out the last next statement again. This is because Windows PowerShell auto- matically closes the statement, and the trailing next is no longer required. Of course, because everything in Windows PowerShell is an object, foreach is actually an alias for the ForEach-

Object cmdlet.

In the ColorCodeProcessOutput.ps1 script, we use a ForEach-Object to produce a color-coded listing of processes running on a local machine that are using more CPU clock cycles than other processes. We use an if statement to decide on the color of the text to display. If the amount of CPU time is less than 100, then the color of text is blue. If it is more than 100, then we change the color of the text to red.

The Write-Host cmdlet is used to write the output from the script to the Windows PowerShell console. There are several properties available from the Get-Process cmdlet. If you wanted to add properties to display, you would simply list them in the first position separated by com- mas, as we did with $_.name and $_.cpu. To see all the properties available from Get-Process, pipeline Get-Process into the Get-Member cmdlet, as shown here:

Get-Process | Get-Member

After you have identified the properties you wish to retrieve, the next argument to supply to the Write-Host cmdlet is the foregroundcolor argument.

The color constants listed in Table 4-5 can be used for both the foregroundcolor argument and the backgroundcolor argument of Write-Host.

Note Before you run ColorCodeProcessOutput.ps1 script on your machine, you may want to use Get-Process with no arguments to see what processes are using the most CPU time, and then adjust the -gt and the -lt arguments in the script accordingly.

ColorCodeProcessOutput.ps1 Get-Process |

ForEach-Object ` {if ($_.cpu -lt 100)

Table 4-5 Write-Host Color Constants

Black DarkBlue DarkGreen DarkCyan

DarkRed DarkMagenta DarkYellow Gray

DarkGray Blue Green Cyan

{Write-Host $_.name, $_.cpu -foregroundcolor blue} elseif ($_.cpu -gt 100)

{Write-Host $_.name, $_.cpu -foregroundcolor red}}

Troubleshooting If you are unable to run Windows PowerShell scripts, one thing to check is the script execution policy. To do this, use the Get-ExecutionPolicy cmdlet.

If the script execution policy is set to Restricted, you will need to change the policy to either Remote Signed or Unrestricted. To do this, you can use the Set-ExecutionPolicy cmdlet.

Exploring the ForEach-Object cmdlet 1. Open Windows PowerShell.

2. Use the Get-Service cmdlet to produce a listing of the name and status of each service

defined on your machine. The code to do this is shown here: Get-Service

3. Pipeline the results from the Get-Service cmdlet to a ForEach-Object. This code is shown here:

ForEach-Object

4. Use the line continuation special escape sequence to continue the ForEach-Object com-

mand to the next line. The line continuation character is the grave accent (`) and on English language keyboards is found on the same key as the tilde (~). This line is shown here (you will not need to repeat ForEach-Object because this is the same line).

ForEach-Object `

5. Open an if statement. The condition to be evaluated is "if the status of the service is equal

to stopped." The status property will be associated with the current pipeline object and is referenced by the special variable $_. The code to do this is shown here:

if ($_.Status -eq "stopped")

6. Open a script block by using the left curly bracket ({). Use the Write-Host cmdlet to

write data to the Windows PowerShell console. Write the name and the status of each service. If the service is stopped, we want to specify the foregroundcolor to be red. Use the separator argument and use the comma, a new line, and a Tab. To specify the new line, use the special escape sequence `n. To specify a Tab, use the special escape sequence `t. The code to do this is shown here:

{Write-Host $_.name, $_.Status -foregroundcolor red -separator ",`n`t"}

7. For the elseif clause, evaluate whether the status of the service in the current pipeline

object is equal to running. This is shown here: elseif ($_.Status -eq "running")

8. If the service is running, we want to write the name and the status of the service in green by

using the foregroundcolor argument of the Write-Host cmdlet. Use the separator argument and use the comma, a new line, and a Tab. To specify the new line, use the special escape sequence `n. To specify a Tab, use the special escape sequence`t. The code to do this is shown here:

{Write-Host $_.name, $_.Status -foregroundcolor green -separator ",`n`t"}}

9. Save your script as yournameColorCodedServiceStatus.ps1. If your script does not work

as expected, compare your script with the ColorCodedServiceStatus.ps1 script in the scripts folder for this chapter.