3686 - Automation Using PowerCLI

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

5/24/2013

Automation Using
PowerCLI

Word of Caution

PowerCLI is very cool and very powerful


But can also get very complex

It’s a deep topic that goes further than what you


should expect to see on the exam

If you want to learn more I highly recommend Hal


Rottenberg’s vSphere PowerCLI training from
TrainSignal

What is PowerCLI?

vSphere PowerCLI is a simple C# and PowerShell


interface to VMware vSphere APIs

It comes with many different cmdlets to let you


quickly and easily work with different VMware
vSphere components

Can gather information and script all sorts of


different things without learning a complex
language

1
5/24/2013

PowerCLI Requirements

Requirements for installing vSphere PowerCLI


.NET 2.0 SP1
Windows PowerShell 1.0/2.0

Supported operating systems for PowerCLI 5.0


Windows 7
Windows Server 2008
Windows Vista
Windows XP SP2
Windows 2003 Server SP2

Remote Signing

Many downloaded scripts are


By default PowerShell is set not signed so may want to
to require code signing for disable this checking
scripts
Set-ExecutionPolicy RemoteSigned

Lab Install PowerCLI


In this lab we will
Confirm installation requirements
Install PowerShell
Install PowerCLI
Enable Remote Signing

2
5/24/2013

Cmdlet Concepts

Commands in PowerShell are


called cmdlets

PowerShell cmdlets use a


consistent verb-noun structure
Verb specifies the action and the
noun specifies the object to operate on

Cmdlets have a simple standard naming format

All command categories take parameters and


arguments
A parameter starts with a hyphen and is used to control the
behavior of the command
An argument is a data value consumed by the command
A simple PowerShell command has the following syntax
command -parameter1 -parameter2 argument1 -argument2

Environment Variables

Environment variables store information about the


operating system environment
This information includes details such as the operating system
path, the number of processors used by the operating system, and
the location of temporary folders.

get-item -path env:* (show all environment variables)

3
5/24/2013

Update Manager PowerShell Library

The VMware vSphere Update Manager PowerCLI


provides a set of cmdlets for downloading software
patches, creating and modifying baselines, and for
scanning and remediating virtual machines or
hosts
Download from http://ITtra.in/vumps

To see a list of all Update Manager


PowerCLI cmdlets
Get-Command -PSSnapin VMware.VumAutomation

Using Cmdlets

Use Get-Help to get help on a specific cmdlet


Get-Help Add-VMHost
Get-Help Add-VMHost –full (for more information)

Connect to a local server


Connect-VIServer -Server megatron.nashlab.local

Even if you aren’t a PowerCLI expert you can


usually understand a script

4
5/24/2013

VM Management

List the VMs on the target system


Get-VM

Save the name and power state of the VM in a


ResourcePool to a file named MyLab.txt
$respool = Get-ResourcePool ResourcePool
Get-VM -Location $respool | Select-Object Name, PowerState > MyLab.txt

Start the VM named MailServer


Get-VM MailServer | Start-VM

Host Management

Add the host Jetfire to vCenter


Add-VMHost -Name Jetfire -Location (Get-Datacenter Galaxy) -User
root -Password pass

Put a host in maintenance mode


$host = Get-VMHost -Name Jetfire
$hostCluster = Get-Cluster -VMHost $host
$updateHostTask = Set-VMHost -VMHost $host –
State "Maintenance" -RunAsync
Get-DrsRecommendation -Cluster

$hostCluster | where {$_.Reason -eq "Host is


entering maintenance mode"} | Apply-
DrsRecommendation

5
5/24/2013

Advanced Cmdlet Usage


• Examples of creating objects
Connect to vCenter
Connect-VIServer -Server 192.168.200.205

Get the root folder and create a new folder under it called Earth
$folder = Get-Folder -NoRecursion | New-Folder -Name Earth

Create a new datacenter named Mars


New-Datacenter -Location $folder -Name Mars

Create a new VM synchronously


New-VM -Name NewMailVM -VMHost $host1 -ResourcePool MyResPool -DiskMB
4000 -MemoryMB 256

Create a new VM asynchronously


$vmCreationTask = New-VM -Name NewDBVM -VMHost $host1 -ResourcePool
MyResPool -DiskMB 4000 -MemoryMB 256 –RunAsync

Use Web Service Access Cmdlets

Now referred to as API Access


Cmdlets

Used if the regular cmdlets can


not provide the functionality you
need

Two API Access cmdlets


Get-View
Get-VIObjectByVIView

To really use these you need to know what API


calls are available to you

Highly doubt you’ll see this on the same


Very advanced

6
5/24/2013

Using the Datastore Provider

Datastore provider gives you access to files on


datastores
VimDatastore

When you connect to a server with Connect-


VIServer you get access to two default drives
vmstore - Displays the datastores available on the last connected
vSphere server
vmstores - Contains all datastores available on all vSphere servers
connected within the current vSphere PowerCLI session

Using the Inventory Provider

The Inventory Provider provides a raw inventory


view of the inventory items from a server
VimInventory

When you connect to a server with Connect-


VIServer, the cmdlet builds two default inventory
drives

vi vis

You might also like