# # $JavaRoot = $env:ProgramFiles+"\Java" $NewestJstackPath = $null if (Test-Path $JavaRoot) { # get version of newest available jstack.exe $NewestJstackVersion=Get-ChildItem $JavaRoot"\jstack.exe" -Recurse|foreach-object{([System.Diagnostics.FileVersionInfo]::GetVersionInfo($_).FileVersion)}|%{new-object System.Version($_)}|sort|select -last 1 # store full path to $NewestJstackVersion in $NewestJstackPath Get-ChildItem $JavaRoot"\jstack.exe" -Recurse|foreach-object{if ([System.Diagnostics.FileVersionInfo]::GetVersionInfo($_).FileVersion -eq $NewestJstackVersion){ $NewestJstackPath=$_.FullName write-host "Newest installed JSTACK.EXE version=$NewestJstackVersion has been located in $NewestJstackPath and will be used to create jstack dumps for all running Apache Tomcat services." } } } if ($NewestJstackPath) { # get all tomcat services, currently running $RunningTomcats=get-wmiobject win32_service|where{$_.name -like '*tomcat*' -and $_.state -eq 'Running'} if ($RunningTomcats) { foreach ($Tomcat in $RunningTomcats) { write-host "Found running Apache Tomcat service:"$tomcat.name -ForegroundColor Green # silently create dumpfolder on dekstop if it not already exists $dumpfolder=[Environment]::GetFolderPath("Desktop")+"\TomcatDumps" if (test-path $dumpfolder) { } else { mkdir $dumpfolder|Out-Null } # export a registry file in dump folder, analog to log collector tool & regedit /E $dumpfolder\TomcatRegistrySettings.log "HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Apache Software Foundation" # generate path for stack dump $OutFileStack=[Environment]::GetFolderPath("Desktop")+"\TomcatDumps\Dump_jstack_"+$Tomcat.name+".txt" write-host "Dumping stack to $OutFileStack" # call jstack and create the stack dump & $NewestJstackPath -l $Tomcat.ProcessId >$OutFileStack # find path to jmap.exe in same folder (and hence version) as jmem.exe $NewestJmapPath=(split-path -Parent $NewestJstackPath)+"\jmap.exe" if (test-path $NewestJmapPath) { # if possible, dump heap. Otherwise just ignore heap. $OutFileHeap=[Environment]::GetFolderPath("Desktop")+"\TomcatDumps\Dump_heap_"+$Tomcat.name+".hprof" if (test-path $OutFileHeap) {del $OutFileHeap} & $NewestJmapPath -dump:format=b,file=$OutFileHeap $Tomcat.ProcessId } } # compress, and then delete dumpfolder if compression succeeded. Otherwise, leave dumpfolder on desktop. write-host "Compressing..." if (test-path $dumpfolder".zip"){del $dumpfolder".zip"} Compress-Archive -Path $dumpfolder -DestinationPath $dumpfolder".zip" if (test-path $dumpfolder".zip"){rd $dumpfolder -r -Force} write-host "Java stack trace, Java heap dump, and Tomcat registry parameters has been placed in $dumpfolder" -ForegroundColor Green } else { write-host "Could not find any running services with a name like *tomcat*" -ForegroundColor Red } } else { write-host "Could not find any version of JSTACK.EXE" -ForegroundColor Red write-host "You can copy the Java SE Development Kit installation folder, for example, C:\Program Files\Java\jre1.8.0_171 from another computer to this computer." -ForegroundColor yellow write-host "Warning: Do not install Java SE Development Kit on servers that contain Tomcat instances, deployed with Zylinc Deployment Manager. Your Deployment Manager configuration can be damaged, and this can prevent your existing Tomcat services from starting." -ForegroundColor yellow } # #