Asked by:
Powershell While/Until

Question
-
Probably a simple answer but...
Writing a simple script to test connectivity to a device. I need it to loop back and ask for another name of the connection fails OR let me know if it was successful.
If you have a successful attempt on the 1st try I cannot get it to write the verification. If it fails the 1st time, it will work perfect... le sigh
Here is what I have:
$GET_DEVICE_NAME = Read-Host "Please enter the device you verifing connection to"
$testConnection = Test-Connection $GET_DEVICE_NAME -Quiet
while($testConnection -eq $false)
{ Write-Host "Unable to locate $GET_DEVICE_NAME Please verify name and connectivity."
do{
$GET_DEVICE_NAME = Read-Host "Please enter the device you are backing up"
}
Until($testConnection=Test-Connection $GET_DEVICE_NAME -Quiet)
if($testConnection -eq $true)
{
Write-Host "$GET_DEVICE_NAME connection is verified"
break;}
}- Moved by Bill_Stewart Monday, July 29, 2019 8:01 PM Abandoned
Thursday, December 20, 2018 4:45 PM
All replies
-
Hi Luther ..
you got the script right. I do not see any issues with this. Let me know if you have any specific question.
Below is a nice article on Powershell using Do .. Until .. see if this useful for fine tuning your script.
https://blogs.technet.microsoft.com/heyscriptingguy/2014/05/06/powershell-looping-understanding-and-using-dountil/
Hope this helps...
---------------------------------------------------------------------------------------------------------
Please don't forget to “mark the replies as answers” if they helped, also set "like" it’s a boost for us to keep blogging J
Click here to learn more. Visit the dedicated Community forum to share, explore and talk to experts about Microsoft Kaizala.- Proposed as answer by Mike Crowley Thursday, December 20, 2018 9:07 PM
Thursday, December 20, 2018 4:52 PM -
It looks correct to me as well... however...
If I put a device in that will id true it "Should" go to
Write-Host "$GET_DEVICE_NAME connection is verified"
and it doesn't.
If you type in a false it will write-host "Please enter the device you verifying connection to" correctly.
After a one false, if you put in a true it will display Write-Host "$GET_DEVICE_NAME connection is verified" ??
I hope I am describing it correctly...
Thursday, December 20, 2018 5:59 PM -
I see .. Can you try this method ..
https://blogs.technet.microsoft.com/heyscriptingguy/2014/05/06/powershell-looping-understanding-and-using-dountil/
Thursday, December 20, 2018 6:09 PM