Skip to content

Instantly share code, notes, and snippets.

@mattcorr
Created June 10, 2015 00:06
Show Gist options
  • Save mattcorr/cd31a960609d207a5f5e to your computer and use it in GitHub Desktop.
Save mattcorr/cd31a960609d207a5f5e to your computer and use it in GitHub Desktop.
param ( [string]
[ValidateSet("Confluence", "Jira")]
$Application,
[string]
$DumpFolder = "N:\temp"
)
$currentDate = Get-Date
$PSExecLocation = "N:\Utilities\PSTools\PsExec.exe"
$JstackLocation = "N:\Utilities\JDK\bin\jstack.exe"
$dumpFile = "$DumpFolder\$Application-memdump-$($currentDate.ToString("yyyy-MM-dd-HHmmss")).txt"
# perform some checks first
if (!(Test-Path $DumpFolder -PathType Container))
{
Write-Host "Folder $DumpFolder does not exist. Please create and try again." -f Red
return
}
if (!(Test-Path $PSExecLocation -PathType Leaf))
{
Write-Host "File $PSExecLocation does not exist. Please investigate and try again." -f Red
return
}
if (!(Test-Path $JstackLocation -PathType Leaf))
{
Write-Host "File $JstackLocation does not exist. Please investigate and try again." -f Red
return
}
#determine the Process ID
$ProcessID = Get-Process tomcat7 |Where-Object {$_.Path -ilike "*$Application*"} | Select-Object Id
if ($ProcessID -eq $null)
{
Write-Host "Unable to determine the process Id for $Application. Please confirm it is running and try again." -f Red
return
}
# if we get to here, assume all the bits are in place and run the command
Write-Host "Generating memory dump for $Application with PID $($ProcessID.Id) and storing in file $dumpFile" -f Cyan
# PSexec writes to STDERR so need to pipe it to stop it from outputting red text :P
$output = [string] (& $PSExecLocation -s $JstackLocation -l $ProcessID.Id 2>&1 | Out-File $dumpFile -Encoding utf8 )
Write-Host "All Done." -f Cyan
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment