Alias for Connect-VIServer arguments – PowerShell functions

I think most of you agree with me when I say that typing “Connect-VIServer -Server name-of-server” is very annoying. Especially when you have many vCenter Servers to manage. First thing that comes to everybody’s mind is “hey, we can use PowerShell aliases”! And yes, but only for shorten cmdlet. You can have some problems with passing parameters into the alias. But don’t worry, there is another – better in my opinion – way to do that. Functions. It’s a simple way for creating aliases for Connect-VIServer arguments.

One does not simply

Without further ado, there you are a template of the mentioned functions:

PowerShell Profiles types

Ok, so you have functions, but what should you do with them? The answer is – insert them into any PowerShell Profile. Following the PowerShell documentation (here, here and here), there are some types of the profiles:

Type Variable Path
AllUsers, AllHosts  $PROFILE.AllUsersAllHosts $PsHome\profile.ps1
AllUsers, CurrentHost  $PROFILE.AllUsersCurrentHost $PsHome\Microsoft.PowerShell_profile.ps1
CurrentUser, AllHosts  $PROFILE.CurrentUserAllHosts $HOME\Documents\WindowsPowerShell\ profile.ps1
CurrentUser, CurrentHost  $PROFILE / $PROFILE.CurrentUserCurrentHost  $HOME\Documents\WindowsPowerShell\ Microsoft.PowerShell_profile.ps1

In the mentioned documentation you can find the way how you can check existence of your profiles files and create them if you don’t have any. In this post the most important question is “how to create alias Connect-VIServer arguments with functions”. For that, you should – after you ensure that chosen profile exists – open the profile file with any text editor. You can do this via PowerShell with the following command (example for the CurrentUser, CurrentHost):

notepad $PROFILE

Then you can paste functions, customizing them before. From now, every time you run a new PowerCLI instance, you will have loaded your functions, ready to use. Instead of typing the whole cmdlet, simple “visrv1” will be enough.

Functions customization

My functions are only the templates, which you can customize for your necessities. You can add specific users or parameters, like:

function visrv {
  Param($server,$user)
  Connect-VIServer -Server $server -User $user 
}

And that’s all! Just try it on your own – choose the best profile type, add functions and enjoy!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.