Reputation: 1
I might need a second set of ideas here.
This is what 'd to achieve: I need to perform a task in a Hashtable (Powershell) to each value of a particular Key
I am scripting a portion of enabling a dfsr replication and I am stuck because
the follow command needs to be executed:
New-DfsReplicationGroup -GroupName "" | New-DfsReplicatedFolder -FolderName "" | Add-DfsrMember -ComputerName Server1,Server2
If I run the command it self on a powershell terminal works without any problem.
On a Hashtable I already have the follow:
Content of Grouping servers list
Name Value
---- -----
Group Name1 {Server1, Server2}
Group Name2 {Server6, Server9}
Which now has to be easier right? Thinking as Array:
Write-Verbose "Configuring a full-DFSr connection "
for ($i = 0 ; $i -lt $server_list.Count ; $i++)
{
for ($j = $i + 1 ; $j -lt $server_list.Count ; $j++)
{
Write-Verbose ("Adding bidirectional connections between the member computers named {0} and {1}" -f $server_list[$i],$server_list[$j])
$dfs_connection_status = Add-DfsrConnection -GroupName $GroupName -SourceComputerName $server_list[$i] -DestinationComputerName $server_list[$j]
Write-Output $dfs_connection_status
}
}
However I can't figure it out how to manipulate the content once the key is found
Then I took a break from that script portion and decided to break it down a simple a+b
Take a look on the follow:
where $($key.Name) has the key value:d and the Value content is:1,4,5
lines as follow
$num = @{a= "Test"
b= "Test2"
c= "Test3"
}
$num.d = @() #adds to an array
$num.d+= '1'
$num.d+= '4'
$num.d+= '5'
$result= $num[0]+$num[1] or if decided $result= $num[0]+$num[2]
result should be '5' or '6' if (1+5)
$num.GetEnumerator() | Sort-Object -Property Value | Where-Object {$_.Key}
Name Value
---- -----
d {1, 4, 5}
a Test
b Test2
c Test3
#$sum+= $num.ContainsKey("d")+$num.ContainsKey("d")+1
$num.GetEnumerator() | Sort-Object -Property Value | Where-Object {$_.Key}
if ($num.ContainsKey("d")) { $num['d'] }
if the d key is found then manipulate the value content as needed . Thinking on my original script for dfsr would be once the group name is found enable the bidirectional replication Server1 -> Server2 and Server2 -> Server1
I could not make it to work so I decided to create a new array (probably is not really needed) what I was thinking okay so you found the d key now lets move all the content to a new array so we can manipulate it.
$vm_list+= $num.Values | Where-Object {$num.ContainsKey("d")} | % ToString
Write-Host "n
nt VM_list:
t" $($vm_list)
Hope you guys can help
Upvotes: 0
Views: 50
Reputation: 1
So I found the answer , can't believe it was that easy maybe was loosing my mind.
I am sharing a possible solution to the community ;)
"there is always a multiple ways to get to a place so this could be one;)"
$script:vmlist = @{} #Hashtable
$vm_list = @() #Array
$vmlist.d = @()
$vmlist.d+= 'Server1'
$vmlist.d+= 'Server2'
if ($vmlist.ContainsKey("d")) #Looking for a particular Key
{
$vmlist['d']
$vm_list+= $vmlist['d']
Write-Host "`n`n`t VM_list:`t`t $($vm_list)`n`n$($vm_list[0])`n$($vm_list[1])`n$($vm_list[2])"
Test-Connection $vm_list[0+1]
}
else {Write-Host "Value not found"}
Printing the content of Hashtable
VM_list: Server1 Server2
Printing the content of the new Array , noted that it will contain the values from the Key d
VM_list[0]:Server1
VM_list[1]:Server2
result:
Source Destination IPV4Address IPV6Address Bytes Time(ms)
------ ----------- ----------- ----------- ----- --------
Server source Server2 10.114.x.y 32 0
Server source Server2 10.114.x.y 32 0
Server source Server2 10.114.x.y 32 0
Server source Server2 10.114.x.y 32 0
Hope you guys can use it
Regards JCayetano
Upvotes: 0