I’m not sure how long ago I actually wrote this script, or why it took me so long to get around to blogging it, but this is easily the most used script I’ve ever written. Since VMware made its debut at Sheetz over a decade ago, the number of vCenters to manage has grown quite a bit. The actual number is honestly irrelevant, because connecting PowerCLI to more than one at a time annoys me. Don’t get me wrong, it’s nothing that I think the PowerCLI team did poorly; I’m just a slow, sloppy typer and I never get the command right on the first try. The other thing I like that this script does for me is that it keeps me from forgetting any environments. I often get requests like ‘check TLS on all of the production vCenters’, or ‘verify the VDI vCenters have NTP configured correctly.’ Instead of having to remember and type out all those names, now I can just pick it from a list!

I also used this script as a great trainer for myself. I’m barely above a noob at Powershell and PowerCLI, so I used this opportunity to get comfortable working with functions, switches, and capturing input.

The only resource I leaned on for this job was an article written by Adam Bertram at business.com. I was pretty much able to plug and play his menu into my script; just modifying it to ask the question and present the answers as I needed.

function Show-Menu
{
     param (
           [string]$Title = 'Choose Environment'
     )
     Write-Host "================ $Title ================"
     Write-Host "1: Press '1' for Production."
     Write-Host "2: Press '2' for TestDev."
     Write-Host "3: Press '3' for VDI."
     Write-Host "4: Press '4' for all."
     Write-Host "Q: Press 'Q' to quit."
}

Next, I created arrays that stored my vCenter FQDNs.

$production = @( 
    "prodvc1.company.com",
    "prodvc2.company.com",
    "prodvc3.company.com" 
);
$testdev = @( 
    "testvc1.company.com",
    "testvc2.company.com",
    "testvc3.company.com" 
);
$vdi = @( 
    "vdivc1.company.com",
    "vdivc2.company.com" 
);

Now that we have those, we can actually run the menu function to allow the user to pick their environment. With their input, I populate a new variable with the vCenters they want to get to. In the case of the ‘all’ option, I am concatenating them all together in the one variable.

Show-Menu
     $input = Read-Host "Please make a selection"
     switch ($input)
     {
             '1' {
                'You chose Production.'
                $hosts = $production
           } '2' {
                'You chose TestDev.'
                $hosts = $testdev
           } '3' {
                'You chose VDI.'
                $hosts = $vdi
           } '4' {
                'You chose all!'
                $hosts = $production + $testdev + $vdi
           } 'q' {
                return
           }
     }

I am also requiring the user to enter their credentials for the connection string. I explored using a stored credential in a password vault or on my machine, but I like having the flexability and portability of the script just using the credentials that the user requires for that job.

$cred = Get-Credential

Finally, we actually make the connections.

Connect-VIServer -Server $hosts -Credential $cred

And that’s it. A very simple, incredibly useful script that I use pretty much every day. Find this entire script and more in the Sheetz GitHub repo.

-Willie