mystikal1000
asked on
Powershell to end a process on over 1000 servers!
I have a powershell script that will kill/end a process on over 1000 servers. However some servers show RPC is unavailable, how do you skip them automatically?
This is urgent as we are having major problems with our user environement.
I appreciate the help!
This is urgent as we are having major problems with our user environement.
I appreciate the help!
(Get-Content 'c:\Temp\Computers.txt') | ForEach-Object {
Get-WmiObject -computer $_ -class win32_process -filter "name = 'process.exe'" -credential $cred| %{$_.terminate()} | out-null
}
Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At line:2 char:11
+ Get-WmiObject -computer $_ -class win32_process -filter "n ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~
+ CategoryInfo : InvalidOperation: (:) [Get-WmiObject], COMException
+ FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerSh ell.Comman ds.GetWmiO bjectComma nd
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
And there are errors in the log file...same process id but running on a different servers. See below.
Transcript started, output file is C:\Temp\StopProcess.log
Processing 'Server1' ... process terminated.
Processing 'Server2' ...Error: Invalid query "Select * From Win32_Process Where Name="\\Server2\root\cimv2 :Win32_Pro cess.Handl e="33896"" "
Transcript stopped, output file is C:\Temp\StopProcess.log
Transcript started, output file is C:\Temp\StopProcess.log
Processing 'Server1' ... process terminated.
Processing 'Server2' ...Error: Invalid query "Select * From Win32_Process Where Name="\\Server2\root\cimv2
Transcript stopped, output file is C:\Temp\StopProcess.log
Seems like you brought down the EE site ;) - only got "Bad Gateway" for the last 30 minutes.
Anyway, sorry, there were too many variables named "$process"; fixed it above, just download the script again.
Anyway, sorry, there were too many variables named "$process"; fixed it above, just download the script again.
ASKER
Yes I believe it was Cloudfare, it wasn't me.... :)
Thank you so much for your help!
Thank you so much for your help!
hmm, why not running it using the Workflow ForEach -Parallel?
Yes, you need some sort of thread-pool like ForEach Parallels
Alternatively you can do threading via my tool
https://www.experts-exchange.com/articles/30139/How-to-thread-single-threaded-applications.html
Alternatively you can do threading via my tool
https://www.experts-exchange.com/articles/30139/How-to-thread-single-threaded-applications.html
How about wrapping the statement in a Try{} Catch{} structure.
This really is a task that would benefit from PS Remoting. Easier from a firewall standpoint, and supports parallel operations (which can make your operations faster by orders of magnitude). It's possible for a machine to respond to ping, but not have the rules needed for WMI/DCOM/RPC communication (or WSMan - used by PS Remoting), but assuming pings are allowed it's still the simplest way to check whether you are likely to be able to succeed in further communication (I'm only mentioning this as a side note - obda's script does this check already).
this may help:
$computers = get-content ‘c:\temp\computers.txt’
invoke-command -computername $computers -scriptblock {stop-process -name ‘your process name’}
this will send the command to all computers in parallel. No need to wait for a failing one, that info will come back to you later. All responding computers will execute as soon as possible.
$computers = get-content ‘c:\temp\computers.txt’
invoke-command -computername $computers -scriptblock {stop-process -name ‘your process name’}
this will send the command to all computers in parallel. No need to wait for a failing one, that info will come back to you later. All responding computers will execute as soon as possible.
ASKER
Thank you everyone for your suggestions! All is appreciated!
invoke-command -computername $computers -scriptblock {stop-process -name ‘your process name’}
Unless I am mistaken, these commands run one at a time.https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/invoke-command?view=powershell-6
Think it is
$scriptblock = {
Param($server)
#the function
}
$servers | % {Start-Job -Scriptblock $scriptblock -ArgumentList $_ | Out-Null}
Get-Job | Wait-Job | Receive-Job
https://social.technet.microsoft.com/Forums/lync/en-US/9d2df27c-49b1-4a41-85b1-35ec97b0f2da/run-invokecommand-script-on-multiple-computers-parallel?forum=winserverpowershell
@Shaun - I see in the help the line that is causing confusion, but no, running the Invoke-Command command as you and Luc showed will result in it being executed remotely in parallel on the remote machines. There is a -ThrottleLimit parameter that controls the maximum number of concurrent connections (default 32). This is standard fan-out PS Remoting.
From a quick look at the forum post you referenced, the only reason I see they were trying a different approach was the need to use different sets of credentials (for multiple forests).
From a quick look at the forum post you referenced, the only reason I see they were trying a different approach was the need to use different sets of credentials (for multiple forests).
ASKER
Open in new window