Get-Process Against Multiple Computers

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 5 years, 9 months ago.

  • Author
    Posts
  • #5215

    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 `
    -ComputerName $Computer `
    -ErrorAction Stop |
    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)

    < #
    Function Get-Process2
    {
    [CmdletBinding(Helpuri = 'http]
    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 `
    -ComputerName $Computer `
    -ErrorAction Stop |
    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

You must be logged in to reply to this topic.