Understanding PowerShell versions
PowerShell, or PoSH as it is also known, is a shell scripting language that Microsoft created to replace batch and VBScript:
- Version 1 was introduced in 2006, was available to be downloaded for Windows XP, Vista, or Windows Server 2003, and was an optional component of Windows Server 2008
- Version 2 was integrated into Windows 7 and Windows Server 2008 R2 server and was available for download for earlier versions Windows XP SP3, Vista SP1, Server 2003 SP2, and Server 2008
- Version 3 was included in the base version of Windows 8 and Windows Server 2012 and could be installed on Windows 7 SP1 and 2008 R2 SP1
- Version 4 was integrated into Windows 8.1 and Windows Server 2012 R2 and could be installed on Windows 7 SP1, Windows Server 2008 R2 SP1, and Windows Server 2012
- Version 5 was still public beta as of the time of writing this book and is only installable on Windows 8.1, Server 2012, and 2012 R2
The basic structure of PowerShell
PoSH, based on the .NET framework, establishes a structure for programming/scripting into a human-readable format. Previous Windows scripting languages were, for the most part, cryptic and it wasn't always easy to pass a script to another administrator without the other administrator having extensive experience with the language.
Originally, the Command Line Interpreter or CLI (cmd.exe
or command.com
) allowed the running of specific executables or EXEs through a recipe of other known commands into a batch (.bat
) or command script (.cmd
). These scripts were used to build a setup for users (such as login scripts) or to launch a customized experience for an application startup or application installation.
The cscript.exe
command enabled the inclusion of JavaScript or VBScript in the recipe and gave an administrator much more power to do far more complex and useful tools. The script was written with an external program (notepad.exe
, for example) and then the cscript.exe
command would have to precede the script.
After Microsoft's previous attempts at power and user friendliness, PoSH follows a basic human readable format of Verb-Noun
for commands to be written. Get-Service
, for example, uses the verb Get
and the noun Service
to specify the desire to get what services are running. Switches such as DisplayName
can add to the clarity of the command and shorten the output:
Get-Service –DisplayName "VMware*" Status Name DisplayName ------ ---- ----------- Running VMTools VMware Tools Running VMUSBArbService VMware USB Arbitration Service Stopped vmvss VMware Snapshot Provider Running vmware-view-usbd VMware View USB Running wsnm VMware View Client
Although, not all commands contain switches such as get-service, clarification of this topic will be discussed later in this chapter.
Why is version understanding important?
As a typical System Administrator of multiple systems, it is likely that PoSH has been used due to newer Microsoft systems' (Exchange, SQL, or basic Windows admin functions) requirements for administration. With every version increment, additional features get added to the product, such as syntax simplification and additional cmdlets (pronounced command-let); this provides more functionality and script simplification possibilities to ensure certain version baselines in the environment allow single source scripts and common programming specifications.
To simplify, using the newest version available allows more functionality and ease of use. To use an analogy of buying a car, getting a newer car generally provides more features, performance, and gadgets. The manufacturer learns from the shortcomings of previous years and makes a better product.
Many companies use a plethora of Windows servers and Windows desktops to support their business's attempt at maintaining a standard platform for operating, deployment, and implementation to reduce the amount of work. In most environments, there are a variety of server versions in place, from Windows Server 2003 to Windows Server 2012 R2, all supporting different applications and being supported by hundreds of vendors. Each iteration of the Server product may, and typically does, have a variety of components that need to be maintained, such as Windows Patches, and in the case of VMware Infrastructure, VMware Tools and Hardware versions (Drivers).
As seen in the Understanding the PowerShell versions section, there are five listed versions available. Of these, version 2 (v2) and version 3 (v3) apply the greatest variance amongst all of the versions. Programming scripts using the v3 syntax will produce errors in v2, whereas v2 syntax will work in v3. Microsoft provides a much simpler experience using the newer versions, and all scripts within this book will be written using v3 unless otherwise stated.
Thankfully, there is a simple way to test what version of the Windows OS is running. To find out the current version of PoSH, type this command:
$PSVersionTable.PSVersion.Major
It will produce a very brief output, as follows:
3
v2 or v3 – what's the difference?
Is there a difference? There are quite a few variations between the two, but a single key differentiator shown here highlights it.
A basic one-line v2 command is as follows:
Get-VM | where {$_.Name –eq "Server"}
The same command using the v3 syntax is as follows:
Get-VM | where Name –eq Server
At first glance, the differences are apparent. v2 demands additional brackets, quotes and variable definitions to make the command work, whereas, v3 removes the cryptic demands and simplifies the command string. With v3, it is backwards compatible, so PoSH v2 commands will also work within the v3 command structure.
One of the significant additions to v3 is the inclusion of workflows. Workflows allow the running of multiple scripts in a cascading fashion. This provides a means to develop the ability to write recipes with many parts into a single consistent plan. This can be extremely powerful and useful and is discussed in later chapters.
So the question that begs to be asked is: Why not develop all code within the context of v2? As seen earlier, PoSH v3 simplifies the development of the code base, so why not use this? This is the dilemma of every site or company embarking on a path to automation, whether to choose a path of conformity versus the added administration of functionality. As an Administrator, most environments that are worked on will include a variety of different systems, from 2003 Server, 2008 R2 Server, and 2012 Server, which include different versions of PowerShell. One of the first automation scripts that will be shown will be a sample bit of code to look through the environment and find PowerShell versions and OSs with the installed version of PowerShell.
Because this book deals with workflows and automation in the latter chapters, all code will be written in v3. Version 4 (v4) provides many additional cmdlets but doesn't provide a significant interface change to warrant the inclusion of it. If there is a v4 specific command used, the text will ensure the reader's understanding of such.
Installing PowerShell v3 on a Windows 7 or Windows 2008 R2 machine
On a standard Windows 7 computer, PoSH v2 comes standard as referred to earlier in this chapter. If running the PoSH command, as shown in the last section, reveals that the system is indeed running v2, there is a simple process that can be gathered from this Microsoft URL:
http://www.microsoft.com/en-us/download/details.aspx?id=34595
The one point it doesn't list is the fact that because PoSH v3 uses the .NET framework for the majority of its functions, .NET version 4 at minimum must be installed on the system as well. Both components can be pushed through any patch management framework that may be in use.