powershellscripts.com

Tutorials  Tutorials

Powershell Basics
Introduction to Windows PowerShell Part 1
Introduction to Windows PowerShell Part 2
Introduction to Windows PowerShell Part 3
PowerShell Script Signing
PowerShell Scheduled Tasks
PowerShell Networking Tasks

Powershell Advanced Tutorials
PowerShell and VMware Introduction



 



PowerShell and VMware - Introduction

What Can I do with PowerShell and VMware?

The question is, what can't you do? PowerShell has full API access to VMware's vSphere platform

Getting Started

First, if you don't have access to a vSphere install (read: IP connectivity to a vCenter instance) you're going to have to get that running in a lab or gain access to an existing setup. Note, if you have just a single ESX server but no vCenter, you can still run many of the commands, however not everything will work, likewise some PowerCLI commands must be ran directly against the ESX host.

After that you'll want to download PowerCLI from VMware so you have all the proper snapin's and modules http://communities.vmware.com/community/vmtn/server/vsphere/automationtools/powercli It's free but it does require a login. That same site has a ton of good links to additional help and guides as well. After installing the CLI you'll notice you have new CLI icon, go ahead and launch it. If you are like me, you'll get an error:

File C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1 
cannot be loaded because the execution of scripts is disabled on this system. Please see "get-help about_signing" 
for more details.
To change this, refer to my article about powershell script signing, or just go ahead and run this command

Set-ExecutionPolicy RemoteSigned

Then close out and launch the PowerCLI icon again, this should give you

      	Welcome to the VMware vSphere PowerCLI!

Log in to a vCenter Server or ESX host:          	Connect-VIServer
To find out what commands are available, type:   	Get-VICommand
To show searchable help for all PowerCLI commands:   Get-PowerCLIHelp
Once you've connected, display all virtual machines: Get-VM
If you need more help, visit the PowerCLI community: Get-PowerCLICommunity
Much better.

Connecting

After launching PowerCLI, the first thing you'll need to do is connect to your vSphere install. Let's try it

Connect-VIServer myipaddress

Server[0]: PowerCLI C:\> Connect-VIServer myipaddress
WARNING: There were one or more problems with the server certificate:

* A certification chain processed correctly, but terminated in a root certificate which isn't
trusted by the trust provider.

* The certificate's CN name does not match the passed value.

Certificate: [Subject]
  E=support@vmware.com, CN=vCenter5.vsphere, OU="VMware, Inc.", O="VMware, Inc."
[Issuer]
  E=support@vmware.com, CN=vCenter5.vsphere, OU="VMware, Inc.", O="VMware, Inc."
..

Etc..., etc... I'm getting this because this is just a lab instance I'm hitting, and the certificates don't match up. To keep from getting this in the future we can take the advice of the error and run set-powercliconfiguration to ensure scripts are not affected by the change, use Set-PowerCLIConfiguration to set a value for the InvalidCertificateAction option.

 set-powercliconfiguration -InvalidCertificateAction Ignore

Perform operation?
Performing operation 'Update vSphere PowerCLI configuration.'?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"): Y

Proxy Policy	Default Server  Invalid Certificate
            	Mode        	Action
------------	--------------- --------------------
UseSystemProxy  Multiple    	Ignore

OK, let try again

PowerCLI C:\> Connect-VIServer MyIPAddress

When prompted fill in user and password for your vCenter server. You can also just pass cred's with

PowerCLI C:\> Connect-VIServer 10.xx.xx.71 -User usernamehere -Password passwordhere

Name                       	Port  User
----                       	----  ----
10.xx.xx.71               	443   Administrator

Looking Around

So, now that we are in, what can we do?

PowerCLI C:\> Get-VICommand | more
As you can see, there are quite a few Cmdlet's available, and they follow the normal syntax of Get- let's you view things, Add- adds things, Set- set's things, etc... Let's try one
PowerCLI C:\> Get-VM

Name             	PowerState Num CPUs Memory (MB)
----             	---------- -------- -----------
Win2k8x64   		PoweredOn  1    	4096
Windows Server 20... 	PoweredOff 1    		4096
vCenter          		PoweredOn  1    	4096
vCloud           		PoweredOn  1    	1024
CentOS 5.x       		PoweredOn  1    	2048
.

That's pretty useful, let's try listing our hosts

PowerCLI C:\> Get-VMHost

Name        	ConnectionState PowerState  	Id CpuUsage CpuTotal  Memory  Memory
                                                    	Mhz  	Mhz UsageMB TotalMB
----        	--------------- ----------  	-- -------- -------- ------- -------
esx2.vsphere	Connected   	PoweredOn  ...t-15  	811 	9308	6349   12282
esx1.vsphere	Connected   	PoweredOn  ...st-9  	589 	9308   10667   12282

I wonder how much storage I have left

PowerCLI C:\> Get-Datastore

Name                           	FreeSpaceMB  	CapacityMB
----                           	-----------  	----------
share2                              	141119      	196860
datastore1                          	267330      	273664
datastore1 (1)                      	272692      	273664
share1                               	26971      	196860

We can actually browse inside these datastores as well using the PowerCLI automatically created vmstores: drive object.

ls vmstores:\MyIPAddress@443\vSphere5\share2

Datastore path: [share2]

     	LastWriteTime        	Type   	Length Name
     	-------------        	----   	------ ----
   9/28/2011   4:16 PM      	Folder          	.dvsData
   4/23/2012  11:32 AM      	Folder          	Windows Server 20...
   9/21/2012   4:43 PM      	Folder          	CentOS 5.x
   9/27/2011   8:24 AM      	Folder          	.vSphere-HA
   9/7/2012  11:25 AM      	Folder          	New Virtual Machine
.

In future articles we will do a deeper dive into PowerCLI capabilities by breaking the subject into VM Guest related capabilities and VM Host and Infrastructure capabilities.