2

I have a PowerShell script, which contain an $arrayip and $hash. I want to add each IP address from $arrayip to be a name or key in my $hash hashtable.

My wrong syntax:

$arrayip = @("192.168.1.1", "192.168.1.2", "192.168.1.3")
$hash = @{
    name = "Name"
    $arrayip = "Is a server IP"
}

Bad Result for me:

PS C:\> $hash

Name                           Value                                           
----                           -----                                           
name                           Name                                            
{192.168.1.1, 192.168.1.2, ... Is a server IP

image result

2
  • 4
    What is your desired result? Do you want the addresses to be keys or values in your hashtable? That's a fundamental difference. What do you want to do with the hash once it holds the addresses? Commented Sep 24, 2017 at 12:21
  • yes sir, i want to add each ip as a key, and the values are same Commented Sep 25, 2017 at 3:06

3 Answers 3

2

Is this what you mean?

$arrayips = @("192.168.1.1", "192.168.1.2", "192.168.1.3")

$foreachhash = foreach($arrayip in $arrayips)
{
    $hash = [ordered]@{'Name'=$arrayip;
                       'Is a server IP' = $arrayip
                      } #end $hash

    write-output (New-Object -Typename PSObject -Property $hash)
} #end foreach

$foreachhash

Produces:

enter image description here

Thanks, Tim.

Sign up to request clarification or add additional context in comments.

10 Comments

The $hash keys in console contain one value!, i want to add each key for each IPs, for example: ip-----192.168.1.1 ip-----192.168.1.2 ip-----192.168.1.3 name-----name
I have updated my answer to add IP under the Name column. I also added [ordered] so that the Name column will always be first.
Oh, a key value pair? From this link technet.microsoft.com/en-us/library/ee692803.aspx The Key needs to be unique. $arrayips = [ordered]@{"IP1" = "192.168.1.1"; "IP2" = "192.168.1.2"; "IP3" = "192.168.1.3"} If you then check $arrayips it will look the way I think you are asking.
The other option is to set a variable for your whole foreach loop. Then display that. Formatting might be a bit odd as a comment. $foreachhashes = foreach($arrayip in $arrayips) { $hash = [ordered]@{'Name'='IP'; 'Is a server IP' = $arrayip } #end $hash write-output (New-Object -Typename PSObject -Property $hash) } $foreachhashes
Updated answer after comments.
|
1

For adding the array elements as keys to an existing hashtable you could do something like this:

$arrayip = '192.168.1.1', '192.168.1.2', '192.168.1.3'
$hash    = @{ 'Name' = 'Name' }

$arrayip | ForEach-Object {
    $hash[$_] = 'Is a server IP'
}

Comments

0

This will create array of hashes, however, you still need to think what to put in "name" property in hash.

# declare array of ip hashes
$iphashes = @();
# for each array ip
for($i = 0; $i -lt $arrayip.Count; $i++){
    $hash = @{
        name = "Name";
        ip = $arrayip[$i];
    }
    # add hash to array of ip hashes
    $iphashes += $hash;
}

1 Comment

When I type $hash in console, return one value for 'ip', I want to add each IPs for each values, for example: ip-----192.168.1.1 ip-----192.168.1.2 ip-----192.168.1.3 name-----name

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.