Loop to keep flushing DNS cache until Test-Connection returns true

I am stuck trying to figure this one out. I am building a script to automate printer installations. I have most of the script working, but one of the issues I have in my environment is that sometimes when setting the DNS record it takes a few minutes the DNS changes to be reflected on the print server. This is even after running an ipconfig /flushdns. Usually, when I update the dns record, I have to try flushing the DNS cache a few times before the device is pingable by host name.

I thought it would be really cool to build into the script a do until, or do while loop of some sorts that firsts flushes the dns cache on the remote print server. I have tested the below successfully for flushing dns cache

Invoke-WmiMethod -class Win32_process -name Create -ArgumentList (“cmd.exe /c ipconfig /flushdns”) -ComputerName $Server

Next I would want to have the remote print server ping the printer by host name. I have tested the command below successfully

(Invoke-Command -computername $Server { test-connection -ComputerName $PrinterName }

If the ping test fails. I would want to run the flush the dns cache then try another ping, and keep looping until the printer is finally online, and then I would run the rest of the script to add the printer port and printer.

Is there any way I can do this? Everything I’ve been reading on test-connection loops has you run the test-connection on the do section with a quiet switch and stored in a variable, and then run “until” the test connection variable equals true.

submitted by /u/DNM13
[link] [comments]

Leave a Reply