Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Code runs in ISE but not in powershell command window

$
0
0

I have created a script to check for java deadlocks as soon as they occur but it will only run correctly in the ISE. Any assistance to identify why would be appreciated. I have only written a couple of simple powershell scripts previously.

#deadlock_check.ps1
param ($folder='C:\temp\ibm', $delay=10)

$Version = 'Version 1.0 25/11/2019'


<#This script makes use of FileSystemWatcher to detect when javacore files are created 
  and then checks the core file for notification of a deadlock situation. Any deadlocks 
  found are recorded in a log file for Splunk to consume and raise as an alert.

  This script uses an endless While loop, to terminate the process delete the lock file 
  defined below as $LockFile to ensure the cleanup code runs.
#>

$Text='This is a lockfile, delete to stop process'
$LockFile='C:\temp\ibm\lockfile.txt'
$LogFile='C:\temp\ibm\Logs\deadlock_check.log'
#$folder = 'C:\temp\ibm' #'c:\ibm\WebSphere\AppServer\profiles\AppSrv01' OR 'c:\ibm\WebSphere\AppServer\profiles\Custom01'
$filter = 'javacore.*'                             
#$delay=3
    
function Get-TimeStamp {
    
return "[{0:dd/MM/yyyy} {0:HH:mm:ss:ms}]" -f (Get-Date)
}

#Delete the $LockFile if it exists to end any running copy of this script and give it time for cleanup.
if(test-path -PATH $LockFile ){ Remove-Item $LockFile }
Start-Sleep -Seconds $delay
#Create a new $LockFile to ensure the while loop doesn't terminate immeadiatly.
$Text > $LockFile
#Clear the log file on startup. Splunk should be ingesting so we have history if required.
Write-Output "$(Get-TimeStamp) INFO:Starting deadlock_check.ps1 on $env:computername" | Out-file $LogFile  -Encoding Unicode 

$fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{
        IncludeSubdirectories = $false              
        NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'
        }

$onCreated = Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {
        $path = $Event.SourceEventArgs.FullPath
        $name = $Event.SourceEventArgs.Name
        $changeType = $Event.SourceEventArgs.ChangeType
        $timeStamp = $Event.TimeGenerated
        Write-Output "$(Get-TimeStamp) INFO:The file '$path' was $changeType at $timeStamp on $env:computername" |
        Out-file $LogFile  -Encoding Unicode -Append
        Write-Host "The file '$path' was $changeType at $timeStamp"
        Start-Sleep -Seconds $delay # A slight delay may be neccessary to ensure the file is completly written before checking for the deadlock string!
        if (Select-String -Path $path -Pattern 'Deadlock detected'){
        Write-Output "$(Get-TimeStamp) ERROR:Deadlock condition detected in $path on $env:computername" |
        Out-file $LogFile  -Encoding Unicode -Append
        Write-Host "DeadLock!"}
        }


While (test-path -PATH $LockFile ){sleep -m 100}

#Cleanup
Unregister-Event FileCreated 
Write-Output "$(Get-TimeStamp) INFO:Stoping deadlock_check.ps1 on $env:computername" | Out-file $LogFile  -Encoding Unicode -Append


Viewing all articles
Browse latest Browse all 21975

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>