Solved

How to get data in an array powershell

Posted on 2014-12-12
9
55 Views
Last Modified: 2015-07-10
Hello,

I have a lot of data I want to import in an array so I can configure a server (idrac)
now I would like get everything in an array.
See attachment

I only care about these settings

                    category                     = audit
                    severity                     = warning
                    Sub-Cat Desc                 = Licensing
                    Sub-Category                 = LIC
                    Notifications                = remotesyslog, email
                    Permitted Notifications      = OSLog, snmp, ws-events, remotesyslog, email

thanks!

Regards

Hans
idrac8-alerts.txt
0
Comment
Question by:Hans de Jongh
[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
  • 5
  • 3
9 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 40496836
Might help to know how you're expecting to use the output...?
Do you want one array with all of that relevant data, or an array per each element?
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 40496851
One option that comes to mind...

$filters = 'category','severity','Sub-Cat Desc','Sub-Category','Notifications','Permitted Notifications'

foreach ($filter in $filters) {
    $arrData += @{$filter = $data | where {$_ -like "$filter*"} }
}

Open in new window

0
 

Author Comment

by:Hans de Jongh
ID: 40496889
sorry one array with all data or a csv would be handy
0
Get Actionable Data from Your Monitoring Solution

Your communication platform is only as good as the relevance of the information you send. Ensure your alerts get to the right people every time with actionable responses. Create escalation rules that ensure everyone follows the process and nothing is left to chance.

 
LVL 67

Expert Comment

by:sirbounty
ID: 40496899
The above is a single array/hash table of the elements you're looking to retrieve, but without knowing your purpose, I could point you into a dozen different routes and be guessing each time.
If you could elaborate on how you're using it perhaps that would help come up with something more appropriate.
0
 

Author Comment

by:Hans de Jongh
ID: 40496913
im going to create a script which is updating all my idracs with these settings

so something like

foreach $array in arrays
racadm bla $array.subcategory in $array.catergory and so one...

I hope im clear enough :(
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 40496951
So...does the above work for your scenario?

A bit cleaner rendition here:
$filters = 'category','severity','Sub-Cat Desc','Sub-Category','Notifications','Permitted Notifications'

foreach ($filter in $filters) {
    $arrData += @{$filter = ($data | Where-Object {$_ -like "$filter*"}).replace($filter,'').replace('=','').trim() }
}

Open in new window


Then, as an example, you could access the first element using
$arrData.category[0],$arrData.severity[0],$arrData.'Sub-Cat Desc'[0],$arrData.'Sub-Category'[0],$arrData.Notifications[0],$arrData.'Permitted Notifications'[0]

which results in
system
warning
Fiber Channel
FC
remotesyslog, email
snmp, ws-events, remotesyslog, email


So for your loop above to work, you would need to base the loop iteration off the number of instances...you can get at that easily enough, assuming that each 'group' will contain each of the items you're searching, and just taking the total in one of those groups (I've relied on the 'category' count below).  
*Just notate how I've encapsulated some of those fields that either have spaces or the - (hyphen) character...

For an example,
for ($x=0;$x -le $arrdata.category.count -1 ;$x++) {
    write-host "Category: $($arrdata.category[$x]) has a severity of $($arrdata.severity[$x]) and a sub-cat of $($arrdata.'Sub-Category'[$x])"
  #Or, using your example above:
  racadm bla $arrdata.subcategory[$x] in $arrdata.catergory[$x] #etc...
}

Open in new window

0
 

Author Comment

by:Hans de Jongh
ID: 40497879
thanks really:) it looks very good. but I think this is over complicated
I just want something like this:

Event ID                     = 2378
category                     = system
severity                     = warning
Sub-Cat-Desc                 = Sys Event Log
Sub-Category                 = SEL
Filter Actions               = none
Permitted Filter Actions     = none
Notifications                = remotesyslog, email
Permitted-Notifications      = OSLog, snmp, ws-events, remotesyslog, email

would become:

system,warning,Sys Event Log,SEL,remotesyslog,email,remotesyslog, email

in an array or csv.
I don't know how to work with array[x]
If I type $array in powershell I would just like to see everything at once and no need to do array[x]

sorry my powershell knowhow is very limited.

(I tried  $arrdata.category,$arrdata.severity | ft but that doesn't work either...)
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 40497942
I think it's under-using the power of powershell if you parse through the data one line at a time.  
What we're doing is taking the data as a whole, and filtering out each of the items you need, and grouping each instance of it into it's own array, combining the results into a table.
What you end up with is something like the following:
Name                           Value                                                                                                                     
----                           -----                                                                                                                     
Notifications                  {remotesyslog, email, remotesyslog, remotesyslog, email, remotesyslog...}                                                 
category                       {system, system, system, system...}                                                                                       
Sub-Cat Desc                   {Fiber Channel, Fiber Channel, NIC Config, NIC Config...}                                                                 
Permitted Notifications        {snmp, ws-events, remotesyslog, email, snmp, ws-events, remotesyslog, email, snmp, ws-events, remotesyslog, email, snmp...
severity                       {warning, info, warning, info...}                                                                                         
Sub-Category                   {FC, FC, NIC, NIC...}                                                                                                     

Open in new window


So here's a table listing all of the categories, subcategories, etc, in a zero-based array for each of the items you're after...  

I've taken your attachment, saved it as 'alerts.txt' and the following code works quite nicely for retrieving those elements.  When I refer to the $x element of each array item, I'm just asking for that instance within each of the arrays.  In other words, I want the details of the data under [idrac.alert.1].  So, in my for loop, I use x=0 to state "give me the first (0) element in the category array, the first element in the sub-category array, etc, all from the table that I created containing each of those arrays".  It's very compartmentalized, so I can understand why it seems a bit daunting at first.  Think of it as a filing cabinet with 6 drawers.  The first drawer now has all of the "category" items stacked in order of appearance.  If you pulled out the first folder from the first drawer, you're going to get "system".  If you then proceeded to the second drawer and pulled out the first item, you'd see "warning", and so on...

Try this code against your file.  If you load it into the ISE, you can place a break point at the end of the loop so that you can confirm the data is what you need following each loop. (Obviously your data may be named differently and in a different folder, so adjust accordingly)

$data = get-content 'c:\testfolder\alerts.txt'
$filters = 'category','severity','Sub-Cat Desc','Sub-Category','Notifications','Permitted Notifications'
foreach ($filter in $filters) {
    $arrData += @{$filter = ($data | Where-Object {$_ -like "$filter*"}).replace($filter,'').replace('=','').trim() }
}

for ($x=0;$x -le $arrdata.category.count -1 ;$x++) {
    write-host "Category: $($arrdata.category[$x])"
    write-host "Severity: $($arrdata.severity[$x])"
    write-host "Sub-cat desc: $($arrdata.'Sub-Cat Desc'[$x])"
    write-host "Sub-category: $($arrData.'Sub-Category'[$x])"
    Write-Host "Notifications: $($arrData.Notifications[$x])"
    write-host "Permitted notifications: $($arrData.'Permitted Notifications'[$x]) `r`n" #`r`n is a carriage return
} #place breakpoint here to 'pause' execution
}

Open in new window


The first result from the following should show:
Category: system
Severity: warning
Sub-cat desc: Fiber Channel
Sub-category: FC
Notifications: remotesyslog, email
Permitted notifications: snmp, ws-events, remotesyslog, email

To output exactly as you've exampled above, you could use:
write-host "$($arrdata.category[$x]),$($arrdata.severity[$x]),$($arrdata.'Sub-Cat Desc'[$x]),$($arrData.'Sub-Category'[$x]),$($arrData.Notifications[$x]),$($arrData.'Permitted Notifications'[$x])"

or more eloquently:
'{0},{1},{2},{3},{4},{5}' -f $arrdata.category[$x],$arrdata.severity[$x],$arrdata.'Sub-Cat Desc'[$x],$arrData.'Sub-Category'[$x],$arrData.Notifications[$x],$arrData.'Permitted Notifications'[$x]

If you have any questions on the above, feel free to ask!  I love powershell and am continually amazed at what can be accomplished in a few short lines of code.  But, if you're still set on stepping through the data another way, let me know - I'll be glad to provide other options (though admittedly less efficient).  Taking out all of the blank spaces and indentions for clarity, the above code is merely 4 lines!  ;)
0
 
LVL 70

Accepted Solution

by:
Qlemo earned 500 total points
ID: 40504843
sirbounty, that is far from what PowerShell is designed for. In almost all cases you use arrays of objects containing the required properties. And I read it being requested here, too.

Hans,
The INI file format is unknown for PowerShell. The usual recommendation is to use a utility function like Get-IniContent (http://blogs.technet.com/b/heyscriptingguy/archive/2011/08/20/use-powershell-to-work-with-any-ini-file.aspx) but that is creating a hash table, and does not really fit here. So a simplified attempt is shown here:
$curObj = $null
get-content c:\temp\ee\idrac8-alerts.txt | % {
  if ($_ -like '[[]idrac.alert.*')
  {
    if ($curObj) { $curObj }
    $curObj = New-Object PsObject
  }
  elseif ($_)
  {
    $val = $_.Split('=')
    $curObj | Add-Member NoteProperty $val[0].Trim(' ') $val[1]
  }
} | ft -a

Open in new window

Remove the | ft -a to get the real objects, it is only for displaying purposes.
You can export to CSV, or use the resulting objects, but keep in mind that the properties contain spaces and hence need to be enclosed in ticks or curly braces if you want to directly refer to them.
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial 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.
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

717 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