3

I am trying to delete an item from a string array. It is an array of 5 server names and I run a reboot function against each server in the array. I can only reboot 5 servers at a time so the array will never get bigger than that but at the same time I want to keep adding servers to the array so I can cycle through all of the servers in my environment. When a servers has completed its reboot I need to delete that server from the array so I can add another to the array and run the reboot function against it. I have tried the $array.remove method but I get an error saying that it doesn't work on a string array. I have tried redefining the original array without the rebooted server

"$balancedout = $balancedout -notlike $server".
1
  • 1
    Please show a code sample of what you are trying to do, something like $balancedout = $balancedout -notlike $server should work. Commented Jul 9, 2013 at 22:04

3 Answers 3

4

Note that PowerShell Uses .Net like code .

So declare the array as follow:

 $a = New-Object System.Collections.ArrayList

Now you can add items to this array:

 $a.Add("Server1")
 $a.Add("Server2")
 $a.Add("Server3")

If you print the array you get:

PS C:\Users\Work> $a
Server2
Server1
Server3

Now $a does have a Remove method:

 $a.Remove("Server2")

Now If you print the array you get:

   PS C:\Users\Work> $a
Server1
Server3
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your quick response. So by declaring in that manner allows the .remove method? In my script I declared the array as follows (based on Internet articles) "$balancedout = @()". Can you explain to me what the diffrence is and why the .remove would not work?
4

Either use an array list as Joe Tatavaran suggested (which is probably the most elegant solution), or re-assign the array like this:

$a = $a | ? { $_ -ne $server }

Comments

-1

Can you load up the entire server list into an ArrayList? You could split it into arrays of five items, then work on each set of servers.

$servers = Get-Content serverlist.txt
$setIdx = -1
$sets = @()
for( $i = 0; $i -lt $servers.Count )
{
    if( $i % 5 -eq 0 )
    {
        $setIdx++
        $sets[$setIdx] = @()
    }
    $sets[$setIdx] += $servers[$idx]
}

foreach( $set in $sets )
{
    Restart-Computer $set[0]
    Restart-Computer $set[1]
    Restart-Computer $set[2]
    Restart-Computer $set[3]
    Restart-Computer $set[4]
}

1 Comment

I briefly considered this in the beginning but the whole process is quite a bit more elaborate seeing as how these are Citrix servers and I have to ween users off each one. So in a group of 5, one server may take 20 minutes to ween users off and another server might take 24hrs to ween users off. Instead of waiting for the last server in a group of 5 to finish, even if the other 4 have been ready for possibly several hours I thought I would set up an array that I could moved finished servers out and then move new ones in. That way I could keep a constant flow of servers in and out of the array

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.