VMware Cloud Community
admin
Immortal
Immortal
Jump to solution

storing output into array

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

0 Kudos
1 Solution

Accepted Solutions
avlieshout
VMware Employee
VMware Employee
Jump to solution

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.

Arnim van Lieshout Blogging: http://www.van-lieshout.com Twitter: http://www.twitter.com/avlieshout If you find this information useful, please award points for "correct" or "helpful".

View solution in original post

0 Kudos
10 Replies
RvdNieuwendijk
Leadership
Leadership
Jump to solution

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

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
Zsoldier
Expert
Expert
Jump to solution

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.

Chris Nakagaki (中垣浩一)
Blog: https://tech.zsoldier.com
Twitter: @zsoldier
avlieshout
VMware Employee
VMware Employee
Jump to solution

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.

Arnim van Lieshout Blogging: http://www.van-lieshout.com Twitter: http://www.twitter.com/avlieshout If you find this information useful, please award points for "correct" or "helpful".
0 Kudos
bulletprooffool
Champion
Champion
Jump to solution

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?

One day I will virtualise myself . . .
admin
Immortal
Immortal
Jump to solution

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

0 Kudos
bulletprooffool
Champion
Champion
Jump to solution

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

One day I will virtualise myself . . .
0 Kudos
avlieshout
VMware Employee
VMware Employee
Jump to solution

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.

Arnim van Lieshout Blogging: http://www.van-lieshout.com Twitter: http://www.twitter.com/avlieshout If you find this information useful, please award points for "correct" or "helpful".
0 Kudos
admin
Immortal
Immortal
Jump to solution

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.

0 Kudos
bulletprooffool
Champion
Champion
Jump to solution

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

One day I will virtualise myself . . .
0 Kudos
admin
Immortal
Immortal
Jump to solution

Putting everything into a new-object worked well.

Thanks All

0 Kudos