How to get data in an array powershell

Posted on 2014-12-12
Last Modified: 2015-07-10

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



Question by:Hans de Jongh
  • 5
  • 3
LVL 67

Expert Comment

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?
LVL 67

Expert Comment

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


Author Comment

by:Hans de Jongh
ID: 40496889
sorry one array with all data or a csv would be handy
LVL 67

Expert Comment

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.
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.


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 :(
LVL 67

Expert Comment

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
Fiber Channel
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 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


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...)
LVL 67

Expert Comment

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!  ;)
LVL 68

Accepted Solution

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.

The INI file format is unknown for PowerShell. The usual recommendation is to use a utility function like Get-IniContent ( 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.

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…

910 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now