Reader small image

You're reading from  Learning PowerCLI - Second Edition

Product typeBook
Published inFeb 2017
Publisher
ISBN-139781786468017
Edition2nd Edition
Right arrow
Author (1)
Robert van den Nieuwendijk
Robert van den Nieuwendijk
author image
Robert van den Nieuwendijk

Robert van den Nieuwendijk is an IT veteran from the Netherlands with over thirty years of experience in Information Technology. He holds a bachelor degree in software engineering. After working a few years as a programmer of air traffic control and vessel traffic management systems, he started his own company Van den Nieuwendijk Informatica in 1988. Since then he has worked as a freelance systems administrator of OpenVMS, Windows Server, Linux, and VMware vSphere systems, for Dutch governmental organizations and cloud providers. During winter he is also a ski and snowboard instructor at an indoor ski school. With his background as a programmer, he always tries to make his job easier by writing programs or scripts to perform repeating tasks. In the past, he used the C programming language, OpenVMS DCL, Visual Basic Script and KiXtart to do this. Now, he uses Microsoft PowerShell and VMware PowerCLI for all of his scripting work. Robert is a frequent contributor and moderator at the VMware VMTN Communities. Since 2012 VMware awarded him the vExpert title for his significant contributions to the community and a willingness to share his expertise with others. He has a blog at http://rvdnieuwendijk.com where he writes mainly about VMware PowerCLI, Microsoft PowerShell, and VMware vSphere. If you want to get in touch with Robert, then you can find him on Twitter. His username is @rvdnieuwendijk. Robert is also the author of Learning PowerCLI, Packt Publishing.
Read more about Robert van den Nieuwendijk

Right arrow

Chapter 15. Reporting with PowerCLI

Creating reports is the task that most new PowerCLI users start with. If your boss wants a report of all the new virtual machines created last month or a list of all the datastores that are over 90 percent full, PowerCLI will make it easy for you to create such a report and save it as a CSV or HTML file. In this chapter, you will learn some techniques to create reports.

The following topics are covered in this chapter:

  • Retrieving log files

  • Creating log bundles

  • Performance reporting

  • Exporting reports to CSV files

  • Generating HTML reports

  • Sending reports by e-mail

  • Reporting the health of your vSphere environment with vCheck

Retrieving log files


ESXi servers and vCenter Servers generate log files. Some of these log files can be retrieved using PowerCLI. The Get-LogType cmdlet can be used to retrieve information about the available log file types on a virtual machine host or vCenter Server. The syntax of the Get-LogType cmdlet is as follows:

Get-LogType [[-VMHost] <VMHost[]>] [-Server <VIServer[]>]
    [<CommonParameters>]

There are no required parameters. If you specify a value for the -VMHost parameter, the available log types on the host will be retrieved. If you omit the -VMHost parameter, the available log types on the default vCenter Server will be retrieved.

In the first example, we will retrieve the available log types on the vCenter Server:

PowerCLI C:\> Get-LogType

The output of the preceding command is as follows:

Key                              Summary
---                              -------
vpxd:vpxd-13.log                 vCenter Server log in 'plain' format
vpxd:vpxd...

Creating log bundles


VMware technical support might ask for a log bundle when you submit a support request. You can create a log bundle from the vCenter Server's log files with the following command:

PowerCLI C:\> Get-Log -Bundle -DestinationPath c:\

The output of the preceding command is as follows:

Data
----
C:\vcsupport-52387fc8-52de-113b-7f28-1908e9ffa0d7.zip

In the following command, we will create a log bundle from the log files on the host 192.168.0.133:

PowerCLI C:\> Get-Log -VMHost 192.168.0.133 -Bundle
    -DestinationPath  c:\

The output of the preceding command is as follows:

Data
----
C:\vmsupport-52348fab-b7a7-97f1-bf2a-719b91ab84b3.tgz

Performance reporting


If your users complain that their virtual machines are running slowly, you may want to find the reason for their complaints. You also want to know how your vSphere environment is performing, to see if additional hosts are necessary to keep your systems running smoothly. In PowerCLI, you can use the Get-Stat cmdlet to retrieve statistical information that is available on ESXi hosts and vCenter servers.

VMware vCenter servers keep statistical information about the performance of the virtual machines, hosts, resource pools, and so on. This statistical information is retrieved by the hosts in real time and aggregated in four statistical intervals on the vCenter servers. The real-time information is collected by the hosts with a collection frequency of 20 seconds and kept for 1 hour. The aggregated information is stored in the database of the vCenter server.

Retrieving the statistical intervals

Information about the statistical intervals can be retrieved using the Get-StatInterval...

Exporting reports to CSV files


If your boss asks for a report, they probably want it in the form of a spreadsheet. The easiest way to create a spreadsheet from a PowerCLI report is to export it to a CSV file. This CSV file can be imported into a spreadsheet. Another use case for CSV files is creating export files of one system that you can use later to import into another system. For example, you can use CSV files to export the settings of a vCenter Server and import them in another vCenter Server. PowerShell contains the Export-CSV cmdlet to create CSV files. The syntax of the Export-CSV cmdlet is as follows. The first parameter set can be used to specify a delimiter to separate the property values:

Export-Csv [[-Path] <String>] [[-Delimiter] <Char>] [-Append] [-Confirm] [-Encoding {Unicode | UTF7 | UTF8 | ASCII | UTF32 | BigEndianUnicode | Default | OEM}] [-Force] -InputObject <PSObject> [-LiteralPath <String>] [-NoClobber] [-NoTypeInformation] [-WhatIf] [<CommonParameters...

Generating HTML reports


CSV files are nice, but HTML files are more impressive. This section will show you how to create nice-looking HTML files from your reports. PowerShell gives you the ConvertTo-Html cmdlet to do this. The syntax of this cmdlet is as follows.

The first parameter set creates an entire HTML page:

ConvertTo-Html [[-Property] <Object[]>] [[-Head] <String[]>]  [[-Title] <String>] [[-Body] <String[]>] [-As {Table | List}] [-CssUri <Uri>] [-InputObject <PSObject>] [-PostContent <String[]>]  [-PreContent <String[]>] [<CommonParameters>]

The second parameter set creates only an HTML table and omits the HTML, HEAD, TITLE, and BODY tags:

ConvertTo-Html [[-Property] <Object[]>] [-As {Table | List}] [-Fragment] [-InputObject <PSObject>] [-PostContent <String[]>] [-PreContent <String[]>] [<CommonParameters>]

There are no required parameters.

In the first example, you will create a report about the connection...

Sending reports by e-mail


After creating a report, you might want to send it to your boss or to yourself. The PowerShell Send-MailMessage cmdlet can send e-mail messages using an SMTP server. The syntax of this cmdlet is as follows:

Send-MailMessage [-To] <String[]> [-Subject] <String> [[-Body]  <String>] [[-SmtpServer] <String>] [-Attachments <String[]>] [-Bcc  <String[]>] [-BodyAsHtml] [-Cc <String[]>] [-Credential  <PSCredential>] [-DeliveryNotificationOption  {None | OnSuccess | OnFailure | Delay | Never}] [-Encoding <Encoding>] [-Port <Int32>] [-Priority {Normal | Low | High}] [-UseSsl] -From <String> [<CommonParameters>]

The -From, -To, and -Subject parameters are required to send a report by e-mail. You can use the PowerShell $PSEmailServer preference variable for the SMTP server. If the $PSEmailServer variable is not set, you have to use the -SmtpServer parameter.

You can send a report by putting it in the body...

Reporting the health of your vSphere environment with vCheck


In this section of the book, I want to introduce a PowerCLI script that every vSphere admin should use. The vCheck script written by Alan Renouf can check your vSphere environment for various configuration issues and report them in HTML format. The vCheck script reports several issues, such as VMs having CD-ROMs connected, VMs with CPU or memory reservations configured, VMs ballooning or swapping, VMs with less than 100 MB free space on a disk, VMs with an old hardware version, and VMs that do not have VMware Tools installed. These are just a few examples. The script reports many more issues.

The script is written in a modular way, and it uses a plugin for every check it performs. It is very easy to write plugins and add them to the script. There are plugins created to check other technologies such as Microsoft Exchange, vCloud Director, vCloud Air, System Center Virtual Machine Manager (SCVMM), and Cisco UCS. Reading the vCheck...

Summary


In this chapter, you learned techniques to create reports with PowerCLI. You saw how to create log files and log bundles using the Get-Log cmdlet. Performance reporting using the Get-Stat cmdlet was discussed. CSV files and HTML reports were created using the Export-CSV and ConvertTo-Html cmdlets, and they were sent by e-mail using the Send-MailMessage cmdlet. Then you were introduced to the vCheck script to report about common issues in your vSphere environment, and finally, you learned how to use PowerGUI.

This was the last chapter of the book. I hope that you enjoyed reading this book and I hope that you will use PowerCLI to make your job as a vSphere administrator easier.

If you have questions about PowerCLI or PowerCLI scripts that you are writing, the best place to ask your questions is the PowerCLI community in the VMware VMTN Communities at http://www.vmware.com/go/powercli .

Go to the Discussions tab and click on Start a Discussion, or use the PowerCLI command Get-PowerCLICommunity...

lock icon
The rest of the chapter is locked

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
You have been reading a chapter from
Learning PowerCLI - Second Edition
Published in: Feb 2017Publisher: ISBN-13: 9781786468017
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.
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 £13.99/month. Cancel anytime

Author (1)

author image
Robert van den Nieuwendijk

Robert van den Nieuwendijk is an IT veteran from the Netherlands with over thirty years of experience in Information Technology. He holds a bachelor degree in software engineering. After working a few years as a programmer of air traffic control and vessel traffic management systems, he started his own company Van den Nieuwendijk Informatica in 1988. Since then he has worked as a freelance systems administrator of OpenVMS, Windows Server, Linux, and VMware vSphere systems, for Dutch governmental organizations and cloud providers. During winter he is also a ski and snowboard instructor at an indoor ski school. With his background as a programmer, he always tries to make his job easier by writing programs or scripts to perform repeating tasks. In the past, he used the C programming language, OpenVMS DCL, Visual Basic Script and KiXtart to do this. Now, he uses Microsoft PowerShell and VMware PowerCLI for all of his scripting work. Robert is a frequent contributor and moderator at the VMware VMTN Communities. Since 2012 VMware awarded him the vExpert title for his significant contributions to the community and a willingness to share his expertise with others. He has a blog at&nbsp;http://rvdnieuwendijk.com where he writes mainly about VMware PowerCLI, Microsoft PowerShell, and VMware vSphere. If you want to get in touch with Robert, then you can find him on Twitter. His username is @rvdnieuwendijk. Robert is also the author of Learning PowerCLI, Packt Publishing.
Read more about Robert van den Nieuwendijk