Home > Blogs > VMware PowerCLI Blog > Monthly Archives: August 2009

Monthly Archives: August 2009

What is “Onyx”? Visit us at VMworld and get a chance to find out.

Sorry, no clippy. Do you enjoy reading huge piles of API documentation? Do you use wireshark to reverse-engineer wire protocols? Do you like using strace to figure out what the heck a program is doing?

Me neither, which is why I’m really excited about a little project we’ve been working on called “Project Onyx”. What exactly is Onyx? Quite simply, Onyx converts mouse clicks into PowerCLI scripts. Sort of like Excel’s macro record, except it’s for vSphere and doesn’t come with a stupid paper clip.

Does this sound like a tool you could use? We’re looking for a handful of dedicated people with a burning need for automation to help us get Onyx really solid. I would probably describe the tool as somewhere between alpha and pre-alpha and it’s a bit hard to set up at the moment, so we need people who are dedicated enough to work through the tough initial phases. Still it really is worth it. I’ve been working with vSphere API for about 3 years now and even I learned a lot by just toying around with Onyx.

So if you want to give Onyx a spin, here’s the deal. Come by session VM2241 at VMworld next week, and bring your business card. During the session we’ll be showing how to take scripts that Onyx generates and turn them into reusable bits of PowerShell code, that can be used in the pipeline with all the PowerCLI commands you know and love. There are two sessions, one on Tuesday at Noon and one on Tuesday at 5:30. Give us your card and we’ll pick 5 names from each session and give you first dibs on the raw power of Onyx. Enjoy the teaser video and hope to see you next week.

VMworld 2009: PowerCLI and PowerShell roundup.

There’s a lot of stuff related to PowerCLI and PowerShell going on at VMworld, so it really deserves a summary of everything. It’s not enough to rename the event to PowerCLIWorld 2009 (maybe next year) but it’s still a pretty big list. Each of the sessions below either talks about PowerCLI directly and in depth, or introduces a new PowerShell tool.

  1. VM2241 – Managing vSphere with PowerCLI
  2. TA2650 – Take PowerCLI to the next level
  3. DV2507 – VMware View – Upcoming Attractions
  4. EA2526 – Sharepoint vSphere Best Practices
  5. TA3521 — vNetwork Monitoring/Troubleshooting Demystification and Ecosystem Tool Demonstration

So if you’re looking to get more automated, scale out your existing environment, or just save a lot of time, be sure to check all these sessions out. Visit vmworld.com to add them to your schedule.

How to list datastores and VMs that are on shared storage.

A lot of the magic VMware provides relies on shared storage. If a VM is on local storage, you can’t VMotion it, HA won’t restart it, and a host of other limitations. Do you know which of your VMs, if any, are on local storage? This script can make answering that question pretty easy. First, here’s a function that will locate datastores that use shared storage.

To use, launch PowerCLI, connect to your vCenter server, load the function above and run:

Get-ShareableDatastore

And you’ll wind up with a list of datastores that your hosts can share.

But that’s not what we’re after, we want datastores that can’t be shared. Not too hard,

$sharableIds = Get-ShareableDatastore | Foreach { $_.ID } 
$localOnly = Get-Datastore | Where { $sharableIds -notcontains $_.ID }

Once we have $localOnly, we pass that to Get-VM.

$localOnly | Get-VM

This gives us the list of VMs that can’t be shared amongst hosts.

Trying to dig into PowerCLI but don’t know PowerShell? Start here.

A lot of people see potential with PowerCLI, but struggle to get started
because PowerShell is a fairly new technology and not too many people know it
yet. If you’re getting started, here are my recommendations.

If you’re getting your hands on PowerShell for the very first time, I’d
recommend working through the first 3 or 4 sections of the PowerShell
owner’s manual
, this will get you to the point where you can launch
PowerShell and are ready to run some commands.

After that, to master PowerShell you’ll have to master these two separate
areas:

  1. The PowerShell language.
  2. PowerShell cmdlets.

Know how to get help.

First, if you’re an absolute beginner, start by mastering PowerShell help.
Spending a few hours here will save you a lot of time later on. PowerShell
cmdlets are self-documenting, so the online help is accurate and definitive. It
also shows you critical facts about cmdlets, such as what arguments they accept,
what input types are needed, and what can be piped in to the cmdlet. There’s a
great intro on using help over at concentratedtech, and
you should also check out MSDN.
Use these guides, play around with the cmdlets, then try these same
principles with a few cmdlets of your own, you won’t regret it.

The key things to remember are “help <cmdlet name> –full” to get all
information about a particular cmdlet (example “help get-vm”) or “help
<cmdlet name> –examples” to get just the examples. One of the great things
about PowerShell is that help cmdlet –examples almost always yields a useful
example or two.

This works great for cmdlets, but unfortunately the situation is not so
simple for things that are part of the PowerShell language itself, like the if
statement or comparison operators. We’ll talk about how to get a handle on those
later.

The top 25 cmdlets worth knowing.

Anyway there are a lot of cmdlets and only so much time in the day. You may
be wondering where to focus your attention. There’s a great online of repository
of PowerShell code over at poshcode.org.
Recently I analyzed all the code on poshcode to count the frequencies of various
cmdlets and operators.

So here are the top 25 cmdlets used by code over at This sample is drawn from a lot of different authors all trying to accomplish different goals, so it's a good cross section of the most useful cmdlets. Note however that I would recommend that beginners not learn add-member until later, even though it is obviously a very useful cmdlet for experts.

Poshcode.org most popular cmdlets.

Rank

Cmdlet
Name

Cmdlet
Frequency

1

add-member(*)

4862

2

foreach-object

4039

3

write-host

3696

4

write-output

2494

5

new-object

2139

6

out-file

1669

7

where-object

1550

8

sort-object

1473

9

get-content

1399

10

get-childitem

1187

11

get-command

805

12

select-object

754

13

read-host

693

14

get-history

693

15

set-variable

682

16

write-debug

657

17

invoke-expression

576

18

test-path

501

19

split-path

471

20

get-process

436

21

add-content

408

22

get-wmiobject

383

23

get-member

379

24

start-process

370

25

write-verbose

354

(*) This cmdlet ranks high due to the nature of the code on
poshcode.org, it’s a useful cmdlet to know but you should wait until you’re an
expert to really dig in to it.

If you’re starting out, you should begin by mastering most of these cmdlets
(you should skip add-member until you’ve become a master in your own right).
Remember, each of these has samples within its help that will get you on your
way. There’s one more cmdlet that VMware admins in particular get a lot of use
from, namely Measure-Object. If you’re mainly going to use PowerShell to manage
VMware, that one should be in your top 10 because our users use this cmdlet a
lot.

Operators and other language features.

As I mentioned, you’ll also need to master the PowerShell language. This
includes learning how to branch using if/then/else, how to build loops with
while and foreach, etc. For example, consider this code:

if ($x –eq 1) {
 write-host "x is 1"
} else {
 write-host "no it isn't"
}

 So how, exactly, do if and else work? Help is available for all these things,
but in some cases it’s hard to track down. PowerShell’s help contains a lot of
topics in the form of about_something. For example, there is an
about_if, which talks about both the if and else statements. You can see all of
them by typing “help *about*”. The other thing to note in the statement above is
the use of “-eq”. This is a so-called comparison operator, and mastering these
is very important. Type “help about_comparison_operators” to learn all about
these. Also, here are the top comparison operators as found on poshcode.org:

 Poshcode.org most popular operators.

Rank

Operator

Frequency

1

-eq

4480

2

-gt

1329

3

-and

1320

4

-match

924

5

-ne

775

6

-contains

582

7

-not

514

8

-lt

506

9

-or

480

Lastly but not leastly, you’ll need to master the pipe. This tends to be
pretty easy because once you start piping it will seem very natural to you. Just
for reference, our trusty vertical friend appears 15491 times.

Getting really serious.

Another great little tutorial is the Top 10 Tips for Using Windows PowerShell. But if you’re really serious about upgrading your PowerShell skills,
you should consider getting a hold of either Hal Rotterberg’s Managing VMware Infrastructure
with Windows PowerShell TFM
and Bruce Payette’s Windows
PowerShell in Action
. Bruce’s book is the best from a theory perspective,
while Hal’s book lets you learn while you improve the way you manage VMware.

If you’re having trouble and need more help, don’t forget that help is
available in the PowerCLI
community
.