If you’d like to enter the VMware scripting contest that Carter announced the other day, but don’t have any good ideas to get going, here is a thread I started a while back that has some good ones in it.
VMware Communities: Call for script ideas
I’m also personally sitting on a ton of ideas for other stuff that I may or may not get to during the process of writing my book. If you’d like to explore what can or can’t be done, or bounce ideas off of me (and the rest of the community), go ahead and post in the VITK forums.
(As Carter reminded me—I’m a judge. I probably shouldn’t be having private conversations on this topic with people.
)
This one reports your server uptime. Wow, that was redundant. Anyway, here ‘tis:
PS > Get-VMHost | Get-View | select Name, @{N="Uptime"; E={(Get-Date) - $_.Summary.Runtime.BootTime}}
Name Uptime
---- ------
192.168.0.51 16:21:30.5514830
192.168.0.55 16:20:47.0913190
PS > Get-VMHost | Get-View | ForEach-Object { $_.Summary.Hardware }
Vendor : Dell Computer Corporation
Model : PowerEdge 1600SC
Uuid : 44454c4c-3400-1052-8030-d9c04f303031
OtherIdentifyingInfo :
MemorySize : 2147352576
CpuModel : Intel(R) Xeon(TM) CPU 2.40GHz
CpuMhz : 2392
NumCpuPkgs : 2
NumCpuCores : 2
NumCpuThreads : 4
NumNics : 1
NumHBAs : 1
DynamicType :
DynamicProperty :
Vendor : Dell Computer Corporation
Model : PowerEdge 1600SC
Uuid : 44454c4c-3100-1036-8042-b8c04f573431
OtherIdentifyingInfo :
MemorySize : 2147352576
CpuModel : Intel(R) Xeon(TM) CPU 2.40GHz
CpuMhz : 2392
NumCpuPkgs : 2
NumCpuCores : 2
NumCpuThreads : 4
NumNics : 1
NumHBAs : 1
DynamicType :
DynamicProperty :
Update: LucD has created a detailed cheatsheet with the changes. You can find it on the VMware Communities document library.
The VMware Toolkit (for Windows) was officially released last night. I’m very excited by this for several reasons. For one, I no longer have to “backport” my public code samples to make them compatible with the then-current beta. Two, when I find bugs I can actually converse about them on the VITK forum, rather than having to privately report them in email to a guy who is already busy enough that he doesn’t need me adding to his workload. And three, this stuff is just cool, I’m glad that everyone can play with it now!
I wanted to write a quick post to highlight some of the more obvious changes between the public beta and the 1.0 build 103777.
- This change was mentioned by Carter on the VI PowerShell blog: the cmdlet used to connect to your virtual infrastructure servers has been renamed from Get-VIServer to Connect-VIServer.
- This one is really neat—the sessions are now kept alive just as you see with the graphical VI Client. This means no more running a script only to have it die because your session had timed out.
- The Get-View cmdlet is used when you have an instance of one of the .NET automation objects (type names begin with VMware.VimAutomation.Client20), and you want to use that to retrieve the managed object reference. Prior to release, this cmdlet could only pull up a managed object (the server-side stuff) by the ID property of an automation object. They have since enhanced it so that all you have to do is pass an automation object to Get-View on the pipeline and it just figures it out. Gotta love the pipeline! Here’s a before and after:
Before:
$vmview = Get-VM | foreach-object { Get-View $_.ID }
After:
$vmview = Get-VM | Get-View
If you have any questions, be sure to hang out in the VITK forum or in #PowerShell or #VMware on irc.freenode.net. Lots of nice smart people are ready to answer your calls!
Here goes nuthin’! This was achieved by adding this markup to my blog entry:
<script src="http://poshcode.org/js/474"></script>
Hmm, it blew out my site’s theme pretty bad, so I took it out. CSS needs a little work, I think.
Update @ 10:41pm
Man, that was quick. Sapien has all the reviewers they need. Thanks to everyone who applied!
My publisher needs technical reviewers! If you work with VMware products and want to learn how to automate it with PowerShell (no prior experience with Powershell is necessary) then we would love your feedback. If you review the whole book then you’ll get a free print copy, of course, plus $100. For more details and how to sign up, see Jeffrey Hick’s recent blog post on the SAPIEN Technologies blog.
The technical requirements to participate are:
- PowerShell v1 OR v2
- VI Toolkit for Windows v1.0 (http://vmware.com/go/powershell)
- VMware ESX Server 3.x or 3i (Virtual Center is optional)
OR
- VMware Server 2.0 beta (experimental but may be suitable for testing)
(Updated post to make it clear that you can use ESX or Server, thanks Marco.)
In another case of the web team not talking to the marketing team, looks like the VI Toolkit for Windows is available for download—before the formal release!
Rather than give you the gory details here (125 cmdlets—woo hoo!), I’ll refer you to a gentleman who has been working with VMware for much longer than I have: Eric Sloof. Watch his blog, I expect he’ll be highlighting this shortly.
And of course, when my book, Managing VMware Infrastructure with PowerShell: TFM is ready, that will be a very fine source of information on the subject.
I still don’t have a date yet but definitely “this year”.
Oh and last but not least, we have Carter Shanklin in studio (ok—on the phone) for Eposide 34 of the PowerScripting Podcast. That show will air Friday or Saturday (Aug 1st), so watch for that.
Happy PowerShelling (with VMware)!
These functions will allow you to easily save a set of credentials to disk so that they can be reused during an unattended session later. There are a few blog entries and newsgroup posts about this topic elsewhere, and Lee Holmes covers it in his “PowerShell Cookbook”, but nothing that I have seen actually helps you do the end-to-end of credential –> disk –> credential process. Hope you find it useful! I wrote this for my book, but as it’s of general use I thought I would share.
Update: 7/21/08 @ 11:16a
I’m changing the import conditions to work better on v1 of PowerShell. Download link has updated code.
Update: 7/21/08 @ 7:10p
Reader Steve S. pointed out to me that if you give Export-PSCredential a username, it does not work, but he expected it to. I agreed, so changed the logic a bit. I like it this way, now it works just like Get-Credential does (try get-credential “username” to see what I mean).
One last thing—big props to Brandon Shell who has written on this topic as well. In fact, I umm, er, was troubleshooting that script with him when he wrote it so I should know better. To be fair, I—ok I have no excuse. But I didn’t mean to steal any well-deserved thunder.
My script does store the username where his does not, although he says he’s got an updated one which does. Even so—the more the merrier so that you can see multiple ways to solve a problem.
(This post is the second in my Solving Problems with PowerShell series.)
Problem: Extract list of IP addresses and ping them
So I had a bunch of text in a webpage form which looked like this:
[172.16.16.139] (2.0.39) [172.16.16.31] (2.0.40) [172.16.16.189] (2.0.40) [172.16.16.156] (2.0.45) [172.16.15.89] (2.0.46) [172.16.16.81] (2.0.46) [172.16.16.73] (2.0.47) [172.16.16.31] (2.0.48) [172.16.15.106] (2.0.49) [172.16.16.238] (2.0.49) [172.16.15.14] (2.0.54) [172.16.15.79] (2.0.54)
My goal was to ping each host to see if they were alive. Here’s how I went about that process.
Step One – Get the data into PowerShell
After copying the above into the clipboard, I used PowerShell’s “here-string” feature to quickly get the list into a variable. I just typed this into a console window:
$a = @"
Then I hit enter, then ctrl-V, followed by another enter (so that its on a line by itself) and this:
"@
Now, $a contains the string I want to match against.
Step Two – Parse the text
This one was easier than you may think because I had done some prep some months ago for just such an occasion. There’s this really cool tool Expresso (website appears down, uh oh! google similar results to the rescue!) which helps you to graphically create a regular expression which can then be used to for parsing text. That tool came with a library of commonly used regular expressions, one of which was made to match IP addresses. To save time for when I might need to use it, I have this in my profile which loads when PowerShell is started:
New-Variable -Name RegexIP -Force `
-Description "A regular expression object which matches and validates IP addresses." `
-Value ([regex]'(?<First>2[0-4]\d|25[0-5]|[01]?\d\d?)\.(?<Second>2[0-4]\d|25[0-5]|[01]?\d\d?)\.(?<Third>2[0-4]\d|25[0-5]|[01]?\d\d?)\.(?<Fourth>2[0-4]\d|25[0-5]|[01]?\d\d?)')
With that in hand, here’s how I parsed the text which was now in the $a variable:
97# $regexip.Matches($a) | select value
Value
-----
172.16.16.139
172.16.16.170
172.16.16.31
172.16.16.189
172.16.16.57
172.16.16.156
172.16.15.89
172.16.16.81
172.16.16.73
172.16.16.31
172.16.16.80
172.16.15.106
172.16.16.238
172.16.15.14
172.16.15.79
Step Three – Ping the male progeny of female dogs!
And lastly, pipe the mess to the Ping-Computer cmdlet which is available from SAPIEN’s PowerShell Extensions (PshX-SAPIEN).
PS > $regexip.Matches($a) | select -ExpandProperty value | Ping-Computer ComputerName StatusCode ResponseTime ProtocolAddress ------------ ---------- ------------ --------------- 172.16.16.139 0 0 172.16.16.139 172.16.16.170 0 0 172.16.16.170 172.16.16.31 0 0 172.16.16.31 172.16.16.189 0 0 172.16.16.189 172.16.16.57 0 1 172.16.16.57 172.16.16.156 0 0 172.16.16.156 172.16.15.89 0 0 172.16.15.89 172.16.16.81 0 0 172.16.16.81 172.16.16.73 0 0 172.16.16.73 172.16.16.31 0 0 172.16.16.31 172.16.16.80 0 0 172.16.16.80 172.16.15.106 0 0 172.16.15.106 172.16.16.238 0 0 172.16.16.238 172.16.15.14 0 0 172.16.15.14 172.16.15.79 0 0 172.16.15.79
Instead of using Select-Object –ExpandProperty, I could have used the Foreach-Object cmdlet in a loop like this:
PS > $regexip.Matches($a) | % { Ping-Computer $_.Value }
Same end result, but I have been using select –exp a lot lately and have gotten used to that technique. To each his own.
So that’s it! I hope this walkthrough helps you out with a problem that you need to solve.
In this episode, Jonathan and I have a panel discussion with Dmitry and Kirk from Quest Software. This was the first try at a Skype call to the other side of the globe. It worked fairly well but the lag was somewhat noticeable. This was also our first panel discussion as opposed to interviews. I think it came out pretty good but I definitely have to hone my moderator skills.
