Reader small image

You're reading from  Mastering PowerCLI

Product typeBook
Published inOct 2015
Reading LevelIntermediate
PublisherPackt
ISBN-139781785286858
Edition1st Edition
Languages
Right arrow
Author (1)
Sajal Debnath
Sajal Debnath
author image
Sajal Debnath

Sajal Debnath is a highly certified Cloud computing technocrat with more than 12 years of experience in virtualized data center design, Cloud computing, and BC/DR solutions. He is an EMCISA, VCAP-DCD/DCA, VCAP-CID/CIA, RHCE 4/5/6, RHCVA, Openstack, and ITIL certified person. He is presently associated with VMware Software India Pvt. Ltd. as a senior system engineer. Previously, he worked with France Telecom, Hewlett Packard, and many more in multiple roles. He is involved in prestigious Indian government projects, such as National Cloud, Digital Locker, and so on.
Read more about Sajal Debnath

Right arrow

Chapter 12. Best Practices and Sample Scripts

So far, we have discussed all the topics that we intended to cover. This has been a long journey and I thank you for staying with me this far. This is going to be the last chapter of this book. In this chapter, we are going to talk about various best practices and three sample scripts. We will start with a discussion on general best practices to be followed while writing PowerShell scripts. In the subsequent sections, we will discuss three sample scripts. In general, this chapter will cover the following topics.

  • Best practices

  • Security hardening script

  • Capacity check report

  • Using a proper editor

So without wasting much time, let's start our first topic of discussion.

Best practices


We have covered many aspects of PowerShell scripting in the preceding chapters, and while doing so we wrote many examples. The majority of these examples were a few lines or blocks of script, but the main aim of these examples was to showcase the feature or functionality that I was explaining. So I tried to use very simple examples without much fuss about best practices. These scripts probably did not conform to the guidelines, but that was intentional as I wanted you to focus on the example, script, and cmdlet itself and not get distracted by the other best practices. In this section, we are going to talk about the best practices that you should follow while writing scripts for yourself or for others.

If you want to script like a pro, then it is necessary to follow these best practices. Many do not consider scripting to be serious coding, but believe it or not scripting is a serious business. You can achieve the same results as applications written in professional programming...

Security Hardening Script


Although we discussed it in Chapter 8, Managing vSphere Security, SRM, vCloud Air and vROps let's revisit the security checking in vSphere environment. I am going to show a script here that will perform a security audit of the entire vSphere environment. This is the first version of the script and it only checks for the security parameters; it does not set the security parameters. In future, I will create another script that will set the respective parameters as well to make the environment secure. So you will have two scripts, one to check the environment and another to patch the environment as per the VMware security best practices. For these settings, I have used the security guidelines by VMware vSphere_6_0_Hardening_Guide_GA_15_Jun_2015.xls which can be found easily via Google.

So, I am not going to explain the security checking portion, but we're going to discuss the structure of the script and what each portion does. Remember this is a first cut of the script...

Capacity check report


The capacity check report script checks the capacity of the vSphere environment and provides a report in HTML format. The details of the script are provided here.

The report is divided into four major parts:

  • Entire Infrastructure Capacity Report

  • Infrastructure Capacity at Cluster Level (Cluster Wise Capacity Report)

  • Infrastructure Capacity at ESXi Host Level (Host Wise Capacity Report)

  • Infrastructure Capacity at Datastore Level (Datastore Wise Capacity Report)

The relation between the major report parts is shown in the following figure:

We'll discuss the parameter description for all the parts in the next section.

Note that all the information is calculated at individual ESXi host level. At the Cluster level, all the individual ESXi parameters are summed up and then divided by the number of hosts in that cluster so that we get an average value for this cluster. For example, if there are two ESXi hosts in a cluster with CPU utilization of 10 percent and 15 percent, then the...

Using a proper editor


The last topic of this chapter is about an editor. An editor is the proper tool for any script writer. Depending on the editor that you use, your actual scripting time may lessen. So I have decided to discuss my favorite editors here.

Sublime Text

In Mac, I use Sublime Text as my PowerShell editor. It is probably one of the most useful editors in a Mac environment. It does what it is supposed to do: work as an editor. By default, it does not support PowerShell formatting but you can add the PowerShell support by using the required packaging. Follow the instructions provided in these links and you are good to go.

First, follow the steps in this link to install the Package Control: https://packagecontrol.io/installation.

Then, install the package for PowerShell using the information provided at https://github.com/SublimeText/PowerShell.

This is what the editor looks like once the PowerShell support is added to it:

In my environment, I use Sublime Text 3. One of the best blogs...

Summary


In this chapter, we discussed the general best practices for PowerShell. We also discussed two sample scripts. The scripts that I used were not the final ones; they contain some portions that do not adhere to the best practices. I discussed those points and mentioned them here. It is intentional as I want you to work on it and give suggestions so that we can rectify this script. It is a task and challenge for you.

In the entire book, I have not given any tasks for practice. These two scripts are the practice scripts for you. Visit my blog and comment there so that we can take it forward. These scripts might not be useful to you in their current state, but they were provided as a starting point to help sharpen your skills.

I plan to host two more scripts in the blog. The first one is Set-Security, which will set the security of the entire vSphere environment as per VMware best practices (it will be the logical next step of Get-Security). The next one will be for elaborate reporting...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Mastering PowerCLI
Published in: Oct 2015Publisher: PacktISBN-13: 9781785286858
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 AU $19.99/month. Cancel anytime

Author (1)

author image
Sajal Debnath

Sajal Debnath is a highly certified Cloud computing technocrat with more than 12 years of experience in virtualized data center design, Cloud computing, and BC/DR solutions. He is an EMCISA, VCAP-DCD/DCA, VCAP-CID/CIA, RHCE 4/5/6, RHCVA, Openstack, and ITIL certified person. He is presently associated with VMware Software India Pvt. Ltd. as a senior system engineer. Previously, he worked with France Telecom, Hewlett Packard, and many more in multiple roles. He is involved in prestigious Indian government projects, such as National Cloud, Digital Locker, and so on.
Read more about Sajal Debnath