Book Image

Windows Server 2012 Automation with PowerShell Cookbook

By : EDRICK GOAD
Book Image

Windows Server 2012 Automation with PowerShell Cookbook

By: EDRICK GOAD

Overview of this book

Automating server tasks allows administrators to repeatedly perform the same, or similar, tasks over and over again. With PowerShell scripts, you can automate server tasks and reduce manual input, allowing you to focus on more important tasks. Windows Server 2012 Automation with PowerShell Cookbook will show several ways for a Windows administrator to automate and streamline his/her job. Learn how to automate server tasks to ease your day-to-day operations, generate performance and configuration reports, and troubleshoot and resolve critical problems. Windows Server 2012 Automation with PowerShell Cookbook will introduce you to the advantages of using Windows Server 2012 and PowerShell. Each recipe is a building block that can easily be combined to provide larger and more useful scripts to automate your systems. The recipes are packed with examples and real world experience to make the job of managing and administrating Windows servers easier. The book begins with automation of common Windows Networking components such as AD, DHCP, DNS, and PKI, managing Hyper-V, and backing up the server environment. By the end of the book you will be able to use PowerShell scripts to automate tasks such as performance monitoring, reporting, analyzing the environment to match best practices, and troubleshooting.
Table of Contents (19 chapters)
Windows Server 2012 Automation with PowerShell Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Sorting and filtering


One of the great features of PowerShell is its ability to sort and filter objects. This filtering can be used to limit a larger result set and reporting only the information necessary.

This section will review several methods of filtering and sorting data.

How to do it...

  1. To explore the filtering capabilities of PowerShell, we look at the running processes on our computer. We then use the Where clause to filter the results.

    Get-Process | Where-Object {$_.Name -eq "chrome"}
    Get-Process | Where-Object Name -eq "chrome"
    Get-Process | Where-Object Name -like "*hrom*"
    Get-Process | Where-Object Name -ne "chrome"  
    Get-Process | Where-Object Handles -gt 1000
  2. To view sorting in PowerShell, we again view the processes on our computer and use Sort-Object to change the default sort order.

    Get-Process | Sort-Object Handles
    Get-Process | Sort-Object Handles -Descending
    Get-Process | Sort-Object Handles, ID –Descending
  3. To explore the select features of PowerShell, we use Select-String and Select-Object clause:

    Select-String -Path C:\Windows\WindowsUpdate.log -Pattern "Installing updates"
    Get-Process | Select-Object Name -Unique 
  4. To view the grouping capabilities of PowerShell, we use Format-Table with the –GroupBy command:

    Get-Process | Format-Table -GroupBy ProcessName  

How it works...

In the first section, we review various methods of filtering information using the Where clause:

  • The first method uses the PowerShell Where-Object clause format. The $_ identifier represents the object being passed through the pipe, so $_.Name refers to the Name property of the object. The –eq is an equals parameter that instructs Where-Object to compare the two values.

  • The second method performs the same task as the first but uses a parameter format that is new in PowerShell 3.0. In this method the Where-Object comparison no longer needs to use $_ to reference the object being passed, and we no longer need the curly braces {}.

    Note

    Even though the second method shown can often be easier to use and easier to understand, it is important to know both methods. The first method is still in use by PowerShell scripters, and there are some situations that work better using this method.

  • The last three methods perform similar comparisons using different parameter. The –like parameter allows for the use of the wildcard character * allowing for less exact filtering. The –ne parameter means not equal and is the exact opposite of the equals parameter. And the –gt parameter means greater than and compares the attribute value to a known value.

The second section uses the Sort-Object command to sort and organize the object attributes. In this section, we show sorting by the handles attribute in both ascending (the default method), and descending format. Additionally, we see that multiple attributes can be sorted at the same time.

The third section uses the Select-String and Select-Object commands to restrict what is returned. The first method searches the WindowsUpdate.log for the string Installing updates and returns the results. The second method takes the output of Get-Process and filters it to only return a unique list of named processes.

The fourth section shows how to perform grouping based on an attribute. The Format-Table command includes a property named –GroupBy that, instead of returning a single table, will return multiple tables. In this case, for each unique ProcessName, a separate table is returned.