Link to home
Start Free TrialLog in
Avatar of Robb Hill
Robb HillFlag for United States of America

asked on

Powershell

I need a powershell script that will do the following:


Whenever executed I would want it to go out and exit any process of a given name...
So in this case lets say the programs were all called generic.exe.  I would want it to kill any exe with that name or that had that name in the beginning of it...such as generic_2.exe would also get killed.

Then I would want it to start as many as it closed.


Ideally I would think it was awesome if it could do the following.

Lets say I had 5 that fit this criteria.

I would want it to kill, open,kill,open,kill, open...etc any it had finished the list...instead of killing them all at one time and restarting all at one time..this would be cleaner for how I am trying to approach this.


Please help.
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Robb Hill

ASKER

I will be executing this inside of SQL Server...so It will need to be executed by some sql agent.  I am guessing an admin account that has access to my other server.  

Also the path would be some share from the sql server to my application server.

Where would I put that in the script.

And is this stopping them all at once or is it stopping/starting/stopping/starting as it iteratates all the processes.
Avatar of oBdA
oBdA

It's stopping/starting them one by one.
The path should be determined automatically from the running instance.
IF the process is running on another server...how would that work?
THe powershell would be executed as a job from the SQL Server and the path of the process.exe would be on another server.
I get these as the first two errors in the loop...and this is just running it in powershell on the local server.

Stop-Process : Cannot stop process "cadoc (4120)" because of the following error: Access is denied
At line:3 char:2
+     Stop-Process -Id $_.Id -Force
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (System.Diagnostics.Process (cadoc):Process) [Stop-Process], Proce
   ssCommandException
    + FullyQualifiedErrorId : CouldNotStopProcess,Microsoft.PowerShell.Commands.StopProcessCommand
 
Start-Process : Cannot validate argument on parameter 'FilePath'. The argument is null or empty. Provide an
argument that is not null or empty, and then try the command again.
At line:4 char:26
+     Start-Process -FilePath $Path
+                             ~~~~~
    + CategoryInfo          : InvalidData: (:) [Start-Process], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.StartProcessCom
   mand
I have this..but this is only killing one..then I get an error...its also not restarting as we had in the original requirement.  I would need this to not be process.exe but no how to do all exe that start with process.
Also not sure how to get this behavior to work as the one above would have.

[cmdletbinding()]
param(
  $ComputerName='RemoteServer',
 
  $ProcessName='process.exe'
)
$Processes = Get-WmiObject -Class Win32_Process -ComputerName $ComputerName -Filter "name='$ProcessName'"
 
foreach ($process in $processes) {
  $returnval = $process.terminate()
  $processid = $process.handle
 
if($returnval.returnvalue -eq 0) {
  write-host "The process $ProcessName `($processid`) terminated successfully"
}
else {
  write-host "The process $ProcessName `($processid`) termination has some problems"
}
}
this is failing because I have to ruemote the user interaction part
Thanks for the help.

This did not work for a remote server but it did help me to a solution.