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.
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)
Edited: See the first message, there were too many versions floating around.
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.
The original code was broken in a few ways, so I've updated the original post.
the registry key is hkcu:\software\vmware\Virtual Infrastructure Client\Preferences
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?
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.
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.
This one works on both 2.0 and 2.5 clients
Thanks