Reputation: 271
I have a confusing issue here. I have created a script that checks a bunch of machines for a running instance of JAVAW.
$computers = Get-Content C:\computers.txt
foreach ($computer in $computers){
Get-Process -ComputerName $computers -Name Javaw | select machinename, id, ProcessName
}
It certainly finds the JAVAW process running on some of the machines (as expected) in the TXT file but repeats the results in some kind of loop. I would have expected it to only report each machine that's running JAVAW once. Its not reporting different instances of JAVAW on the same machines either, the PID is the same. So for example on one machine it reports JAVAW PID 1612 5 times.
So the output looks like this:
computer1 1612 javaw
computer2 1964 javaw
computer3 8448 javaw
computer1 1612 javaw
computer2 1964 javaw
computer3 8448 javaw
computer1 1612 javaw
computer2 1964 javaw
computer3 8448 javaw
etc
Any help would be greatly appreciated.
Upvotes: 0
Views: 510
Reputation: 28154
You're scanning every computer X
times, where X
is the number of computers you in computers.txt
.
Get-Process
's -Computername
parameter accepts a collection of computer names and when you pass it a collection, it pulls the process list for each of those computers. You're passing $computers
but you meant to pass $computer
- the "iterator" for each time through the loop.computers.txt
once per computer.Choose one or the other - the foreach
loop, or passing $computers
to get-process
.
Option 1 (my preference):
$computers = Get-Content C:\computers.txt;
Get-Process -ComputerName $computers -Name Javaw | select machinename, id, ProcessName;
Option 1a:
Get-Process -ComputerName $(get-content c:\computers.txt) -Name Javaw | select machinename, id, ProcessName;
Option 2:
$computers = Get-Content C:\computers.txt;
foreach ($computer in $computers){
Get-Process -ComputerName $computer -Name Javaw | select machinename, id, ProcessName;
}
Option 3:
Get-Content C:\computers.txt | foreach-object {Get-Process -ComputerName $_ -Name Javaw | select machinename, id, ProcessName};
Upvotes: 3