Robb Hill
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
It's stopping/starting them one by one.
The path should be determined automatically from the running instance.
The path should be determined automatically from the running instance.
ASKER
IF the process is running on another server...how would that work?
ASKER
THe powershell would be executed as a job from the SQL Server and the path of the process.exe would be on another server.
ASKER
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.Proces s (cadoc):Process) [Stop-Process], Proce
ssCommandException
+ FullyQualifiedErrorId : CouldNotStopProcess,Micros oft.PowerS hell.Comma nds.StopPr ocessComma nd
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], ParameterBindingValidation Exception
+ FullyQualifiedErrorId : ParameterArgumentValidatio nError,Mic rosoft.Pow erShell.Co mmands.Sta rtProcessC om
mand
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.Proces
ssCommandException
+ FullyQualifiedErrorId : CouldNotStopProcess,Micros
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], ParameterBindingValidation
+ FullyQualifiedErrorId : ParameterArgumentValidatio
mand
ASKER
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='RemoteServe r',
$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"
}
}
Also not sure how to get this behavior to work as the one above would have.
[cmdletbinding()]
param(
$ComputerName='RemoteServe
$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"
}
}
ASKER
this is failing because I have to ruemote the user interaction part
ASKER
Thanks for the help.
This did not work for a remote server but it did help me to a solution.
This did not work for a remote server but it did help me to a solution.
ASKER
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