A review of Pedro Rodrigo Sabalette about polio (1948) Uma revisão de Pedro Rodrigo Sabalette sobre poliomielite (1948)
4. REVISIÓN DE PEDRO RODRIGO SABA- SABA-LETTE SOBRE LA POLIOMIELITIS (1948)
Using the PowerShell pipeline, you can easily get information from one Cmdlet or object and pass it to another. Using the pipeline and the pipeline variable, this can even be used to perform actions.
1. From the PowerShell console, run the following command to read a list of computers into a variable:
$computers = get-content c:\pshell\part1\lesson3\labs\Computers.txt
This will assign the contents of the computers.txt file to the variable.
2. Type the following command in the PowerShell console to display the list of computers.
$computers
You will now see the list of computers.
Now you will use the pipeline to process this list of computers and ping each computer to test for connectivity. To do this, you will use the foreach-object Cmdlet
3. Type the following to ping each computer from the variable.
$computers | foreach-object {ping $_}
You will now see the results of the ping command.
Note: Each computer’s name is passed to the foreach-object one at a time using the
$_ variable. The ping command uses this name to perform the ping.
This can also be used with other objects, such as files on your computer.
From the PowerShell console, run the following command:
Get-childitem c:\pshell
You will now see the files and folders in c:\pshell.
4. Type the following command to display the full name of each file or folder in the directory.
Get-childitem C:\pshell | foreach-object {$_.fullname}
The full name for each file or folder will be displayed.
Taking this a step further, you may want to filter to just directories or folders. You can do this with the property $_.psiscontainer and the Where-Object Cmdlet. The
psiscontainer property is a Boolean property that is true if the object is a directory or folder.
5. Type the following command to display the full name for each folder in the directory.
Get-childitem C:\pshell | where-object {$_.psiscontainer} | foreach-object {$_.fullname}
You will now see the full name displayed for any folders in c:\pshell.
Expanding on this, you may want to know how many files or folders are under each sub-directory and the total size of each of these folders.
First, try the command on the c:\pshell folder.
6. From PowerShell console, run the following command to display the count and total size of the files in c:\pshell. To reduce the size of the command, use aliases.
Gci c:\pshell –recurse | measure-object length –sum
You will now see displayed the count of files and the sum of the files for the c:\pshell directory.
Using the foreach-object Cmdlet and the pipeline do the same for each top level directory in the c:\pshell path. To do this you will combine the commands used in previous steps.
Get-childitem C:\pshell | where {$_.psiscontainer} | foreach-object {$_.fullname ; gci $_.fullname –recurse | measure-object length -sum}
You will now see displayed the file count and total size of the files for each sub directory The next example uses services.
7. From the PowerShell console, run the following command to display running services:
Get-service | where-object {$_.status –eq “Running”}
The running services will now be displayed.
This can also be done with the foreach-object Cmdlet although this does require more code. This is also using an if statement. This will be covered in more detail in the scripting lesson.
8. From the PowerShell console , run the following command to display running services:
Get-service | foreach-object {if ($_.status –eq “Running”) {write-host “$($_.name) : $($_.status)”}}
Running services will now be displayed. As you are using the write-host Cmdlet you can also add color to the display.
9. From the PowerShell console , run the following command to display running services with color:
Get-service | foreach-object {if ($_.status –eq “Running”) {write-host “$($_.name) : $($_.status)” –foregroundcolor green}}
You will now see the running services displayed in green. Expanding on this you can also display services not running as red by modifying the if statement.
10. From the PowerShell console, run the following command to display running services with color:
Get-service | foreach-object {if ($_.status –eq “Running”) {write-host “$($_.name) : $($_.status)” –foregroundcolor green} else { write-host “$($_.name) :
$($_.status)” –foregroundcolor red}}
You will now see displayed the services and their status in:
Green for running
Red for services that are not running
You might notice that the names of the services are the short names. If you modify the command above to use $_.displayname instead of $_.name you will see the friendly name of the service displayed.
11. From the PowerShell console, run the following command to display running services with color using their displayname:
Get-service | foreach-object {if ($_.status –eq “Running”) {write-host
“$($_.displayname) : $($_.status)” –foregroundcolor green} else { write-host
“$($_.displayname) : $($_.status)” –foregroundcolor red}}
You will notice that the display is not sorted and that the data is displayed as a table starting with status, name and displayname. You can change this by using the sort-object Cmdlet.
12. Type the following to sort the services by their status.
Get-service | sort-object status | foreach-object {if ($_.status –eq “Running”) {write-host “$($_.displayname) : $($_.status)” –foregroundcolor green} else { write-host “$($_.displayname) : $($_.status)” –foregroundcolor red}}
You will now see the services displayed sorted by their status with color.
Note: The use of the foreach-object here is to showcase the use of the pipeline and the processing of each item passed through the pipeline. There are much better ways of performing the above activities.