Hi everyone. I’m working on a script to build out remote servers. As part of that script, I create a new PSSession, join the server to the domain, and then reboot the server. I’m trying to get my script to reconnect to the remote server and reestablish a PSSession so that I can continue on.
If I run this block manually while the server is already up and pingable, it works fine and reestablishes a connection, and moves on as planned, but if I run it automatically as part of my script, and the server is still rebooting, it keeps looping through “Attempting to reconnect to $IPAddress…” and never actually reconnects, even if the server comes back up.
This is my first time trying to do something like this, so I’m sure I’m missing something. If someone could take a peek and give me some suggestions I’d really appreciate it!
if ((Test-Connection -ComputerName $IPAddress -Quiet)) { Write-Host Attempting to reconnect to $IPAddress... $Session = New-PSSession -Computer $IPAddress -Credential $Credential if ($Session -eq $null){ Write-Host "Failed to connect to $IPAddress" } else { Write-Host Connected to $IPAddress! } } else { do { Test-Connection -ComputerName $IPAddress -Quiet | out-null Write-Host Attempting to reconnect to $IPAddress... Start-Sleep 15 } until (Test-Connection -ComputerName $IPAddress -Quiet | out-null) }
Here’s the results: http://oi64.tinypic.com/t9h2fc.jpg
submitted by /u/cofonseca
[link] [comments]
The post Beginner requesting help: If/Else, Do/Until, and looping. appeared first on How to Code .NET.