PowerShell Friday: Getting Started with PowerShell and PowerCLI
PowerShell was release on November 14, 2006 and is now (or should be?) one of the major components in an admins life. If you’re just getting started on Windows or are new to the whole scripting and automation scene, this one is especially for you.
I do a lot of stuff with PowerShell, even for VMware with PowerCLI. PowerCLI adds functionality to an already installed PowerShell to manage your VMware environment from the command line. But before I drift of to PowerCLI I need to explain what PowerShell is and does.
This post is a first in a (hopefully) long line of posts around PowerShell and PowerCLI and other components in your VMware environment, like storage, compute platform and more.
What is PowerShell and Why do I want it
From the Microsoft Website:
PowerShell is an automation platform and scripting language for Windows and Windows Server that allows you to simplify the management of your systems.
The site continues:
Windows PowerShell is a task-based command-line shell and scripting language designed especially for system administration. Built on the .NET Framework, Windows PowerShell helps IT professionals and power users control and automate the administration of the Windows operating system and applications that run on Windows.
Windows PowerShell commands, called cmdlets, let you manage the computers from the command line. Windows PowerShell providers let you access data stores, such as the registry and certificate store, as easily as you access the file system. In addition, Windows PowerShell has a rich expression parser and a fully developed scripting language.
Windows PowerShell includes the following features:
- Cmdlets for performing common system administration tasks, such as managing the registry, services, processes, and event logs, and using Windows Management Instrumentation (WMI).
- A task-based scripting language and support for existing scripts and command-line tools.
- Consistent design. Because cmdlets and system data stores use common syntax and naming conventions, data can be shared easily and the output from one cmdlet can be used as the input to another cmdlet without reformatting or manipulation.
- Simplified, command-based navigation of the operating system, which lets users navigate the registry and other data stores by using the same techniques that they use to navigate the file system.
- Powerful object manipulation capabilities. Objects can be directly manipulated or sent to other tools or databases.
- Extensible interface. Independent software vendors and enterprise developers can build custom tools and utilities to administer their software.
So, to summarize: PowerShell can help me manage the registry, services and so on, from the command line, with an object orientend flow. If I do it correctly it can simplify my day to day business.
Is it difficult?
Learning a new language is not easy, but the guys that build PowerShell sure did everything that they could to create a language that is easy to understand.
PowerShell uses a “verb-noun” naming system, where each cmdlet name consists of a standard verb combined with a noun. The verbs are not always English, but they stand for actions in PowerShell. Nouns in PowerShell look a lot like nouns in any language. They describe the types of objects that you can manipulate in the context of system administration.
A couple of examples that are used/approved verbs: Get, Set, Stop, Start. These verbs get combined to a complete command/cmdlet with a ‘-‘ and a noun, for example ‘Process’ for everything that has to do with processes. That gives you commands like Get-Process, Stop-Process, Start-Process.
For a full list of verbs visit the Microsoft site. For a full list of cmdlets fire up your PowerShell and type
Can you give me an example of how this works?
Ofcourse I can. I can do better, I give you multiple examples. The ‘#’ means it’s a comment/remark line. You don’t have to type it after the command.
Get-Process # Get all processes
Get-Process [pw]* # Get all processes starting with a 'p' or 'w'
Get-Process [pw]* | Stop-Process # Stop all processes starting with a 'p' or 'w'
Get-ChildItem C:\Temp\*.log # Get logfiles in a folder and show their attributes
If you want to do display all these files on your screen you could use:
Get-ChildItem C:\Temp\*.log | get-content
If you want to know the content of an environment variable you do the same thing, but with another ‘drive’:
One of the most useful commands is ‘Get-Help’:
Get-Help Get-ChildItem -full
With this you get the builtin help for the Get-ChildItem cmdlet.
Before executing any of the examples make sure that you understand what they do.
But, what is PowerCLI then?
VMware vSphere PowerCLI is a command-line interface (CLI) tool for automating vSphere and vCloud management.
PowerCLI commands are executed in Windows PowerShell by using PowerShell cmdlets. PowerCLI cmdlets are available for various VMware tasks and components, like HA and DRS setup, configuring virtual machines and other tasks.
The same way you start and stop processes you can start and stop virtual machines. For example:
Starts all VMs that start with ‘w’ or ‘p’. Of course you can create all kinds of combination of cmdlets for VMware as well:
Get-VM [wp]* | Remove-VM
Which removes the VMs from vCenter. if combined with ‘-DeletePermanently’ the VMs get removed from vCenter AND are deleted from disk. Use with caution.
Are there any more PowerShell modules/snapins/orwhatyoucallthem?
There are lots more. I have seen modules/snapins for:
- Horizon View
- vCloud Director
- Cisco UCS
- HP servers
- Dell servers
- git source control
- PowerShell Community Extensions
And there are probably hundreds, if not thousands, more.
Where can I find more info on PowerCLI?
My main resources for PowerCLI are:
I’m convinced, how can I get PowerShell and PowerCLI?
Windows 2012 and Windows 8 already contain Windows PowerShell 3.0. If you want to install PowerShell 4.0 read the instructions very carefully on what and where to install.
You can find PowerCLI (and more help) from www.vmware.com/go/powercli
Other articles in the series PowerShell Friday:
- PowerShell Friday: Getting Started with PowerShell and PowerCLI
- PowerShell Friday: Connecting to vCenter
- PowerShell Friday: Starting VMs
- PowerShell Friday: stopping VMs
- PowerShell Friday: Creating Virtual Machines
- PowerShell Friday: Snapshots
- PowerShell Friday: Adding CPU’s with PowerCLI
- PowerShell Friday: Adding Memory with PowerCLI
- PowerShell Friday: ExtensionData
- PowerShell Friday: Retrieving IP addresses for VMs
- PowerShell Friday: Copying files with Copy-VMGuestFile
- PowerShell Friday: Setting Reservations with PowerCLI
- PowerShell Friday: Enabling SSH with PowerCLI
- PowerShell Friday: Christmas Special
- PowerShell Friday: Configuring vSphere MTU Size
- PowerShell Friday: Load PowerCLI from your own script
- PowerShell Friday: Using the Cisco ACI API