Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Powershell and combining different object arrays

Posted on 2014-03-05
7
Medium Priority
?
310 Views
Last Modified: 2014-04-02
Hello EE,

I wish to create a script whereby it does 3 core tasks

1-  $getapplicationevents = get-eventlog -logname application

2-   $getsystemevents = get-eventlog -logname system

3-  $errorlogs = import-csv "c:\logs\logs.csv"

At this point i wish to combine certain member properties of each of the previous objects into a new "master object" with the goal of inserting into a database table or export to csv. All of the 3 objects would have a similar property the "host's name" . This would be similar concept to a primary key

My question is around the combining of certain member properties ;

Using $masterobject as the target for combining, with $getapplicationevents.message, $getsystemevents.message and $errorlogs.message in one array of objects ,$masterobject .The understanding is i would need to create the $masterobject and then use the add-member command but unsure of the structure from here ..
0
Comment
Question by:davesnb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 29

Expert Comment

by:becraig
ID: 39907501
If these are all strings you can simply just combine them if that is what you are after.

e.g.

 $masterobject =  "$getapplicationevents.message" + "$getsystemevents.message " + " $errorlogs.message " 

Open in new window


If this is an array you are creating you can simply assign using +=



 $masterobject +=  "$getapplicationevents.message" + "$getsystemevents.message " + " $errorlogs.message " 

Open in new window

0
 

Author Comment

by:davesnb
ID: 39909447
That works, however the objects are different type, so if I wish to export to csv or insert to database , the columns will not be consistent. It will assume the master object is of type ( last type combined) when using $masterobject | get-member and $masterobject will only show the members of the last type combined . So I ideally, I need a $master obejct that combines all the members of each object into one master. thoughts?
0
 
LVL 29

Expert Comment

by:becraig
ID: 39910419
What are the challenges  ?

IS this maybe one object is a string - one is a date etc  ?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:davesnb
ID: 39910457
Right , they are of different type . Two are of type diagnostic entry ( get-eventlog), one is a import-csv which is a different type
0
 
LVL 29

Accepted Solution

by:
becraig earned 1500 total points
ID: 39910478
You cast the objects to the different types

Probably best to reassign the variables:
e.g.
$ermsg = [string]$errorlogs.message


More info on how to do this here:
http://mcpmag.com/articles/2013/07/23/object-spell-in-powershell.aspx
0
 
LVL 29

Expert Comment

by:becraig
ID: 39962942
Did you make any progress on this or do you still need help with a resolution.
0
 

Author Closing Comment

by:davesnb
ID: 39972509
Ok thanks for the clarification
0

Featured Post

Looking for the Wi-Fi vendor that's right for you?

We know how difficult it can be to evaluate Wi-Fi vendors, so we created this helpful Wi-Fi Buyer's Guide to help you find the Wi-Fi vendor that's right for your business! Download the guide and get started on our checklist today!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will help you understand what HashTables are and how to use them in PowerShell.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question