getcapid.ps1 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # Licensed Materials - Property of IBM
  2. #
  3. # IBM Cognos Products: btsv
  4. #
  5. # (C) Copyright IBM Corp. 2016, 2017
  6. #
  7. # US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  8. #
  9. # Get caRoot since this script locates in $caRoot/bin or $caRoot/bin64.
  10. $curPSScriptRoot = split-path -parent $MyInvocation.MyCommand.Definition
  11. $caRoot = Split-Path -Path $curPSScriptRoot
  12. $caPIDFile = "$caRoot\logs\capids.log"
  13. $wlpBootPIDFile = "$caRoot\logs\wlp_cogbootstrap_service.pid"
  14. $allPIDFile = "$caRoot\logs\allpids.log"
  15. # Function for get children PIDs. pid and name will be put into caPIDFile
  16. Function GetChildPID {
  17. Param($aPID)
  18. $allProc | Where { $_.ParentProcessID -eq $aPID} | ForEach-Object {
  19. $aPName = '{0}' -f $_.Name
  20. if ( $aPName -ne "conhost.exe") {
  21. $pidName = '{0} {1}' -f $_.ProcessID, $_.Name | Out-File -append $caPIDFile
  22. }
  23. # Recurse call
  24. GetChildPID -aPID $_.ProcessID
  25. }
  26. }
  27. # Main starts here
  28. # Delete $caPIDFile if it exists
  29. if ( Test-Path -Path $caPIDFile) {
  30. del $caPIDFile
  31. }
  32. # Check the existence of $wlpBootPIDFile. If yes get content else exit
  33. if ( Test-Path -Path $wlpBootPIDFile) {
  34. $bootPID = Get-Content $wlpBootPIDFile
  35. if ( !$bootPID ) {
  36. exit 0
  37. }
  38. }
  39. else {
  40. exit 0
  41. }
  42. # Delete $allPIDFile if it exists
  43. if ( Test-Path -Path $allPIDFile) {
  44. del $allPIDFile
  45. }
  46. # Put $bootPID and process name into $caPIDFile
  47. $bootProc = Get-Process -id $bootPID
  48. if ( $bootProc -ne $null ) {
  49. $pidName = '{0} {1}' -f $bootProc.Id, $bootProc.ProcessName | Out-File -append $caPIDFile
  50. }
  51. # Get information for all processes
  52. $allProc = Get-WmiObject -Class Win32_process
  53. # Get information for all processes
  54. $CAPath = [regex]::escape($caRoot)
  55. $allProc | ForEach-Object {
  56. $procInfo = '{0} {1} {2} {3}' -f $_.ProcessID, $_.ParentProcessID, $_.Name, $_.Path
  57. $procPath = $_.Path
  58. if ( $procPath -cmatch $CAPath ) {
  59. "-- $procInfo" | Out-File -append $allPIDFile
  60. }
  61. else {
  62. "$procInfo" | Out-File -append $allPIDFile
  63. }
  64. }
  65. # Call function GetChildPID
  66. GetChildPID -aPID $bootPID