Reader small image

You're reading from  Windows Server 2016 Automation with PowerShell Cookbook - Second Edition

Product typeBook
Published inSep 2017
Reading LevelBeginner
Publisher
ISBN-139781787122048
Edition2nd Edition
Languages
Right arrow
Authors (2):
Thomas Lee
Thomas Lee
author image
Thomas Lee

Thomas Lee is a consultant/trainer/writer based in the UK and has been in the IT business since the late 1960s. After graduating from Carnegie Mellon University, Thomas joined ComShare where he was a systems programmer building the Commander II time-sharing operating system, a forerunner of today's cloud computing paradigm. In the mid-1970s, he moved to ICL to work on the VME/K operating system. After a sabbatical in 1980/81, he joined Accenture, leaving in 1988 to run his own consulting and training business, which is still active today. Thomas holds numerous Microsoft certifications, including MCSE (one of the first in the world) and later versions, MCT (25 years), and was awarded Microsoft's MVP award 17 times.
Read more about Thomas Lee

 Ed Goad
Ed Goad
author image
Ed Goad

Ed Goad is a systems architect who has been working in various roles in the IT field for 16 years. He first became interested in scripting and automation when presented with a task to uninstall software from over 1,000 systems with limited time and resources. He has worked with scripting and automation on multiple platforms and languages including PowerShell, VBscript, C#, and BASH scripting. Ed currently holds multiple Microsoft certifications, most recently including the Microsoft Certified IT Professional Enterprise Administrator. Additional non-Microsoft certifications include VMware Certified Professional (VCP), Red Hat Certified System Administrator (RHCSA), EMC Proven Professional, Brocade Certified Network Engineer (BCNE), and Cisco Certified Network Associate (CCNA). Ed is currently on a sabbatical and volunteering full time at the Amor Fe y Esperanza school in Honduras(http://www.afehonduras.org). There he is teaching computer and math classes to the kids who live and work in the garbage dump outside of the capital city of Tegucigalpa.
Read more about Ed Goad

View More author details
Right arrow

Chapter 6. Managing Performance

In this chapter, we are going to cover the following recipes:

  • Exploring performance counters with Get-Counter
  • Exploring performance counters using CIM cmdlets
  • Configuring and use Data Collector Sets
  • Reporting on performance data
  • Generating performance monitoring graph
  • Creating a system diagnostics report

Introduction


Managing performance has been a challenge since the earliest days of computing. Windows NT 3.1 came with a marvelous tool, Performance Monitor (Perfmon), that allowed you to see what the OS and applications are doing and what resources they are consuming.

This chapter shows you how you can use PowerShell to obtain and display performance information. The recipes in this chapter show you how you can use a cmdlet and WMI to get and report the performance information. This chapter shows how you can generate performance graphs and reports for management. And of course, you can leverage the various recipes in this chapter to conduct your performance monitoring and reporting.

The Windows performance monitoring framework is known as Performance Logging and Alerting (PLA). PLA is built into Windows and uses COM and DCOM to obtain performance and diagnosis information from both local and remote computers.

PLA enables you to obtain a variety of performance data from running systems. PLA...

Explore performance counters with Get-Counter


Get-Counter is the cmdlet you use both to discover the counter sets available on a machine, and to obtain performance samples from a local or remote server. In this recipe, you use a Windows Server 2016 server, SRV1, to examine performance counter sets and counters on local and remote computers.

Getting ready

This recipe uses several remote machines: DC1, CA, SRV1, FS1, FS2, and PSRV. Adjust the recipe to reflect the computers in your testing or production environment.

This recipe uses several servers to simulate a normal organization. Consider using fewer servers.

How to do it...

  1. You start by using Get-Counter to discover performance counter sets on the local machine:
$CounterSets = Get-Counter -ListSet *  "There are {0} counter sets on [{1}]" `
                                   -f $CounterSets.count, (hostname)
  1. Discover performance counter sets on remote systems:
$Machines = 'DC1', 'CAa', 'SRV1', 'FS1', 'FS2', 'PSRV' foreach ($Machine in $Machines...

Explore performance counters using CIM cmdlets


Another way to access performance information is via WMI. You can use either the WMI or the CIM cmdlets to access a large number of performance counters, as an alternative to using Get-Counter. The naming structure is different from using Get-Counter. With WMI, each counter is a separate WMI class.

With WMI, each performance counter set is a WMI class. The WMI performance counters are found in the ROOT\CimV2 namespace and have a name that begins with Win32_Perf. For example, the Memory performance counter set contains 36 separate counters. The WMI class Win32_PerfFormattedData_PerfOS_Memory contains 46 properties including all of the individual performance counters. With WMI, you get all the measurements back in one call to Get-CimInstance, whereas you would need to call Get-Counter for each sample. There are other ways to collect counters as shown in later recipes.

In this recipe, you get performance counters from local and remote machines using...

Configuring and using Data Collector Sets


The first two recipes in this chapter used different techniques (Get-Counter and WMI) to retrieve specific counters and counter sets. As you seen, getting a large number of counter values for detailed analysis can be very slow with these mechanisms. These techniques are ideal for retrieving one or two bits of information (CPU utilization for example, or pages/second). If you want to get a larger number of statistics (for example, all of the networking statistics including TCP, UDP, IP, and ICMP) the techniques do not scale well.

A better approach to gathering large number of counters is to use the Data Collector Sets and have Windows do the work for you. To do this, you first create and configure a collector set. When you start the set, Windows starts collecting the data you have configured the collector set to return. Finally, when the collection has been completed, you use Performance Monitor to view the results.

This approach is very much easier...

Reporting on performance data


In the first three recipes in this chapter, you have seen different ways to collect performance information. In this recipe, you create a report on this gathered performance information based on the CSV files output by the data collection process.

Getting ready

This recipe was written to use SRV1. As with any recipe, you can use any server in your infrastructure, but you would need to adjust the details of all steps to reflect the changes you are making. This recipe also used CSV files, as created by the PLA infrastructure. As noted earlier, it is simple and straightforward to adjust the counter output file to be CSV.

Use the recipe Configuring and using Data Collector Sets, change the counter output file to CSV and generate CSV output in the folder C:\PerfLogs\ADMIN.

How to do it...

  1. Import the performance counters:
$Folder = 'C:\PerfLogs\Admin'$File = Get-ChildItem -Path $Folder\*.csv -Recurse
  1. Import the performance counters:
$Counters = Import-Csv -Path $File.FullName...

Generating performance monitoring graph


In the previous recipe, you created a simple text based report which you could expand to cover not just the CPU on the SRV1 server, but more counters across multiple machines. But they would be pure text. You could use performance monitor and the binary log files to create Perfmon graphs you could cut/paste into a report.

In this recipe, you use the data generated using the data collector mechanism to draw a graph using classes from the Windows.Forms.DataVisualization namespace.

Getting ready

Like the Reporting on performance data recipe, this recipe uses CSV files from the data collection process noted earlier.

How to do it...

  1. Load the System.Windows.Forms and System.Windows.Forms.DataVisulization assemblies:
Add-Type -AssemblyName System.Windows.FormsAdd-Type -AssemblyName System.Windows.Forms.DataVisualization
  1. Import the CSV data from earlier, and fix row 0:
$CSVFile = Get-childitem -Path C:\PerfLogs\Admin\*.csv `
                               -Recurse...

Creating a system diagnostics report


The PLA subsystem that you have been working with in this chapter has an additional kind of report that the PLA and PowerShell can generate, a System Diagnostic report. This report is simple to create and makes use of some of the approaches used in this chapter.

Getting ready

You run this recipe on server SRV1.

How to do it...

  1. Start the data collector on the local system:
$PerfReportName="System\System Diagnostics"  $DataSet = New-Object -ComObject Pla.DataCollectorSet$DataSet.Query($PerfReportName,$null)$DataSet.Start($true)
  1. Wait for the data collector to finish:
Start-Sleep -Seconds $Dataset.Duration
  1. Get the report and view it:
$Dataset.Query($PerfReportName,$null)$PerfReport = $Dataset.LatestOutputLocation + "\Report.html"& $PerfReport

How it works...

In step 1, you create a PLA.DataCollectorSet object and use it to query then start the Systems Diagnostics report. This report comes built into Windows, but you update it (or create customized reports if you...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Windows Server 2016 Automation with PowerShell Cookbook - Second Edition
Published in: Sep 2017Publisher: ISBN-13: 9781787122048
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Authors (2)

author image
Thomas Lee

Thomas Lee is a consultant/trainer/writer based in the UK and has been in the IT business since the late 1960s. After graduating from Carnegie Mellon University, Thomas joined ComShare where he was a systems programmer building the Commander II time-sharing operating system, a forerunner of today's cloud computing paradigm. In the mid-1970s, he moved to ICL to work on the VME/K operating system. After a sabbatical in 1980/81, he joined Accenture, leaving in 1988 to run his own consulting and training business, which is still active today. Thomas holds numerous Microsoft certifications, including MCSE (one of the first in the world) and later versions, MCT (25 years), and was awarded Microsoft's MVP award 17 times.
Read more about Thomas Lee

author image
Ed Goad

Ed Goad is a systems architect who has been working in various roles in the IT field for 16 years. He first became interested in scripting and automation when presented with a task to uninstall software from over 1,000 systems with limited time and resources. He has worked with scripting and automation on multiple platforms and languages including PowerShell, VBscript, C#, and BASH scripting. Ed currently holds multiple Microsoft certifications, most recently including the Microsoft Certified IT Professional Enterprise Administrator. Additional non-Microsoft certifications include VMware Certified Professional (VCP), Red Hat Certified System Administrator (RHCSA), EMC Proven Professional, Brocade Certified Network Engineer (BCNE), and Cisco Certified Network Associate (CCNA). Ed is currently on a sabbatical and volunteering full time at the Amor Fe y Esperanza school in Honduras(http://www.afehonduras.org). There he is teaching computer and math classes to the kids who live and work in the garbage dump outside of the capital city of Tegucigalpa.
Read more about Ed Goad