Connect-VIServer: deep dive or «Where am I connected? »

Cover

VMware PowerCLi – is a powerful management tool and all you need to do to get started with it, is to connect to the virtual infrastructure server(s), which may be the Windows VCenter management server, Linux VCenter Server Appliance (VCSA) or ESXi host.

When you open a new PowerCLi window/console, you will see the installed PowerCLi version in the title bar.

01.Set-PCLiTitle

Connect-VIServer

  • Your first command will be ​​​​Connect-VIServer. Depending on your PowerCLi configuration, you can or cannot connect to multiple virtual infrastructure servers from one session.
Get-Help Connect-VIServer -Online
  • The DefaultVIServerMode setting is responsible for this. You can check it by executing Get-PowerCLIConfiguration cmdlet.
Get-PowerCLIConfiguration -Scope Session | select DefaultVIServerMode
  • The default value is Multiple, i.e. multiple connections allowed from the same session. If for some reason it equals to Single, you can change it by Set-PowerCLIConfiguration cmdlet.
Set-PowerCLIConfiguration -Scope Session -DefaultVIServerMode Multiple

02.Set-PCLiTitle

  • Regardless of this setting, you can always open a few PowerCLi windows/consoles and from each connect to a separate server, but, firstly, it is inconvenient to keep many open consoles, and secondly, you still need to distinguish them somehow (what and where it is connected).

Here you will come to the aid of the function from my PowerCLi Vi-Module module.

Set-PowerCLiTitle

  • Function Set-PowerCLiTitle. The function is very simple and short, but very useful. If VMware uses the PowerCLi window title to show their product version, why do not we use this space for something more informative, because it is always before our eyes and simply must contain something useful?

  • Moreover, what could be more important than the information about the target virtual infrastructure servers for PowerCLi cmdlets!

  • Therefore, all you need to do is just run my function after each change of connection status, i.e., after each use of Connect-VIServer/Disconnect-VIServer cmdlets. As I said, the function is very simple and does not even have a single parameter.

Connect-VIServer VC1 -wa SilentlyContinue
Set-PowerCLiTitle
Disconnect-VIServer -Server VC2
Get-Alias -Definition Set-PowerCLiTitle
title

03.Set-PCLiTitle

  • In the title bar, you will see the server name, the name of the product (VCenter or ESXi host) and its version. All this will be enclosed in square brackets.

  • In the following example, we will connect to one more server and after Set-PowerCLiTitle execution, we will see both servers in the title bar.

04.Set-PCLiTitle

  • In this example, we see that the function Get-VM returns us 1049 VMs. However, if we use the parameter -Server, we see that 61 VMs registered on the server «****10» and 988 VMs on the «****00». From here, we make a very important conclusion, that by default all PowerCLi cmdlets target to all connected VI servers!

  • Now we will disconnect from one of VCenter servers and connect directly to an ESXi host.

05.Set-PCLiTitle

  • After executing the Set-PowerCLiTitle we clearly see that the server «****00» is VCenter, and «****17» is ESXi host. Please note that the cmdlet Connect-VIServer has three more names (Aliases) – Get-ESX, Get-VC and Get-VIServer.
Get-Alias -Definition Connect-VIServer
  • Do not confuse the command Get-ESX and Get-VMHost, the first is to create a connection, and the second to get information about already connected ESXi host(s).

  • Try to avoid direct connection to ESXi hosts without special need, especially when you connected to the VCenter server, where these hosts are registered. Otherwise, you can get the following scenario:

06.Set-PCLiTitle

  • Note that the reference to an object of VM «****98» returned twice by the Get-VM cmdlet without -Server parameter. In the case of Get- cmdlets, it is just getting inadequate information, in the case of the Set-/Add-/Export- etc. the effects can be unpredictable!

  • Train yourself not to close the PowerCLi window/console, without to disconnect from all the servers previously. Test yourself using the Set-PowerCLiTitle.

Disconnect-VIServer -Server * -Force -Confirm:$false
Set-PowerCLiTitle
exit

07.Set-PCLiTitle

  • When you connect to a server, it allocates some resources for each connection. These resources will have freed to the server needs only after «right» disconnect procedure using the Disconnect-VIServer or after the server services restart.

  • Just closing the PowerCLi console, you create a potential resource leak on the server. According to my estimates, each connection to VCenter server «eats» from 100 to 150MB of memory. It is not so little.

Parameter -Server

  • Regarding the -Server parameter, I have repeatedly mentioned. Unfortunately, not all PowerCLi cmdlets support it! Using the following simple code, you can determine whether your command support this parameter or not.

$command = 'Get-VM'
(Get-Command $command).Parameters.Keys | ? {$_ -eq 'server'}
  • The following code will give you all PowerCLi cmdlets that support -Server parameter.
Get-Command -ParameterName server -Module 'VMware*' | select Name | sort Name

08.Set-PCLiTitle

  • We see that the Get-VM cmdlet supports the -Server parameter, but the Add-DeployRule does not.

  • From PowerCLi version 5.5 to version 6.0 Release 3 the situation has not changed much, still more than 100 cmdlets (this is 25-30%) do not allow you to select the target server Emoj.

(Get-PowerCLIVersion).UserFriendlyVersion
$AllCmdlets = (Get-Command -Module 'VMware*').Count
$ServerParam = (Get-Command -ParameterName server -Module 'VMware*').Count
$AllCmdlets - $ServerParam

09.Set-PCLiTitle

10.Set-PCLiTitle

  • Armed with this knowledge, and my function Set-PowerCLiTitle, you will know exactly where will be executed your PowerCLi commands.

  • By the way, note to very interesting parameter -Menu of Connect-VIServer. It will be effective while re-connecting to previously used servers.

Connect-VIServer –Menu

Connect-SrmServer

From version 1.1 Set-PowerCLiTitle supports SRM servers too.

Get-Help Connect-SrmServer -Online
Connect-SrmServer -Server SRM1
$global:DefaultSrmServers
Set-ViMPowerCLiTitle

11.Set-PCLiTitle

12.Set-PCLiTitle

7 thoughts on “Connect-VIServer: deep dive or «Where am I connected? »

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s