Skip navigation
VMware

This Question is Answered

1 "correct" answer available (10 pts) 2 "helpful" answers available (6 pts)
259 Views 9 Replies Last post: Aug 15, 2008 11:27 AM by sanmad RSS
c_shanklin Master VMware Employees 943 posts since
Dec 3, 2007
Currently Being Moderated

Aug 15, 2008 11:13 AM

Connecting to hosts recently used using a menu interface.

If you have a lot of hosts that you connect to it can be frustrating to remember them all. Since I have this problem I wrote this function to help deal with it. If you use the VI Client, connections are stored in the registry. This function reads these entries and presents you with a menu where you can select the host you want to connect to. Hopefully the forum doesn't mangle the code too much. I copied this into my Initialize-VIToolkitEnvironment.ps1 file so I get it automatically on startup.

 


function Connect-Recent {
     $local:ErrorActionPreference = "SilentlyContinue"
     $recent = (Get-Itemproperty "hkcu:\software\vmware\VMware Infrastructure Client\Preferences").RecentConnections
     if ($recent -eq $null) {
          $recent = (Get-Itemproperty "hkcu:\software\vmware\Virtual Infrastructure Client\Preferences").RecentConnections
     }
     if ($recent -eq $null) {
          write-host -fore red "No recent hosts defined."
          return
     }

     $recents = $recent.split(",")

     write-host -fore yellow "Your recently visited hosts are:"
     $max = 10
     if ($recents.length -lt $max) {
          $max = $recents.length
     }
     for ($i = 1; $i -lt $max+1; $i++) {
          write-host -n "["
          write-host -n -f yellow "$i"
          write-host "]", $recents[$i-1]
     }

     $selection = 0
     while ($selection -gt $max -or $selection -lt 1) {
          $selection = [int](read-host "Select a host to connect to")
     }
     write-host -fore yellow "Connecting to", $recents[$selection-1]
     connect-viserver $recents[$selection-1]
     $global:defaultVIServer = $defaultVIServer
}

 

Here's what it looks like in action:

 

PS C:\> connect-recent
[1] 10.16.83.242
[2] 10.21.10.80
[3] 10.16.83.241
[4] 192.168.217.130
[5] 192.168.217.135
[6] pmstaff-esx1.eng.vmware.com
[7] 10.17.25.73
[8] 192.168.217.131
[9] 192.168.217.129
[10] 10.16.83.240
Select a host: 1
Connecting to 10.16.83.242
There were one or more problems with the server certificate:
 
* The certificate's CN name does not match the passed value.

 

halr9000 Master vExpert 1,124 posts since
Jun 7, 2007
Currently Being Moderated
1. Aug 14, 2008 12:49 PM in response to: c_shanklin
Re: Connecting to hosts recently use using a menu interface.

Hey, that is sweet.  I may have to set an upper bound on it for me, it pulled up 33 hosts. 

 

Author of the upcoming book: Managing VMware Infrastructure with PowerShell

Co-Host, PowerScripting Podcast (http://powerscripting.net)

My signature used to be pretty, but then the forum software broked it. vExpert. Microsoft MVP (Windows PowerShell). Author, Podcaster, Speaker. I'm @halr9000
mcorry Lurker 5 posts since
Dec 11, 2007
Currently Being Moderated
3. Aug 15, 2008 6:43 AM in response to: c_shanklin
Re: Connecting to hosts recently use using a menu interface.

 

The line:

 

 

while ($selection -gt $recent.length+1 -or $selection -lt 1) {

 

 

Should  be

 

 

while ($selection -gt $recent{color:#ff0000}s.length+1 -or $selection -lt 1) {

 

 

Otherwise it will only select the first host.

 

 

 

 

 

 

 

 

sanmad Lurker 3 posts since
Feb 3, 2006
Currently Being Moderated
5. Aug 15, 2008 10:47 AM in response to: c_shanklin
Re: Connecting to hosts recently use using a menu interface.

the registry key is hkcu:\software\vmware\Virtual Infrastructure Client\Preferences

sanmad Lurker 3 posts since
Feb 3, 2006
Currently Being Moderated
7. Aug 15, 2008 11:07 AM in response to: c_shanklin
Re: Connecting to hosts recently use using a menu interface.

I have Version 2.0.1 build 32042.on one of the machines I use and this one has the key hkcu:\software\vmware\Virtual Infrastructure Client\Preferences.

sanmad Lurker 3 posts since
Feb 3, 2006
Currently Being Moderated
9. Aug 15, 2008 11:27 AM in response to: c_shanklin
Re: Connecting to hosts recently use using a menu interface.

This one works on both 2.0 and 2.5 clients

Thanks

Bookmarked By (0)

Share This Page

Communities