Get-Process Against Multiple Computers

by smcreamer at 2012-10-19 11:54:41

When running Get-Process against multiple servers (with -Computername), the computer name is not listed on the default view. I am unable to tell which process goes with which server. I like the default view Get-Process is using. I would like to have the server name listed after the process name.

Same for Get-Service.

What is the easiest way to get the server names to show up with the default view?
by DonJ at 2012-10-19 13:39:11
Short answer, you can’t. The defaults aren’t modifiable. You could override them by writing your own custom view.

Consider using Remoting instead. It adds the computer to its default view.
by Jason_Yoder_MCT at 2012-10-21 11:54:37
Smcreamer,

Here is another option. This one uses WMI to get the process information from remote computers. When using the Get-WMIObject cmdlet, you get a couple of extra properties that start with 2 underscores. You are interested in the __SERVER property. Below is some code to help you out.

Function Get-Process2
{
[CmdletBinding()]
Param (
[Parameter(Mandatory=$True)]$ComputerName,
[Switch]$Quiet,
[Switch]$FullDetail
)

# Cycle through each computer.
ForEach ($Computer in $ComputerName)
{
Try
{
# Get the process information and include the client
# that the information was received from.

If($FullDetail)
{
$Data = Get-WmiObject Win32_Process
}
Else
{
$Data = Get-WmiObject Win32_Process <br> -ComputerName $Computer
-ErrorAction Stop |
Select-Object <br> -Property Handles, NPM, PM, WS, VM, CPU, ID, Name, @{Label=&quot;ComputerName&quot;;Expression={$_.__Server}}<br> }<br><br> Write-Output $Data<br> } # End: Try<br> Catch<br> {<br> # If the client could not be contacted, notify the user.<br> If &#40;!$Quiet&#41;<br> {<br> Write-Host &quot;$Computer is not online&quot; -ForegroundColor Red -BackgroundColor DarkRed<br> }<br> } # End: Catch<br><br><br> } # End: ForEach &#40;$Computer in $ComputerName&#41;<br><br>&lt;#<br>Function Get-Process2<br>{<br>[CmdletBinding&#40;Helpuri = &#39;http]<br>Param &#40;<br> [Parameter&#40;Mandatory=$True&#41;]$ComputerName,<br> [Switch]$Quiet,<br> [Switch]$FullDetail<br>&#41;<br><br> # Cycle through each computer.<br> ForEach &#40;$Computer in $ComputerName&#41;<br> {<br> Try<br> {<br> # Get the process information and include the client<br> # that the information was received from.<br> <br> If&#40;$FullDetail&#41;<br> {<br> $Data = Get-WmiObject Win32_Process <br> }<br> Else<br> {<br> $Data = Get-WmiObject Win32_Process
-ComputerName $Computer <br> -ErrorAction Stop |<br> Select-Object
-Property Handles, NPM, PM, WS, VM, CPU, ID, Name, @{Label="ComputerName";Expression={$_.__Server}}
}

Write-Output $Data
} # End: Try
Catch
{
# If the client could not be contacted, notify the user.
If (!$Quiet)
{
Write-Host "$Computer is not online" -ForegroundColor Red -BackgroundColor DarkRed
}
} # End: Catch


} # End: ForEach ($Computer in $ComputerName)

<#
.SYNOPSIS
Returns process information on local and remote clients.

.DESCRIPTION
Returns process information on local and remote clients, but includes
the computer name.

.PARAMETER ComputerName
The name or names of the computers that you want to return process
information from.

.PARAMETER Quiet
Suppresses error messages.

.PARAMETER FullDetail
Returns the entire process object. The default will return the
same information as Get-Process.

.EXAMPLE
Get-Process2 -ComputerName "Srv01", "Svr2", "Svr3"

Returns the same information as Get-Process, but also includes the
name of the source the data came from.

.EXAMPLE
Get-Process2 -ComputerName "Srv01", "Svr2", "Svr3" -Quiet

Returns the same information as Get-Process, but also includes the
name of the source the data came from. Any errors generated while
attempting to contact the clients will be suppressed.

.EXAMPLE
Get-Process2 -ComputerName "Srv01", "Svr2", "Svr3" -FullDetail
Returns the entire Process object for each object on each server contacted.
This will return a very large amount of data.

.NOTES
-------------------------------------------------------------------------------
Provided as is with no warranty or support.
Jason Yoder, MCT - MCTExpert, Inc.
-------------------------------------------------------------------------------


#>
} # – End Function Get-Process2 ----------------------------------------------



You can dot source this code in or add it to a PowerShell module. I added a help file to show you who to use it.

Dot Source: http://mctexpert.blogspot.com/2011/04/dot-sourcing-powershell-script.html
Create a module: http://mctexpert.blogspot.com/2011/04/creating-powershell-module.html

Have fun!

Jason
by DonJ at 2012-10-21 12:12:44
And in v3, all WMI objects provide PSComputer (which is an alias to __SERVER).
by Jason_Yoder_MCT at 2012-10-21 14:09:00
Thank you Don. I did not notice that. Cool.
by Makovec at 2012-10-22 01:12:57
If you want just easy filtering, Get-Process and Get-Service has also property called MachineName. You can filter just by this one. Not necessary to do WMI magic.

David