Hi guys,
I am trying to create a function which will start and stop a service depending on if a process is running.
The code i have written seems to work but fails after a certain amount of time with the following error:
“The script failed due to call depth overflow”.
in order for my script to work i need to have the function constantly running so that it can find the process and carry out a relevant task. This is why i have done a while loop at the end of the script to keep it running. From doing some research this seems to be my issue, but cant find a way around it.
Any help will be much appreciated
My is below:
function Find-process {
do {
#find BAM process
$processfound = Get-Process -Name BusinessAlertManager -ErrorAction Ignore
#When process is found stop the service (Allows the application to open).
if ($processfound) {
Stop-Service -Name BAMService
}
else {
Start-Service -Name BAMService
}
}
until ($processfound)
#Run continuously on ext and opening of the application
while($true) {
Find-process;
}
}