1

I was trying to execute x different server restarts with a PowerShell script, but as soon as it hits my for loop it just ends and I don't know where I made the mistake.

Code:

$x = Read-Host "How much you want to restart"

for ($i=0; $i -eq $x; $i++)
{
    $name = read-host "Enter Servername" $i "to restart"
    Restart-Computer -ComputerName $name -wait
    write-host "Server" $name "restarded"
}

Edit: thx to the answer I corrected the $i++ but still it does end immediately after the number is entered.

2
  • 3
    i++ --> $i++ ? Commented Aug 30, 2018 at 8:26
  • 1
    try for ($i=1; $i -eq $x ; $i++) instead. Your code starts counting from 0 whereas humans start from 1... Commented Aug 30, 2018 at 11:27

3 Answers 3

2

arco444's answer should fix your problem. However, maybe it's more interesting to write your code like this:

$serverList = @()

Do {
    $name = read-host "Enter Servername to restart"
    if ($name) {
        $serverList += $name
    }
} While ($name)

Foreach ($server in $serverList) {
    Restart-Computer -ComputerName $server -wait
    write-host "Server $server restarded"
}

This will keep asking for a servername, until you provide none. Then it will go ahead and restart the servers.

Or:

Do {
    $name = read-host "Enter Servername to restart"
    if ($name) {
        Restart-Computer -ComputerName $name -wait
        write-host "Server $name restarded"
    }
} While ($name)
Sign up to request clarification or add additional context in comments.

3 Comments

Thx for the quick answer, this looks way better, the only real thing is i can't analyze this to 100% correctly what makes it sadly not useable for me as i have to add more now.
If you mean you want to restart a server every time you enter a servername, you could do it like this. (I'll edit it into my answer)
This is handy and i can follow it more easily but i think i got the clue now. I didn't understand how arrays and how foreach is handled in powershell thats why i got some problems with this.
2

It might be helpful to encapsulate the rebooting code separately from the gathering of COMPUTERNAMEs. This would make it possible for the list of computers to be created from a script prompting for names or any other method.

When you are confident that the correct computers will be rebooted, remove the -WhatIf from the Restart-Computer cmdlet.

=== Do-Reboot.ps1

[cmdletbinding()]
Param (
    [Parameter(Mandatory = $true, Position = 0)]
    [string[]]$ComputerName
)

foreach ($Computer in $ComputerName) {
    Write-Information "reboot $Computer"
    Restart-Computer -ComputerName $Computer -Wait -WhatIf
}

Then, it can be used:

Do-Reboot -ComputerName SERVER1,SERVER2,SERVER3

If you want to see something on the console for each reboot, use:

Do-Reboot -ComputerName SERVER1,SERVER2,SERVER3 -InformationAction Continue

If you have the list of servers in a text file, this could be used.

Do-Reboot -ComputerName $(Get-Content -Path '.\rebootlist.txt')

Comments

1

Use @Michael B.'s answer to improve your code, but below you'll find the explanation why your code doesn't work as expected.


Your script will never enter the for loop due to incorrect condition.

Here you expect to execute the command block when $i is equal to $x:

for ($i=0; $i -eq $x; $i++)

As you initialize $i with the value 0, it'll check that the condition is false and never execute command block.

If you want the command block to be executed x times, your loop should look like:

for ($i=0; $i -lt $x; $i++) {
  # command block
}

Alternatively, you can use -ne instead of -lt.

Examples to check:


Working version

$x = 5
for ($i=0; $i -lt$x; $i++) {$i}

Output:

0
1
2
3
4

Non-working version

$x = 5
for ($i=0; $i -eq $x; $i++) {$i}

Output empty

1 Comment

aaah allright i see thank you for the reply and the lifting answer

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.