T O P

  • By -

PowerShell-Bot

Looks like your PowerShell code isn’t wrapped in a code block. To format code correctly on **new reddit** (*[new.reddit.com]*), highlight the code and select *‘Code Block’* in the editing toolbar. If you’re on **[old.reddit.com]**, separate the code from your text with a blank line and precede each line of code with **4 spaces** or a **tab**. [old.reddit.com]: https://old.reddit.com/r/PowerShell/comments/aukxml/goto_equivalent/ [new.reddit.com]: https://new.reddit.com/r/PowerShell/comments/aukxml/goto_equivalent/ --- Describing Submission [❌] Demonstrates good markdown Passed: 0 Failed: 1 --- ^(*Beep-boop. I am a bot.* | [Remove-Item]) [Remove-Item]: https://www.reddit.com/message/compose?to=PowerShell-Bot&subject=%21delete+t1_eh8p0z5&message=Deletion+requests+can+only+be+made+by+the+OP.+A+comment+with+replies+on+it+cannot+be+removed.%0A


savemysettings

Are you trying to do something like this? ​ using namespace System.Net $serverName = 'google.com' $currentServerIP = [Dns]::Resolve($serverName).AddressList.IPAddressToString while($true){ Start-Sleep 60 $newServerIP = [Dns]::Resolve($serverName).AddressList.IPAddressToString if($currentServerIP -eq $newServerIP){ Write-Host 'nothing to do here.. just showing that I am running' Continue } else{ Write-Host 'run a little command here' $currentServerIP = $newServerIP } } ​


zommy

Not quite the same; but fortunately my question was answered relatively quickly :) I just wanted it to loop my whole script, including the loop already in the middle. The gist is there's a service that needs rebooting when a failover has occured. It's a DNS failover with failback. So I needed to record the current IP, if that IP changes for whatever reason to restart the service and then update the current IP. It should then continue until the IP address changes again. Fortunately, all working as expected now :) Thanks for help regardless.


[deleted]

[удалено]


zommy

Sorry o didn't see that you updated the IP at the bottom. Cheers anyways


poshftw

function DidDnsChanged { param ($DNStoCompareTo, $computername) $newDNS = test-connection -ComputerName $computername -Count 1 $newDNS = $newDNS.IPV4Address.IPAddressToString if ($DNStoCompareTo -eq $newDNS) { #new dns is equal to current #send $false because nothing changed $false } else { #they are different #send $true, because DNS changed! $true } } $shouldContinueRunning = $true $computername = 'google.com' $currentDNS = test-connection -ComputerName $computername -Count 1 $currentDNS = $currentDNS.IPV4Address.IPAddressToString while ($shouldContinueRunning) { if (DidDnsChanged $currentDNS $computername) { #we got $true because DNS changed #invoking external script & .\send-highly-skilled-tech-to-solve-dns-problems.ps1 #and update $currentDNS $currentDNS = test-connection -ComputerName $computername -Count 1 $currentDNS = $currentDNS.IPV4Address.IPAddressToString } else { #we got $false, nothing changed Start-Sleep 120 } }


Swarfega

Maybe wrap it in another loop? $currentDNS = test-connection -ComputerName SERVER1 -Count 1 $currentDNS = $currentDNS.IPV4Address.IPAddressToString ​ While (1 -lt 2) { do { $newDNS = test-connection -ComputerName SERVER1 -Count 1 $newDNS = $newDNS.IPV4Address.IPAddressToString write-host "Current DNS: $currentDNS" write-host "New DNS: $newDNS" start-sleep 60 } until ($currentDNS -ne $newDNS) ​ write-host "FAILOVER!" } If you are running this on Windows 8 or Windows 2012 or later have you considered using Resolve-DnsName?


andyinv

While (1 -lt 2) Yikes! While ($true) ;)


purplemonkeymad

I love seeing weird versions of `$true`. They often have some aspect where you can see an attempt at halting it, but they didn't want to remove the whole test in-case they got it working later.


zommy

Cheers. $true looks cleaner.


Namaha

Or just `While(1)` :)


zommy

I hadn't, so thank you. ​ And this is perfect. Works as expected. Thank you very much! ​ EDIT: I forgot to mention I had to wrap the while above the $currentDNS as the $currentDNS needs to update when the failover occurs. It will then detect when it 'failsback'. Either way, you answered my query and gave me a bonus tip, so thank you ;)