This blog post is a description of my first experience playing around with PowerCLI, which is VMware’s PowerShell module for managing vSphere servers. I haven’t really dealt with VMware much in my past, other than VMware Workstation, so I thought it was exciting to get the chance to play around with PowerShell & VMware together!
I won’t bore you with the installation details, and I’ll get right to firing it up. There’s a shortcut to launch PowerCLI in the Start Menu, and it’s got a custom icon on it, which makes it easily recognizable on the Windows Taskbar.
When I fired it up, I was presented with a series of commands that would help me get around. This was great, since I had no idea what to expect when I fired this module up for the first time. One of the things that I really liked about this introductory section was that they mentioned how to access the PowerCLI Community. Community is extremely important to the success of any technology.
It seems like they covered the bases pretty well. Connect to a vSphere server? Yep. Explore additional commands? Yep. Get some help? Yes, but not in a PowerShell-ish way. They should really have an [cci]about_PowerCLI[/cci] document instead.
The next part is a little embarrassing, but hey you can’t blame me either. I typed the Connect-VIServer command, and was prompted for a server name. Since the URL I was provided had a custom port number in it, I decided I’d try the server name + port number. Unfortunately, this didn’t work, and I was thrown an error: [cci]“Invalid URI: The hostname could not be parsed.”[/cci] I tried again, but using the full URL this time, thinking that maybe it was looking for the https:// prefix, but I was wrong. I got the same, ugly red text!
Finally, I decided that I should use: [cci]Get-Help Connect-VIServer[/cci] to see what a proper calling looked like. As it turns out, there is a [cci]-Port[/cci] parameter specifically designed for declaring the port to connect to. Duh. I should have thought of that up front, but be easy on me … I’m still new to the PowerCLI stuff!
So, now let’s give this whole thing another try. I’ll run this command: [cci]Connect-VIServer -Server server01;[/cci] After running that command, I was presented with a credential input dialog! I was pretty impressed. Why? Well, PowerCLI had detected that my current user account did not have access to the vSphere instance, and so instead of failing out, it prompted me to enter alternate credentials! This is a pretty smooth recovery, to be honest, and I’m glad that they added the extra code to verify credentials, and prompt for alternates.
After establishing the connection, I decided that I was curious as to what type of object was returned once a connection was established. So, I ran [cci]Disconnect-VIServer[/cci], and had to confirm that yes, I wanted to disconnect the session. (I later ran [cci]Disconnect-VIServer -Force[/cci], and it still prompted me to disconnect. This is a bug IMO). After disconnecting, I re-ran my connect command like so: [cci]$conn = Connect-VIServer -Server vsphere01 -Port 443; $conn.GetType().FullName;[/cci] This command returned: [cci]VMware.VimAutomation.ViCore.Impl.V1.VIServerImpl[/cci].
I ran [cci]$conn | select *[/cci], and got back some interesting properties such as: ID, ServiceUri, SessionId, User, Version, Build, etc. These properties could provide very useful information during troubleshooting of a script, or if you want to limit your PowerShell script to run against specific versions of vSphere.
Since the PowerCLI console suggested that I run [cci]Get-VM[/cci] as a starter command earlier, I decided that I would do just that. Of course, I wanted to assign the results to a variable, to make them easier to work with, so I run: [cci]$VMList = Get-VM;[/cci]
Once I have a list of VMs in a variable, I can get a count of them: [cci]$VMList.Count;[/cci] I can also examine a specific VM by indexing into the array of VMs. To get the 150th item in a zero-based array, I would use the indexer like so: [cci]$VMList;[/cci]
Well, that’s enough for now! I’m sure I’ll be exploring this more later on.