@Olaf I have the id of any of the specified processes in $ nid. Even if there is only one worker process, its id is passed into a variable, then a verbose tells me that it is ending this process and then waits for that process to finish.
If there are no running processes, I get a null value, which is processed by the condition, and if the condition tells me that it has a null value, then I can be sure that there are no running processes and move on to another script block to clean up folders.
something is wrong?
The only thing is, when I tested on a test server, verbose says it stop the process and wait-process throws an error that it cannot find the process.
otherwise everything seems to be going as it should.
VERBOSE: Performing the operation “Stop-Process” on target “notepad (15812)”.
VERBOSE: Performing the operation “Stop-Process” on target “notepad (36936)”.
Cannot find a process with the process identifier 15812.
+ CategoryInfo : ObjectNotFound: (15812:Int32) [Wait-Process], ProcessCommandException
+ FullyQualifiedErrorId : NoProcessFoundForGivenId,Microsoft.PowerShell.Commands.WaitProcessCommand
+ PSComputerName : server1
Cannot find a process with the process identifier 36936.
+ CategoryInfo : ObjectNotFound: (36936:Int32) [Wait-Process], ProcessCommandException
+ FullyQualifiedErrorId : NoProcessFoundForGivenId,Microsoft.PowerShell.Commands.WaitProcessCommand
+ PSComputerName : server1
@Olaf Hi.
the script that we have made with you, for some reason, does not work correctly. I have a condition - we terminate the processes and wait for their completion. then, after completion, the process of cleaning the folders begins, but for some reason the folders began to be cleaned even before all processes were stopped. the script started flooding with access denied errors. I went to the server and saw that indeed some processes have not yet completed. I do not understand what is the reason. folders had to be cleared only after all processes were stopped. help me please.
Your script looks way to convoluted to me. I’d try to focus on the core function first and add some eye candy later on if the core function works robustly.
Try it with one server first, plan a downtime, open the folders in separate explorer windows and watch what happens. If that worked add another server and so on …
You can do what ever you think is needed but for me it sounds more and more that you rather should contact the vendor of the software you’re dealing with. It shouldn’t be necessary to cleanup temporary folders regularily.
I just noticed now that you also stop some service in your script. I actually missed that before. You may add the needed Stop-Service commands to your script if needed and try again. But you should keep in mind that some services do get restarted when they get stopped.
yes, the services are stopped and after that the worker processes remain. therefore, these processes are terminated forcibly. And then the folders should be cleaned up.
Unfortunately, we need to do this every day, because without these actions, the cluster starts to work incorrectly. for now, we can’t do without cleaning up temp folders on a daily basis.
still don’t understand why folders start to be cleaned up before stopping processes if Wait-Process is specified?
@Olaf I run the script from a remote PC, there are no problems with running the script. He works on my behalf, and I am the administrator of these servers.
other employees do it manually at night, go to each server, stop the services, then close the remaining processes, then clear the specified folders, and then start the services again.
@Olaf the script works, but on one server out of 5, folders are cleaned up before all processes are stopped. Since the processes are still there, the folder on this server is busy and does not allow for cleanup. because of this, we will have to manually clean. This is the question, why does the folder start to be cleaned without waiting for | Wait-Process
the only problem remained with this.
So you don’t have a PowerShell issue or a script issue - you have an issue with this server I’d say.
I don’t know if it’s possible to help you troublesooting with this particular issue.
@Olaf Okay, I’ll try to add a couple of loops that will check all processes for completion and that the folders are empty.
but the problem is not with the server, but that the script does not wait for the processes to finish, although I tell it to wait.