VMware

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  

Connecting to hosts recently used using a menu interface. posted: Aug 15, 2008 11:13 AM

Click to view c_shanklin's profile Master 755 posts since
Dec 3, 2007
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.
Click to view halr9000's profile Master 814 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)
Click to view mcorry's profile Lurker 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.


Click to view sanmad's profile Lurker 3 posts since
Feb 3, 2006
the registry key is hkcu:\software\vmware\Virtual Infrastructure Client\Preferences
Click to view sanmad's profile Lurker 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.
Click to view sanmad's profile Lurker 3 posts since
Feb 3, 2006
This one works on both 2.0 and 2.5 clients
Thanks

VMware Developer

SDKs, APIs, Videos, Learn and much more in the Developer community.

Learn More

Developer Sample Code

Increase your developer productivity with VMware API sample code.

Learn More

VMworld Sessions & Labs

Online access to the latest VMworld Sessions & Labs and online services.

Learn more

Purchase PSO Credits Online

Purchase credits to redeem training and consulting services online.

Buy Now

Community Hardware Software

View reported configurations or report your own.

Learn More

VMware vSphere

Come witness the next giant leap in virtualization.

Register Today

Communities