Manually collecting log bundles from vCenter and hosts can be repetitive and time-consuming. Here’s how I automated the process with PowerCLI. I wanted a short script that would let me pick a vCenter server, then do the other relevant tasks:
- Export the vCenter log bundle
- Pick any single cluster & export a log bundle from the first three hosts in the cluster
Troubleshooting: Underlying connection was closed
When I attempted to use the PowerCLI Get-Log
command to generate a bundle, I was met with the error: The underlying connection was closed: A connection that was expected to be
This led me to a knowledge base article with resolved the issue:
kept alive was closed by the server.
Set-PowerCLIConfiguration -WebOperationTimeoutSeconds 2700 -Scope:User
This sets the timeout for such operations from a default 5 minutes to 45 minutes (2700 seconds). After setting the timeout for the user, I needed to exit and relaunch my powershell window for the setting to become effective.
Sample Script
The following sample script assumes you are connected to a vCenter Server to which you’d like to collect logs.
$dateString = (Get-Date).ToString('yyyyMMdd')
$newFolder = (New-Item "D:\tmp\VCLogExport\$dateString" -ItemType Directory).FullName
Get-Log -Bundle -DestinationPath $newFolder
Get-Cluster | Get-Random -Count 1 | Get-VMHost | Sort-Object Name | Select-Object -First 3 | Get-Log -Bundle -DestinationPath $newFolder
The few lines of code will:
- Create an output folder with the current date
- Export a vCenter log bundle
- Select a single cluster at random, select the first three hosts from that cluster and create a log bundle for each of them. Note: If the selected cluster has fewer than 3 hosts, fewer than 3 bundles may be created.
Conclusion
By increasing the PowerCLI timeout and using just a few lines of script, what was once a repetitive monthly manual process can now be automated and kicked off in just a few minutes. The log bundles will still take some time to generate, but this will happen in the background without attention. This approach not only saves time but also ensures consistency in how log bundles are collected for troubleshooting or archival purposes.