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)
2 Likes

Automation and reporting dashbaord for VMware and Hyper-V systems
PowerShell ESXi Dashboard
#2

Hi Alex,
I am using your complete demo code as a test to connect to a VM on the local network.
I kept getting the error: “Cannot validate argument on parameter ‘Server’. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.” I have installed modules: VMware.PowerCLI and Power-NVM but I am still getting the error even after rebooting. I am not a coder or programmer but I am learning as I go. Any help is greatly appreciated. Thanks!

0 Likes

#3

Hi Tom and welcome,

you need to change line 11

$Cache:vCenterServer = 'virtualcenter.yourdomain.com'

to your vCenter nerver name or IP.

I just checked the functionality with UD 2.3.2. It is working for me.
Here is a version with a little error handling. If it is still not working a screenshot would help us:


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:EndpointError = $false
    $Cache:vCenterServer = 'virtualcenter.yourdomain.com'
    if (!($global:DefaultVIServer.Name -eq $Cache:vCenterServer)){
        try{
            $Cache:VCSession = Connect-VIServer -Server $Cache:vCenterServer -Credential $Cache:Creds -ErrorAction Stop
        }
        catch{
            $Cache:EndpointError = $_.Exception.Message
        }
    }
    $Cache:ViServerList = $global:DefaultVIServer
}

$pages = @()
$pages += New-UDPage -Name "home" -Content {
    New-UDRow {
        New-UDCollection -Content {
            New-UDCollectionItem -Content {"PS Version: $([string]$PSVersionTable.PSVersion)"}
            New-UDCollectionItem -Content {"Host: $($Host.Name)"}
            New-UDCollectionItem -Content {"OS: $((gwmi win32_operatingsystem).caption)"}
            New-UDCollectionItem -Content {".Net Version: $((Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full').Release)"}
        }
    }
    New-UDRow {
        New-UDCard -Title "PowerCli Version" -Content {
            Get-Module -ListAvailable | where{$_.Name -eq 'VMware.VimAutomation.Core'} | foreach{
                New-UDRow{"$($_.Name) - $($_.Version) - $($_.Path)"}
            }
        }
    }
    New-UDRow -Endpoint {
        if(!($Cache:EndpointError)){
            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
        }
        else{
            New-UDCard -Title "vCenter Connect Error:" -Text $Cache:EndpointError
        }
    }
}

$Dashboard = New-UDDashboard -Title 'vmware test' -Page $pages
Start-UDDashboard -Port 10001 -Dashboard $Dashboard -Endpoint @($Schedule)
0 Likes

#4

Hi Alex,
I will check on line 11 again to make sure I got the right address. Thank you again for all your help.

-Tom

0 Likes

#5

Hi Alex,
Big THANKS! I used the recent demo code from your last reply and it actually worked! :smile: Is there a way to connect directly to a particular VM server instead of just the vCenterServer? I input the VM server address on line 11 but it still doesn’t work. Any suggestion?

0 Likes