VMware Communities > Developer Community > VMware vSphere™ PowerCLI > Discussions

This Question is Answered

1 "correct" answer available (10 pts) 2 "helpful" answers available (6 pts)
9 Replies Last post: Aug 15, 2008 11:27 AM by sanmad
Reply

Connecting to hosts recently used using a menu interface.

Aug 15, 2008 11:13 AM

Click to view c_shanklin's profile Master c_shanklin 625 posts since
Dec 3, 2007
VMware
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.
Reply Re: Connecting to hosts recently use using a menu interface. Aug 14, 2008 12:49 PM
Click to view halr9000's profile Master halr9000 766 posts since
Jun 7, 2007
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)
Reply Re: Connecting to hosts recently use using a menu interface. Aug 15, 2008 11:10 AM
Click to view c_shanklin's profile Master c_shanklin 625 posts since
Dec 3, 2007
VMware
Edited: See the first message, there were too many versions floating around.
Reply Re: Connecting to hosts recently use using a menu interface. Aug 15, 2008 6:43 AM
Click to view mcorry's profile Lurker mcorry 5 posts since
Dec 11, 2007

The line:

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

Should be

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

Otherwise it will only select the first host.


Reply Re: Connecting to hosts recently use using a menu interface. Aug 15, 2008 9:46 AM
in response to: mcorry
Click to view c_shanklin's profile Master c_shanklin 625 posts since
Dec 3, 2007
VMware
The original code was broken in a few ways, so I've updated the original post.
Reply Re: Connecting to hosts recently use using a menu interface. Aug 15, 2008 10:47 AM
in response to: c_shanklin
Click to view sanmad's profile Lurker sanmad 3 posts since
Feb 3, 2006
the registry key is hkcu:\software\vmware\Virtual Infrastructure Client\Preferences
Reply Re: Connecting to hosts recently use using a menu interface. Aug 15, 2008 10:52 AM
in response to: sanmad
Click to view c_shanklin's profile Master c_shanklin 625 posts since
Dec 3, 2007
VMware
sanmad wrote:the registry key is hkcu:\software\vmware\Virtual Infrastructure Client\Preferences
That's strange, I have both. What client version do you run?
Reply Re: Connecting to hosts recently use using a menu interface. Aug 15, 2008 11:07 AM
in response to: c_shanklin
Click to view sanmad's profile Lurker sanmad 3 posts since
Feb 3, 2006
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.
Reply Re: Connecting to hosts recently use using a menu interface. Aug 15, 2008 11:11 AM
in response to: sanmad
Click to view c_shanklin's profile Master c_shanklin 625 posts since
Dec 3, 2007
VMware
Can you try the latest version I posted (I edited the first message)? I think it will support both 2.5 and 2.0 clients.
Reply Re: Connecting to hosts recently use using a menu interface. Aug 15, 2008 11:27 AM
in response to: c_shanklin
Click to view sanmad's profile Lurker sanmad 3 posts since
Feb 3, 2006
This one works on both 2.0 and 2.5 clients
Thanks
Actions