header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
Working with Expressions and Array
Last Post 28 Oct 2009 07:48 AM by Shay Levy. 6 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
Vishal RamnaniUser is Offline
New Member
New Member
Posts:68
Avatar

--
27 Oct 2009 05:11 AM
    Hey Guys,

    Little help here!

    I am trying to format a output of below scriptlet.

    $Servers = "Server1","Server2"
    $ProcessCount = $Servers | foreach {
           $Count = $(Get-Process -Computername $_ | Where {$_.processname -like "*SampleProcess*"}).count
    $_ + ": " + $count
    }

    With this i do get the resluts as below..

    Server1: 3
    Server2: 4

    but i do not want it in this way. i want it something like as below...

    Server           Count
    -------            ------
    Server1          3
    Server2          4

    I am not sure how to achieve this. Can someone point me to the right direction?

    Thanks in advance.

    Vishal Ramnani
    MCITP - Exchange 2007, MCSE Messaging, MCTS - Win 2008 Config
    AythUser is Offline
    Basic Member
    Basic Member
    Posts:232
    Avatar

    --
    27 Oct 2009 05:15 AM
    Ahh this leads me into my next blog post, if I can ever finish it up. Custom objects is what you want. Try this:

    $Servers = "Server1","Server2"
    $colResult = $()
    foreach ($server in $servers)
    {
    $Count = $(Get-Process -Computername $_ | Where {$_.processname -like "*SampleProcess*"}).count
    $objResult = New-Object System.Object
    $objResult | Add-Member -memberType NoteProperty -value $Server
    $objResult | Add-Member -memberType NoteProperty -value $Count
    $colResult += $objResult
    }
    $colResult

    Let me know if it helps.
    My Blog about Powershell http://poweroftheshell.blogspot.com/ Follow me on twitter @darrinhenshaw
    AythUser is Offline
    Basic Member
    Basic Member
    Posts:232
    Avatar

    --
    27 Oct 2009 05:18 AM
    Actually I mixed that up, try this:

    $Servers = "Server1","Server2"
    $colResult = $()
    foreach ($server in $servers)
    {
    $Count = $(Get-Process -Computername $_ | Where {$_.processname -like "*SampleProcess*"}).count
    $objResult = New-Object System.Object
    $objResult | Add-Member -memberType NoteProperty -Name Server -value $Server
    $objResult | Add-Member -memberType NoteProperty -Name Count -value $Count
    $colResult += $objResult
    }
    $colResult
    My Blog about Powershell http://poweroftheshell.blogspot.com/ Follow me on twitter @darrinhenshaw
    Vishal RamnaniUser is Offline
    New Member
    New Member
    Posts:68
    Avatar

    --
    27 Oct 2009 05:40 AM
    Perfect. Thanks Ayth! This one worked with slight modifications. So can you please throw some light on how and where all we can use this Add-Member cmdlet. One i learned just now :) .

    ----------------
    $Servers = "Server1","Server2"
    $colresult = @()
    foreach ($server in $Servers)
    {
    $agentcount = $(Get-Process -ComputerName $server | where {$_.processname -like "*SampleProcess*"}).count
    $objResult = New-Object System.Object
    $objResult | Add-Member -MemberType NoteProperty -value $server -Name Servers
    $objResult | Add-Member -MemberType NoteProperty -Value $count -Name Count
    $colresult += $objResult
    }
    $colresult
    ----------------------

    Thanks Again.
    Vishal Ramnani
    MCITP - Exchange 2007, MCSE Messaging, MCTS - Win 2008 Config
    AythUser is Offline
    Basic Member
    Basic Member
    Posts:232
    Avatar

    --
    27 Oct 2009 05:46 AM
    Certainly, Add-Member basically does what it says, it allows you to add Members(aka Properties) to objects. To get the desired result you wanted we needed to create a blank object, and then add in the data you wanted. Also, Add-Member doesn't just work on custom objects you created, it can work with the normal Pipeline objects as well. Check out the Powershell Team blog for a little more detail: http://blogs.msdn.com/powershell/ar...stem.aspx.

    Hope that help Vishal.
    My Blog about Powershell http://poweroftheshell.blogspot.com/ Follow me on twitter @darrinhenshaw
    Vishal RamnaniUser is Offline
    New Member
    New Member
    Posts:68
    Avatar

    --
    27 Oct 2009 05:58 AM
    Thanks Ayth. Appreciate your help.
    Vishal Ramnani
    MCITP - Exchange 2007, MCSE Messaging, MCTS - Win 2008 Config
    Shay LevyUser is Offline
    PowerShell MVP, Admin
    Veteran Member
    Veteran Member
    Posts:1362
    Avatar

    --
    28 Oct 2009 07:48 AM
    You can get the result you're after with a simple one-liner:

    Get-Process *SampleProcess* -Computername Server1,Server2 | group MachineName -NoElement

    Shay Levy
    Windows PowerShell MVP
    http://PowerShay.com
    PowerShell Community Toolbar
    Twitter: @ShayLevy
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer