Work with VMware vCenter connections

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

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!

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)
1 Like

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

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?

Hi! I’m using your test dashboard with a few tweaks to fit my environment. Its working fine. When I use “Publish-UDDashboard”, I cannot get it to work. It does run fine if I execute it from Powershell directly.
Do you know what might be happening?

Hi,
I try to get this script work on IIS, but i only get 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.

Has anybody got the vmware connection working on IIS?

My adapted Code is:

$null = Set-PowerCLIConfiguration -DefaultVIServerMode Multiple -Scope User -Confirm:$false
#$Cache:Creds = Get-Credential -Credential $(whoami)
$cache:secpasswd = ConvertTo-SecureString "Pa$$w0rd" -AsPlainText -Force
$cache:VCCredentials = New-Object System.Management.Automation.PSCredential ("robert.priedl@network.local", $cache:secpasswd)
$Cache:Creds = $cache:VCCredentials
#####
$Every60Sec = New-UDEndpointSchedule -Every 60 -Second
$Schedule = New-UDEndpoint -Schedule $Every60Sec -Endpoint {

    $Cache:vCenterServer = 'vcenter.network.local'
    if (!($global:DefaultVIServer.Name -eq $Cache:vCenterServer)){
        try{
            Connect-VIServer -Server $Cache:vCenterServer -Credential $Cache:Creds -ErrorAction Stop
        }
        catch{
            $err = $_.Exception.Message
            $err | Out-File -FilePath c:\temp\testvm2.log -Append
        }
    }
    $Cache:ViServerList = $global:DefaultVIServer
}
Enable-UDLogging -Level Info -FilePath c:\temp\testvmudlog.log
$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 -Dashboard $Dashboard -wait -AdminMode -Endpoint @($Schedule)

I’ve found the error(s)
1.) I’ve installed the PowerCLI on the server and didn’t reboot the server
2.) The module installed globaly under %programfiles% and the appPool user was set to standard. With an appPool User with local admin rights the Module and the dashboard are working!