Skip to content

Instantly share code, notes, and snippets.

@rdronov
Created April 22, 2019 04:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rdronov/f776b3d130bde9e3e1a27fc3b265e9eb to your computer and use it in GitHub Desktop.
Save rdronov/f776b3d130bde9e3e1a27fc3b265e9eb to your computer and use it in GitHub Desktop.
# File name: Remove-VMHostCoredumpLocation.ps1
# Description: This script removes a core dump file for the particular ESXi host.
#
# 11/03/2019 - Version 1.0
# - Initial release
#
# Author: Roman Dronov (c)
# Define common functions
function ex {exit}
# Get the host name and check it is valid
$vmhosts = Get-VMHost | ? {$_.ConnectionState -eq "Connected" -or $_.ConnectionState -eq "Maintenance"} | ForEach-Object {$_.Name.Split('.')[0]}
$vmhost = (Read-Host -Prompt "`n Please type in the ESXi host name").Split('.')[0]
While ($vmhosts.Contains("$vmhost") -ne "True") {
Write-Host "`n Checking the host exists..." -NoNewline
Write-Host " The host is not reachable." -ForegroundColor Yellow
$vmhost = Read-Host -Prompt "`n Please type in the host name correctly"
}
$vmhost = $vmhost + "*"
# Get the system configuration
$esxcli2 = Get-EsxCli -VMHost $vmhost -V2
# Activate the current coredump (this is to identify it properly later in this script)
Write-Host "`n Searching for a coredump file and trying to activat it..." -NoNewline
$arguments = $esxcli2.system.coredump.file.set.CreateArgs()
$arguments.Item('enable') = $true
$arguments.Item('smart') = $true
Try {
$activation = $esxcli2.system.coredump.file.set.Invoke($arguments)
}
Catch [Exception]{
Write-Host " File doen't exist!" -ForegroundColor Yellow
}
# Get the current coredump configuration
$dumpConfigured = $esxcli2.system.coredump.file.get.Invoke().Configured
# Prompt for the coredump removal
If ($dumpConfigured -ne ''){
Write-Host " File exists." -ForegroundColor Green
Write-Host "`n Current configuration: $dumpConfigured"
$choice = $null
While ("Yes","No" -notcontains $choice) {
$choice = Read-Host -Prompt "`n Would you like to remove this file? (Yes/No)"
}
Switch ($choice){
"Yes" {
# Remove the coredump file
Write-Host " Removing the old coredump file..." -NoNewline
$arguments = $esxcli2.system.coredump.file.remove.CreateArgs()
$arguments.Item('force') = $true
$arguments.Item('file') = "$dumpConfigured"
$remove = $esxcli2.system.coredump.file.remove.Invoke($arguments)
Write-Host " Done!" -ForegroundColor Green
}
"No" {
# Exit this script
Write-Host "`n Exiting..."
ex
}
}
}
Write-Host "`n Exiting..."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment