forcestop.ps1 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # Licensed Materials - Property of IBM
  2. #
  3. # IBM Cognos Products: btsv
  4. #
  5. # (C) Copyright IBM Corp. 2016
  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. $logFile = "$caRoot\logs\forcestop.log"
  13. $caPIDFile = "$caRoot\logs\capids.log"
  14. # Check the existence of $caPIDFile. If yes get content else exit
  15. if ( Test-Path -Path $caPIDFile) {
  16. $allCAProc = Get-Content $caPIDFile
  17. if ( !$allCAProc ) {
  18. exit 0
  19. }
  20. }
  21. else {
  22. exit 0
  23. }
  24. # Delete $logFile if it exists
  25. if ( Test-Path -Path $logFile) {
  26. del $logFile
  27. }
  28. # Get date time for logging
  29. $dateTime = Get-Date -format s
  30. # For each process in $caPIDFile, if it still running and if its name still the same as before, log a message and forcefully stop
  31. $index = 1
  32. foreach ( $aCAProc in $allCAProc ) {
  33. $aCAPid = $aCAProc.Split(' ')[0]
  34. $aCAPName = $aCAProc.Split(' ')[1]
  35. $procInfo = gwmi win32_process | Where-Object { $_.ProcessID -eq $aCAPid } | % { "$($_.ProcessID) $($_.Name) $($_.ParentProcessID) $($_.GetOwner().User)"}
  36. if ( $procInfo ) {
  37. if ( $index -eq 1 ) {
  38. $msg = "$dateTime The following processes will be shutdown forcefully`r`n" | Out-File -append $logFile
  39. $index = $index + 1
  40. }
  41. foreach ($line in $procInfo) {
  42. $procName = $line.Split(' ')[1]
  43. if ( ($procName -ne $null) -and ($aCAPName -ne $null) -and ($procName -eq $aCAPName)) {
  44. $msg = "$line" | Out-File -append $logFile
  45. stop-process -id $aCAPid -Force 2>&1 | Out-File -append $logFile
  46. }
  47. }
  48. }
  49. }