Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Powershell and combining different object arrays

Posted on 2014-03-05
7
Medium Priority
?
312 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
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
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 anti-spam), the admin…
Screencast - Getting to Know the Pipeline

782 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