Work with VMware vCenter connections


#1

There was one annoying error i got when working with vCenter connections and UD:

You have  modified the global:DefaultVIServer and global:DefaultVIServers system variables. This is not allowed. 
Please reset them to $null and reconnect to the vSphere server.

This happens because the default settings of PowerCli is to only accept one connection to one vCenter server.

Here is my way to work around that:

  1. Allow multiple vCenter server connections
$null = Set-PowerCLIConfiguration -DefaultVIServerMode Multiple -Scope User -Confirm:$false
  1. create a scheduled endpoint to first connect to your vCenter servers or check that the cennections is established.
    This will save the $global:DefaultVIServer variable to your UD cache. (it contais the SessionSecret you need to reconnect to that connection)
$Cache:vCenterServer = 'virtualcenter.yourdomain.com'
if (!($global:DefaultVIServer.Name -eq $Cache:vCenterServer)){
        try{
            Connect-VIServer -Server $Cache:vCenterServer -Credential $Cache:Creds -ErrorAction Stop
        }
        catch{
            $err = $_.Exception.Message
        }
    }
$Cache:ViServerList = $global:DefaultVIServer
  1. everywhere on your site where you want to access the data you need to open the established connection via the SessionSecret:
$null = Connect-VIServer -Server $Cache:ViServerList.Name -Session $Cache:ViServerList.SessionSecret
$hostlist = Get-VMHost
  1. Never use “Disconnect-ViServer”, the connection needs to be open all the time.

Complete demo code:


Get-UDDashboard | Stop-UDDashboard

$null = Set-PowerCLIConfiguration -DefaultVIServerMode Multiple -Scope User -Confirm:$false
$Cache:Creds = Get-Credential -Credential $(whoami)

#####
$Every60Sec = New-UDEndpointSchedule -Every 60 -Second
$Schedule = New-UDEndpoint -Schedule $Every60Sec -Endpoint {

    $Cache:vCenterServer = 'virtualcenter.yourdomain.com'
    if (!($global:DefaultVIServer.Name -eq $Cache:vCenterServer)){
        try{
            Connect-VIServer -Server $Cache:vCenterServer -Credential $Cache:Creds -ErrorAction Stop
        }
        catch{
            $err = $_.Exception.Message
        }
    }
    $Cache:ViServerList = $global:DefaultVIServer
}

$pages = @()

$pages += New-UDPage -Name "home" -Content {

    New-UdGrid -Title "First 2 Hosts" -Headers @("Name", "ConnectionState", 'PowerState') -Properties @("Name", "ConnectionState", 'PowerState') -Endpoint {
        $null = Connect-VIServer -Server $Cache:ViServerList.Name -Session $Cache:ViServerList.SessionSecret

        $hostlist = Get-VMHost | select -First 2 | select Name,ConnectionState,PowerState 
        $hostlist | Out-UDGridData
    } -AutoRefresh -RefreshInterval 60
}
$pages += New-UDPage -Name 'Dashboard Status' -Content {
    New-UDGrid -Title "vcenter connect" -Headers @('Name','IsConnected') -Properties @('Name','IsConnected') -Endpoint {
      $Cache:ViServerList | Out-UDGridData
    } -AutoRefresh -RefreshInterval 30
}

$ei = New-UDEndpointInitialization -Module @("C:\Program Files\WindowsPowerShell\Modules\VMware.VimAutomation.Core\10.1.0.8344055\VMware.VimAutomation.Core.psm1")
$Dashboard = New-UDDashboard -Title 'vmware test' -Page $pages -EndpointInitialization $ei
Start-UDDashboard -Port 10001 -Dashboard $Dashboard -Endpoint @($Schedule)

Automation and reporting dashbaord for VMware and Hyper-V systems