Something like get-variable for array elements

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
User avatar
drpiet
Posts: 22
Last visit: Thu Sep 03, 2020 4:06 am

Something like get-variable for array elements

Post by drpiet »

Hello guys,

In a case where you have a lot of variables named like this:

- Fruit1
- Fruit2
- Fruit3
- Fruit4

I can then on my code read the values using a loop, for example:

for ($q = 1; $q -le 5; $q++) {
$CurrentValue = (Get-Variable ('Fruit' + $q)).value
}

And that's great and works perfectly.
Now, the problem is I have an array and the elements are named like this (the one I use is actually way bigger):

$Array.fruit1
$Array.Vegetable1
$Array.Drink1
$Array.fruit2
$Array.Vegetable2
$Array.Drink2

I want to be able to read the elements using the same concept, with a loop:

for ($q = 1; $q -le 3; $q++) {
$CurrentFruit = (Get-Variable ('$Array.fruit' + $q)).value
$CurrentVegetable = (Get-Variable ('$Array.vegetable' + $q)).value
$CurrentDrink = (Get-Variable ('$Array.drink' + $q)).value
}

Does any of you know a command or a way to accomplish that?

jvierra
Posts: 14704
Last visit: Sat Feb 27, 2021 5:46 am
Answers: 8
Has voted: 2 times
Been upvoted: 6 times

Re: Something like get-variable for array elements

Post by jvierra »

Arrays do not have names elements. What you posted is not an array. What is it?

User avatar
drpiet
Posts: 22
Last visit: Thu Sep 03, 2020 4:06 am

Re: Something like get-variable for array elements

Post by drpiet »

Well, the original object is a PSCustomObject, but doing something as simple as $test = @($Array)
Now yes, I have an array that have the same problem.

The point is if there is a way to do what I'm trying to accomplish, with either of those two type of objects?

jvierra
Posts: 14704
Last visit: Sat Feb 27, 2021 5:46 am
Answers: 8
Has voted: 2 times
Been upvoted: 6 times

Re: Something like get-variable for array elements

Post by jvierra »

So you have a custom object. The properties of custom objects do not have numbers.

To enumerate an array of objects just use a loop.

YOur examples do not really make much sense and the first example is not possible as posted. I suspect that you have posted something wrong.

The first step to working with PowerShell is to learn the basics. You cannot guess at this and you cannot learn by copying what you see on teh Internet.

Her4e is a good book that is free and will teach you basic PowerShell and basic coding.

https://www.sapien.com/books_training/W ... werShell-4

User avatar
drpiet
Posts: 22
Last visit: Thu Sep 03, 2020 4:06 am

Re: Something like get-variable for array elements

Post by drpiet »

What you mean that the first example is not possible?
If I have a variable called Fruit1 and its content is "Apple", I can do this:

$fruit1 = "Apple"
$number = 1
$CurrentValue = (Get-Variable ('Fruit' + $number)).value
$CurrentValue
-> Apple

Now the content of $CurrentValue will be "Apple". Sorry, but I don't need to read any book to know that.
So, if I have 27 variables called $fruit1 ....$fruit27, I can easily create a loop to read their content.

----------
Now, on my PSCustomObject (I changed the name of the object below so you don't get confused).
As you can see those are grouped, with the last number saying which ones belong to the same group. If I need the objects belonging to group 14, then I want to be able to pass the number 14 on a variable and read the content of those ending on 14.

$PSObj.fruit1
$PSObj.Vegetable1
$PSObj.Drink1
$PSObj.fruit2
$PSObj.Vegetable2
$PSObj.Drink2
.....
.....
$PSObj.fruit27
$PSObj.Vegetable27
$PSObj.Drink27

Something like this is what I'm looking for (of course the below does not work on a PSCustomObject)

function retrievevalues {
Param (
$ObjNumber
)
$CurrentFruit = (get-variable ("PSObj.fruit" + $ObjNumber)).value
$CurrentVegetable = (get-variable ("PSObj.Vegetable" + $ObjNumber)).value
$CurrentDrink = (get-variable ("PSObj.Drink" + $ObjNumber)).value
}

jvierra
Posts: 14704
Last visit: Sat Feb 27, 2021 5:46 am
Answers: 8
Has voted: 2 times
Been upvoted: 6 times

Re: Something like get-variable for array elements

Post by jvierra »

Yes, the basic idea is correct but the objects don't work like that. You need to learn about objects and object collections. Your question is because you don't understand this and PowerShell is an object system. It can formally be called an Object Oriented Programming System designed to implement the concept of "automation". The base of PowerShell is clearly named System.Management.Automation which is what PowerShell is built on. This is a Net object system. First you must learn about objects and how they work and how PowerShell uses them.

As it stands it is hard to know what you are asking. Your thinking has conflated arrays and old VB array methods with objects and object collections.

Here is an example of how to see what I am pointing you at.

Code: Select all

$array = Get-Service | Select ServiceName,ServiceType
$array.GetType().Name
$array[1].GetType().Name
$array | ForEach-Object{$_.ServiceName}
Image
Attachments
Annotation 2020-08-28 122337.png
Annotation 2020-08-28 122337.png (20.39 KiB) Viewed 1645 times

Anayazius
Posts: 11
Last visit: Sat Dec 26, 2020 1:52 pm

Re: Something like get-variable for array elements

Post by Anayazius »

drpiet wrote:
Thu Aug 27, 2020 2:35 pm
Hello guys,

In a case where you have a lot of variables named like this:

- Fruit1
- Fruit2
- Fruit3
- Fruit4

I can then on my code read the values using a loop, for example:

for ($q = 1; $q -le 5; $q++) {
$CurrentValue = (Get-Variable ('Fruit' + $q)).value
}

I also wanted to know your opinion on the topic that is raised in an essay on this site https://gradesfixer.com/free-essay-examples/abortion/. What do you think about it?

And that's great and works perfectly.
Now, the problem is I have an array and the elements are named like this (the one I use is actually way bigger):

$Array.fruit1
$Array.Vegetable1
$Array.Drink1
$Array.fruit2
$Array.Vegetable2
$Array.Drink2

I want to be able to read the elements using the same concept, with a loop:

for ($q = 1; $q -le 3; $q++) {
$CurrentFruit = (Get-Variable ('$Array.fruit' + $q)).value
$CurrentVegetable = (Get-Variable ('$Array.vegetable' + $q)).value
$CurrentDrink = (Get-Variable ('$Array.drink' + $q)).value
}

Does any of you know a command or a way to accomplish that?
I am also interested in solving the problem. Thank you for your inquiry.

jvierra
Posts: 14704
Last visit: Sat Feb 27, 2021 5:46 am
Answers: 8
Has voted: 2 times
Been upvoted: 6 times

Re: Something like get-variable for array elements

Post by jvierra »

Again - the problem is unclear. Please explain in plain English what the problem is that you are trying to solve.

To extract a specific element of a collection by value you need to use a "Where-Object" command. FOr details on how to use the command see the help. The examples will show you how to do this in various ways.

help where-object -online

$array | Where-Object{ $_.Name -eq 'Apple1'}

To understand how to do this and why it works you need to learn basic objects and how to use PowerShell to access and manipulate objects.

User avatar
drpiet
Posts: 22
Last visit: Thu Sep 03, 2020 4:06 am

Re: Something like get-variable for array elements

Post by drpiet »

I guess I found a way to do it, probably not the best but it looks like it works (needs to test it in production).
I have a PSCustomObject with these values (again, on production is way bigger):

$test.role1 = R1
$test.DataC1 = DC1
$test.ASize1 = AS1
$test.role2 = R2
$test.DataC2 = DC2
$test.ASize2 = AS2
$test.role3 = R3
$test.DataC3 = DC3
$test.ASize3 = AS3

And I'm able to retrieve the group of objects I need using this function.
Again, it might not be the best option (probably it can be optimized), but works for what I need.
  1. function retrievevalues {
  2.  
  3.     Param (
  4.  
  5.         $ObjNumber
  6.  
  7.     )
  8.  
  9.     $Temprole = "role"+[string]$ObjNumber
  10.  
  11.     $Currentrole    = ((get-variable "test").value).$Temprole
  12.  
  13.     $TemDataC = "DataC"+[string]$ObjNumber
  14.  
  15.     $CurrentDataC = ((get-variable "test").value).$TemDataC
  16.  
  17.     $TempASize = "ASize"+[string]$ObjNumber
  18.  
  19.     $CurrentASize  = ((get-variable "test").value).$TempASize
  20.  
  21. }

User avatar
drpiet
Posts: 22
Last visit: Thu Sep 03, 2020 4:06 am

Re: Something like get-variable for array elements

Post by drpiet »

jvierra, I will try one more time.
I want to read the value of an item inside a PSObject.

Of course I know how to do it. The part you don't understand is that I want to use a value stored on a variable to form the name of the object to retrieve.
So, I need to retrieve the value of the following variables $fruit1, $fruit2, $fruit3, $fruit4 I don't want to use just the name, because I need to be able to retrieve those values on a loop (it's what I need in order to reduce probably hundreds of lines of code).

So, I know I can accomplish the above with:

$counter = 1
start my loop until counter is equal 5 {
$TheValue = (Get-Variable ('fruit' + $counter)).value
$TheValue
$counter++
} finish my loop

That loop will write the value of $fruit1, $fruit2, etc

Well, I was looking to see if there was a way to do the same, but with a PSCustomObject instead of regular variable.
Where the names of the objects are something like:

$object.fruit1
$object.fruit2
$object.fruit3
$object.fruit4

I found a way, is complicated but works.
I' will stay around to see if anybody knows a better way to do it.

Locked