header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

We have a new sponsor!  Introducting Pragma Systems.  See the home page for details.

Working with Expressions and Array
Last Post 28 Oct 2009 03:48 PM by Shay. 6 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
vishalramnaniUser is Offline
New Member
New Member
Posts:68
Avatar

--
27 Oct 2009 01:11 PM  
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:173
Avatar

--
27 Oct 2009 01:15 PM  
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:173
Avatar

--
27 Oct 2009 01:18 PM  
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
vishalramnaniUser is Offline
New Member
New Member
Posts:68
Avatar

--
27 Oct 2009 01:40 PM  
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:173
Avatar

--
27 Oct 2009 01:46 PM  
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/archive/2008/09/06/hate-add-member-powershell-s-adaptive-type-system.aspx.

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

--
27 Oct 2009 01:58 PM  
Thanks Ayth. Appreciate your help.
Vishal Ramnani
MCITP - Exchange 2007, MCSE Messaging, MCTS - Win 2008 Config
ShayUser is Offline
Veteran Member
Veteran Member
Posts:1140
Avatar

--
28 Oct 2009 03:48 PM  
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://blogs.microsoft.co.il/blogs/ScriptFanatic
PowerShell Community Toolbar
Twitter: @ShayLevy
You are not authorized to post a reply.

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