myITforum.com Community Forum myITforum.com Community Forum

Home  Forums  Blogs  Live Support chat  Search Articles  Wiki  FAQ  Email Lists  Register  Login  My Profile  Inbox  Address Book  My Subscription  My Forums 

Photo Gallery  Member List  Search  Calendars  FAQ  Ticket List  Log Out

All Forums RSS Feed Subscription:


  


combining similar objects

 
View related threads: (in this forum | in all forums)

Logged in as: Guest
  Printable Version
All Forums >> [Scripting Technologies] >> Windows PowerShell >> combining similar objects Page: [1]
Login
Message << Older Topic   Newer Topic >>
combining similar objects - 8/26/2008 12:00:33 PM   
bziegler

 

Posts: 5
Score: 0
Joined: 8/26/2008
Status: offline
Is there a way to combine 2 similar objects (ex. output from get-wmiobject for different servers) without forcing it into a simple array with strings?  I would like to keep the object structure if possible.

Thanks
Post #: 1
RE: combining similar objects - 8/26/2008 11:35:43 PM   
martylist

 

Posts: 41
Score: 0
Joined: 6/10/2005
From: Colorado, USA
Status: offline
What do you mean by "combine 2 similar objects", explain how you want them combined.  Do you want an array of objects rather than an array of strings?  Can you provide an example of what you would like to happen, even if it's just pseudocode?

(in reply to bziegler)
Post #: 2
RE: combining similar objects - 8/27/2008 9:33:59 AM   
SAPIENScripter

 

Posts: 100
Score: 0
Joined: 5/15/2007
From: SAPIEN Technologies
Status: offline
You can always create a custom object.  Are you after something like this?
$computer=$env:computername
       
       $os=Get-WmiObject win32_operatingsystem -computername $computer
       $sys=Get-WmiObject win32_computersystem -computername $computer
       
       [datetime]$lastboot=$os.convertToDateTime($os.LastbootUpTime)
       
       $obj=New-Object PSObject
       
       $obj | Add-Member -MemberType "Noteproperty" -name "Computer" -value $sys.caption
       $obj | Add-Member -MemberType "Noteproperty" -name "OS" -value $os.Caption
       $obj | Add-Member -MemberType "Noteproperty" -name "ServicePack" -value $os.csdversion
       $obj | Add-Member -MemberType "Noteproperty" -name "Mfg" -value $sys.manufacturer
       $obj | Add-Member -MemberType "Noteproperty" -name "Model" -value $sys.model
       $obj | Add-Member -MemberType "Noteproperty" -name "LastBoot" -value $lastboot
               
       write $obj  



_____________________________

Jeffery Hicks
Microsoft MVP - Windows PowerShell
http://blog.SAPIEN.com
coming soon: Managing Active Directory with Windows PowerShell: TFM
followme: http://www.twitter.com/jeffHicks

(in reply to bziegler)
Post #: 3
RE: combining similar objects - 8/27/2008 11:08:46 AM   
bziegler

 

Posts: 5
Score: 0
Joined: 8/26/2008
Status: offline

Basically, I want to get to get a compilation of calls to different servers.  I want to be able to treat them as 1 object and not multiple objects.  So yes, I want an array of objects.

$pgmProcesses_total
=""
$pgmProcesses1
=""
$pgmProcesses2
=""
...

$pgmProcesses1
=get-WmiObject -computer DEVBOX01 -class Win32_Process -filter "name like `'asn%`'" | select-object -property name, commandLine

if
( $pgmProcesses_total = $null) {
    $pgmProcesses_total = $pgmProcesses1
}
else {
    $pgmProcesses_total = $pgmProcesses_total + $pgmProcesses1
}


$pgmProcesses2
=get-WmiObject -computer DEVBOX02 -class Win32_Process -filter "name like `'asn%`'" | select-object -property name, commandLine
 
$pgmProcesses_total
= $pgmProcesses_total + $pgmProcesses2

The result would be an array,
$pgmProcesses_total that had all the objects from both WMI calls, and also any previous calls to retrieve a name and command line.
 
The previous code results in the following error:
Method invocation failed because [System.Management.Automation.PSObject] doesn't contain a method named 'op_Addition'.
At line 12, position 44
$pgmProcesses_total = $pgmProcesses_total + $pgmProcesses2

 
Thanks for any help.

(in reply to SAPIENScripter)
Post #: 4
RE: combining similar objects - 8/27/2008 11:41:53 AM   
SAPIENScripter

 

Posts: 100
Score: 0
Joined: 5/15/2007
From: SAPIEN Technologies
Status: offline
Now I have a better understanding. You could still use my custom object approach but this more along the lines of what you already have.  You need to define $pgmProcesses_total as an array:

$pgmProcesses_total=@()

Then run your WMI Query:
$data=get-WmiObject -computer DEVBOX02 -class Win32_Process -filter "name like `'asn%`'" | select-object -property name, commandLine ,CSName

I added CSName to give you the server name. Finally, add $data to the array:

$pgmProcesses_total+=$data

When finished, look at $data and I think it will have what you want.  There are also more elegant ways to do this for a list of computers but I don't want to take away all your fun.



_____________________________

Jeffery Hicks
Microsoft MVP - Windows PowerShell
http://blog.SAPIEN.com
coming soon: Managing Active Directory with Windows PowerShell: TFM
followme: http://www.twitter.com/jeffHicks

(in reply to bziegler)
Post #: 5
RE: combining similar objects - 8/27/2008 12:29:28 PM   
bziegler

 

Posts: 5
Score: 0
Joined: 8/26/2008
Status: offline
Thanks Jeffery, for the solution.  I don't want to impose upon you but since I'm new to Powershell, I would be happy to learn of more elegant ways to retrieve data against multiple servers (if you have time).  I'm sure that the knowlege would be valuable to other newbies too.

(in reply to SAPIENScripter)
Post #: 6
RE: combining similar objects - 8/27/2008 12:40:19 PM   
SAPIENScripter

 

Posts: 100
Score: 0
Joined: 5/15/2007
From: SAPIEN Technologies
Status: offline
Since you asked nicely here's a more complete solution:

$pgmProcesses_total=@()

Get-Content c:\test\servers.txt | foreach {
$pgmProcesses_total+= (Get-WmiObject -computer $_ -class Win32_Process -filter "name like `'asn%`'" |
Select-Object -property name, commandLine ,CSName)
}

$pgmProcesses_total

_____________________________

Jeffery Hicks
Microsoft MVP - Windows PowerShell
http://blog.SAPIEN.com
coming soon: Managing Active Directory with Windows PowerShell: TFM
followme: http://www.twitter.com/jeffHicks

(in reply to bziegler)
Post #: 7
RE: combining similar objects - 8/27/2008 6:05:31 PM   
bziegler

 

Posts: 5
Score: 0
Joined: 8/26/2008
Status: offline
Thanks, it works great now.

(in reply to SAPIENScripter)
Post #: 8
RE: combining similar objects - 8/28/2008 4:58:58 PM   
bziegler

 

Posts: 5
Score: 0
Joined: 8/26/2008
Status: offline
FYI:  I did find one piece of unexpected behavior in the code.  If it finds no processes for a particular server, the count of $pgmProcesses_total will still increase by one.  I got around this by assigning the WMI object to a temporary array and check it for any values before concatenating the array with $pgmProcesses_total.  The problem was with +=.

(in reply to bziegler)
Post #: 9
RE: combining similar objects - 9/3/2008 8:54:30 AM   
SAPIENScripter

 

Posts: 100
Score: 0
Joined: 5/15/2007
From: SAPIEN Technologies
Status: offline
That's because the WMI expression is completing successfully even though no processes were found. Even though you can get a lot done with PowerShell in only a few lines, that's only true if you want to forego error handling and situations like these.

_____________________________

Jeffery Hicks
Microsoft MVP - Windows PowerShell
http://blog.SAPIEN.com
coming soon: Managing Active Directory with Windows PowerShell: TFM
followme: http://www.twitter.com/jeffHicks

(in reply to bziegler)
Post #: 10
Page:   [1]
All Forums >> [Scripting Technologies] >> Windows PowerShell >> combining similar objects Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts



  
Forum Software © ASPPlayground.NET Advanced Edition 2.4.5 ANSI

0.234