TechProsaic

I write about great software, Internet technology, cool gadgets, and The Next Big Thing.

February 21st, 2008

Call for Script Ideas: VMware PowerShell Toolkit

Powershell

Calling all ESX admins!  I am looking for novel ideas for scripts to write for the upcoming VMware Toolkit for Windows PowerShell.  Yes–I am offering to do the writing.  I am doing research for a project (details of which to be announced in the coming weeks), and I could use some really great ideas of missing functionality or fixes to problems you have seen while working with VMware Virtual Infrastructure 3.  Pointers to something cool you have seen done with the VMware Perl Toolkit are good too.  It’ll be amusing to see how much simpler those will be in PowerShell.  :D

So please share anything you can come up with. You can leave them in the comments or contact me directly.  Thanks!

February 20th, 2008

Windows 2008 Server launch event in Atlanta

Powershell Windows

If you are going, let me know via the event I just created in Facebook.  Details are in the link.

February 18th, 2008

New PowerScripting Podcast Episode Released

Powershell

We talk about some pretty cool stuff this week.  Several news articles and a lot of good tips.  You can head over to powerscripting.net to read the full show notes and download the latest show.

February 10th, 2008

Using Web Services from PowerShell: UrbanDictionary.com

Powershell

I was just surfing quasi-randomly tonight and I came across the site programmableweb.com.  Very neat concept.  They are a reference site for various web services available on the Internet.  I was in a writin’ mood, so I thought I’d start a series on how to talk to web services using PowerShell.  The first example will be the UrbanDictionary.com SOAP API as described here and here.

If you are not familiar with the term, by “web service”, I mean a programmatic interface to something which can be reached over the Internet (or a private network), usually via HTTP or HTTPS.  One example might be a weather service.  Say you want to include a weather forecast on your website.  Rather than having a script go to a webpage and plugging in a zip code, then “scraping” the results and try to parse them in your program, what you do is use a connector or library for your language of choice which can speak the protocol that the web service uses.  Some web service protocol examples are: SOAP, XML-RPC, REST, or JSON.  (I just realized, so is WMI + WS-MGMT aka WinRM on Windows…)  You wave your hands over the hat, plug in the web service’s URL, give it a zipcode parameter, and you magically get back the forecast in a nice and tidy bundle of properties to an object, something you can manipulate natively in your program.

That’s your one paragraph web service intro.  Now, on to the tutorial!

  1. Step numero uno: Read Keith Hill’s “Calling a Web Service from PowerShell” article.   He explains how to setup your environment, bind a web service, compile that to a DLL, load the DLL in PowerShell and instantiate an object which represents your brand new web service. 

    (Ok, I’m cheating a little (lot)–Keith’s done most of the work for me here.  But it’s all about the applications, right?)

  2. Go to UrbanDictionary’s API page and request an API key.  Find it in your email and go to the next step.
  3. Follow Keith’s steps, plugging in the web service URL (easily found at programmableweb’s listing for UrbanDictionary).  It looks like this:
       1: wsdl http://api.urbandictionary.com/soap?wsdl
       2: csc /t:library .\UrbanSearch.cs
       3: [reflection.assembly]::LoadFrom( “$pwd\UrbanSearch.dll” )
       4: $urbandicsvc = New-Object UrbanSearch
  4. Now, the $urbandicsvc object holds magical treasures such as Lookup().
       1: $urbandicsvc | gm lookup
       2:    TypeName: UrbanSearch
       3: Name   MemberType Definition
       4: —-   ———- ———-
       5: lookup Method     Definition[] lookup(String key, String term)
  5. Now you find that key from your email and supply it to the Lookup method, along with a term you’d like to lookup.
       1: $key = ‘<your key goes here>’
       2: $urbandicsvc.lookup($key,“fo shizzle”)
       3:  
       4: Exception calling “lookup” with “2″ argument(s): “Invalid key”
       5: At line:1 char:20
       6: + $urbandicsvc.lookup <<<< ($key,“fo shizzle”)

Hmmph.  That’s when I read this little gem from the email I received with the key:

Please also note that your key may not become active until 10PM Pacific. We apologize for the delay.

Oh well, maybe tomorrow, I’m East Coast!  :)

Let me know if you find this sort of article useful, and I’ll write more.

Update - Feb 10th @ 11:40am

Seems I picked a bad first example–I can’t get the service to work, and the developer wiki on urbandictionary.com seems to be defunct.  Maybe they no longer provide the service.

February 9th, 2008

VMware Perl Toolkit versus PowerShell VI Toolkit

Powershell

   1: # The below Perl is equivalent to just typing “Get-Vm” in powershell.
   2:  
   3: use VMware::VILib;
   4: Opts::parse();
   5: Opts::validate();
   6: Util::connect();
   7: my $datacenter = “.”
   8: my $datacenter_view = Vim::find_entity_view(view_type => ‘Datacenter’, filter => { name => $datacenter });
   9: my $host_views = Vim::find_entity_views(view_type => ‘HostSystem’, begin_entity => $datacenter_view);
  10: my $counter = 1;
  11: print “Hosts found:\n”;
  12: foreach (@$host_views) {
  13: print “$counter: “ . $_->name . “\n”;
  14: $counter++;
  15: }
  16: $counter = 1;
  17: print “\nVM’s found:\n”;
  18: my $vm_views = Vim::find_entity_views(view_type => ‘VirtualMachine’, begin_entity => $datacenter_view);
  19: foreach (@$vm_views) {
  20: print “$counter: “ . $_->name . “\n”;
  21: $counter++;
  22: }
  23: Util::disconnect();

 

‘Nuff said.

February 4th, 2008

PowerShell Tip: Using scriptblock objects to simplify calculated properties

Powershell

This came up on the Powershell newsgroup, thought I’d post it here as well.

Jason wrote:
> ok so I ended up with this:

$database

.Tables |% { $_.Columns | select @{Name=“Schema”;

Expression

={$_.Parent.Schema}}, @{Name=“Table”; Expression={$_.Parent.Name}},

Name

, DataType, @{Name=“MaxLength”; Expression={$_.DataType.MaximumLength}},

@{

Name=“Nullable”; Expression={if($_.Nullable -eq $TRUE){“YES”}else{“NO”}}},

@{

Name=“Precision”;

Expression

={$_.DataType.NumericPrecision}},@{Name=“Scale”;

Expression

={$_.DataType.NumericScale}} } | Export-Csv -noTypeInformation

$exportFileName

…which all will agree is a mess.  :D

I find that calc’d properties inevitably grow during creation so I’ve taken to separating out the scriptblock (or the whole hashtable) out to a separate command.  Here’s how:

$NullableSb

= {if($_.Nullable -eq $TRUE){“YES”}else{“NO”}}

then you’d have something like this:

$database

.tables | % { $_.columns | select @{Name=Nullable; Expr=$NullableSb} }

Or you can back out one step further:

   1: $SchemaCol = @{
   2:     Name=“Schema”
   3:     Expression={$_.Parent.Schema}
   4: }
   5: $TableCol = @{Name=“Table”; Expression={$_.Parent.Name}}
   6: $NullableCol = @{ 
   7:     Name = “Nullable”
   8:     Expr = {if($_.Nullable -eq $TRUE){“YES”}else{“NO”}}}
   9: } 

Then you execute it like so:

$database

.tables | % { $_.columns | select $SchemaCol,$TableCol,$NullableCol }

|
Close
E-mail It