Robb Hill
asked on
Powershell + stopping and starting on a remote server
I have the following script.which is working...but I need to edit it to do the following.
Currently this script is executed and goes out to a remote server and kills a process.
I need the process to have a wildcard....so instead of process.exe....it would work something like process*
This process will have multiple instances of itself.
I need it to kill one, then start one..ect until finishing the loop.
In summary I am not sure how to make this use the wildcard for process name, terminate and restart the process.
Please help.
Currently this script is executed and goes out to a remote server and kills a process.
I need the process to have a wildcard....so instead of process.exe....it would work something like process*
This process will have multiple instances of itself.
I need it to kill one, then start one..ect until finishing the loop.
In summary I am not sure how to make this use the wildcard for process name, terminate and restart the process.
Please help.
[cmdletbinding()]
param(
$ComputerName='SERVERIC',
$ProcessName='cadoc.exe'
)
$Processes = Get-WmiObject -Class Win32_Process -ComputerName $ComputerName -Filter "name='$ProcessName'"
foreach ($process in $processes) {
$returnval = $process.terminate()
$processid = $process.handle
}
ASKER
Not sure what you have there...how is that going to start and restart the process in the loop?
Also looks as if there was a paste error.
Also looks as if there was a paste error.
Simply pasted your text with a where object.
Second when you say start and stop are you hoping to continually query process for that value until it returns false?
Second when you say start and stop are you hoping to continually query process for that value until it returns false?
ASKER
I basically have a process on a remote server.
Lets say there are 5 running.
I would want to stop one...then restart it.
stop one ...restart it..
until I had looped through all of that process.
A recycle so to speak.
Lets say there are 5 running.
I would want to stop one...then restart it.
stop one ...restart it..
until I had looped through all of that process.
A recycle so to speak.
Ok so I am assuming that either the process won't have the same name or the command line values will be different ?
Here goes if this is what you are after:
Tell me if this is down the path you are thinking.
The foreach actually does what you are asking for, since it goes through them one at a time.
You can add a start-sleep if you want to wait as well.
Here goes if this is what you are after:
#In the event you don't need the full cadoc.exe and you want to wildcard
$ProcessName='cadoc'
)
# The "?" indicates where object and "*" means anything that matches the wildcard value
$Processes = Get-WmiObject -Class Win32_Process -ComputerName $ComputerName | ? {$_.name -like "$process*"}
foreach ($process in $processes) {
$returnval = $process.terminate()
$processid = $process.handle
Start-process $process.commandline
}
Tell me if this is down the path you are thinking.
The foreach actually does what you are asking for, since it goes through them one at a time.
You can add a start-sleep if you want to wait as well.
ASKER
to be exact the process name will be cadoc.exe or cadoc_extranet.exe
Knowing how to do a start sleep would be cool.
Is there a way to kill the process cleanly or is the only way to kill immediately.
Knowing how to do a start sleep would be cool.
Is there a way to kill the process cleanly or is the only way to kill immediately.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
How do I declare the variables properly for this to excecute....I seem to be getting an error.....and this will work remotel right?
ASKER
nevermind...i some how missed your last post...testing now
ASKER
I am executing this from SQL server's powershell option...so I cannot write anything to the screen.
If there was a way to write any info for email that would be cool...but this has to be strictly low level commands as SQL Server is sending this to another server.
If there was a way to write any info for email that would be cool...but this has to be strictly low level commands as SQL Server is sending this to another server.
ASKER
thank you so much....several months of an issue just smoothed over by this powershell...if you ever need .net or something ..hit me up...thanks again
Happy to help sorry I missed your earlier questions.
param(
$ComputerName='SERVERIC',
Open in new window
Simple edit to pass to where.