• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 64
  • Last Modified:

How to get data in an array powershell

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
Hans de Jongh
Asked:
Hans de Jongh
  • 5
  • 3
1 Solution
 
sirbountyCommented:
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
 
sirbountyCommented:
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
 
Hans de JonghAuthor Commented:
sorry one array with all data or a csv would be handy
0
Who's Defending Your Organization from Threats?

Protecting against advanced threats requires an IT dream team – a well-oiled machine of people and solutions working together to defend your organization. Download our resource kit today to learn more about the tools you need to build you IT Dream Team!

 
sirbountyCommented:
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
 
Hans de JonghAuthor Commented:
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
 
sirbountyCommented:
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
 
Hans de JonghAuthor Commented:
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
 
sirbountyCommented:
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
 
QlemoC++ DeveloperCommented:
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

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now