Automation

 View Only
Expand all | Collapse all

storing output into array

  • 1.  storing output into array

    Posted Feb 11, 2011 02:21 PM

    This one should be easy so I am a little ashamed. I have a script that queries our storage and for each host returns output in the form of:

    name value2 value3 value4
    name value2 value3 value4
    name value2 value3 value4
    etc....

    So each line represents a host and each value is some property of that host, name, initiator group.... and so on

    I am trying to figure out a way to put this information into an array, hash whatever so that I can get value 2, 3 or 4 by simply providing the first value (name).



  • 2.  RE: storing output into array

    Posted Feb 11, 2011 02:31 PM

    Something like this:

    [vSphere PowerCLI] C:\users\robert> $array=@{}
    [vSphere PowerCLI] C:\users\robert> $array["name1"]={"value2","value3","value4"}
    [vSphere PowerCLI] C:\users\robert> $array["name2"]={"value5","value6","value7"}
    [vSphere PowerCLI] C:\users\robert> $array["name1"]
    "value2","value3","value4"
    [vSphere PowerCLI] C:\users\robert> $array["name2"]
    "value5","value6","value7"
    [vSphere PowerCLI] C:\users\robert>


    Regards, Robert



  • 3.  RE: storing output into array

    Posted Feb 11, 2011 02:31 PM

    Easiest way would be to put the output of your script into an object:

    $object = some-cmdlet $arguments

    $object | $where {$_.name -eq "nameyouarelookingfor"}

    At least if I'm understanding your question.



  • 4.  RE: storing output into array

    Broadcom Employee
    Posted Feb 11, 2011 02:32 PM

    Create a custom object and put the object in a hash array using the name.

    $report = @{}

    inside your storage foreach loop

         $obj = "" | Select value1, value2, value3

         $report.Add($Name, $obj)

    You can access the values using:

    $report["Name1"].value1

    $report["Name1"].value2

    and so forth.



  • 5.  RE: storing output into array

    Posted Feb 11, 2011 03:09 PM

    If you are running the script indivuidually against each host and are trying to create a new object, try using 'Add-Member'

    to get the help file for add-member type:

    help add-member -examples

    What format is your report currently in?



  • 6.  RE: storing output into array

    Posted Feb 11, 2011 03:47 PM

    I am running the script against the storage and it returns the first five row of information about the storage device which I do not care about. So I  use the Select -Skip 5 to get rid of it. I then pipe that to Out-String  so I am then left with four columns of data. There is no header information it literally looks like I indicated above, each value is a group name (masking view, initiator group, port group and storage group).



  • 7.  RE: storing output into array

    Posted Feb 11, 2011 04:02 PM

    Ah

    You are telling it to output as a string.

    Simply go

    $myArray = get-datastore | select -skip 5

    easy peasy

    saying out-string converts the output to a string  . .

    Substitute Get-Datastore for whatever you are running



  • 8.  RE: storing output into array
    Best Answer

    Broadcom Employee
    Posted Feb 11, 2011 04:05 PM

    Why pipe the output to Out-String?

    This gets rid of the power of Powershell named objects!

    Do you query your storage using PowerShell cmdlets?

    In that case it returns objects.

    Put the objects in a hash array using the name as the key like I did in my previous reply.

    When you are stuck with strings, use the .split() method to split the string.

    Use the string array from the .split() method to craft a custom object.



  • 9.  RE: storing output into array

    Posted Feb 11, 2011 04:18 PM

    Unfortunatley I have to query the storage using the cli provided by the vendor. So I run an exe in my script that returns the information. I don't have to output to string I was doing that to test.



  • 10.  RE: storing output into array

    Posted Feb 11, 2011 04:31 PM

    OK,

    Easy enough.

    your old export is a big string.

    To split each row of this into a seperate row in an array, simply run

    $array = $oldstring.split("`n")

    (that is a backtick, top left of UK keyboard, not an apostrophe next to the n)

    You can now cycle through each row in the array (and split it or whatever you fancy)

    e.g. if you'd like to split the fields frther, run:

    foreach ($row in $array){ . . . .

    can provide more code if you need, but this should get you on your way - look at the various code options above, or add-member for the last piece of the puzzle.

    good luck



  • 11.  RE: storing output into array

    Posted Feb 14, 2011 07:27 PM

    Putting everything into a new-object worked well.

    Thanks All