Answered by:
Scripting Newbie Needs Help

Question
-
Hello - I am trying to write a simple script that will ping an IP address. If it is not successful, then it will run 2 commands. See script below. I need to know what I am doing wrong. Thanks everyone!
{
If (-Not Test-Connection "192.168.1.1")
{
Restart-NetAdapter -Name "Wi-Fi" #Requires -RunAsAdministrator
}}
If (-Not Test-Connection "192.168.1.1")
{
Restart-NetAdapter -Name "Wi-Fi2" #Requires -RunAsAdministrator
}}Greg
Wednesday, November 21, 2018 4:18 PM
Answers
-
$MyComputers=Get-Content C:\temp\list.txt Foreach($Computer in $MyComputers){ #Ping test $Results=Test-Connection $Computer -Count 1 -ea 0 if($Results){ #Do Something Write-host "Ping Passed" }Else{ #Show Ping Failure Write-host "Ping Failed" } }
- Proposed as answer by ComputerScott Wednesday, November 21, 2018 7:32 PM
- Marked as answer by Richard MuellerMVP Friday, November 30, 2018 1:23 PM
Wednesday, November 21, 2018 4:45 PM
All replies
-
You need to post the code correctly using the code posting tool provided and you need to post the complete error message.
\_(ツ)_/
Wednesday, November 21, 2018 4:28 PM -
You also need to learn how to read the CmdLet help before asking a question and you need to learn basic PowerShell instead of guessing.
if(!Test-Connection '192.168.1.1' -Quiet){ Restart-NetAdapter -Name 'Wi-Fi' #Requires elevation } if(!Test-Connection '192.168.1.1' -Quiet){ Restart-NetAdapter -Name 'Wi-Fi2' #Requires elevation }
\_(ツ)_/
- Edited by jrv Wednesday, November 21, 2018 4:34 PM
Wednesday, November 21, 2018 4:32 PM -
Hope this helps
$MyComputers=Get-Content C:\temp\list.txt
Foreach($Computer in $MyComputers){
#Ping test
$Results=Test-Connection $Computer -Count 1 -ea 0
if($Results){
#Do Something
Write-host "Ping Passed"
}Else{
#Show Ping Failure
Write-host "Ping Failed"
}
}Please code in the code posting tool provided.
Look at my example above to learn both how to do this and how to post code in this forum.
\_(ツ)_/
- Proposed as answer by ComputerScott Wednesday, November 21, 2018 4:44 PM
- Unproposed as answer by ComputerScott Wednesday, November 21, 2018 4:45 PM
Wednesday, November 21, 2018 4:36 PM -
$MyComputers=Get-Content C:\temp\list.txt Foreach($Computer in $MyComputers){ #Ping test $Results=Test-Connection $Computer -Count 1 -ea 0 if($Results){ #Do Something Write-host "Ping Passed" }Else{ #Show Ping Failure Write-host "Ping Failed" } }
- Proposed as answer by ComputerScott Wednesday, November 21, 2018 7:32 PM
- Marked as answer by Richard MuellerMVP Friday, November 30, 2018 1:23 PM
Wednesday, November 21, 2018 4:45 PM -
Kind of new, figured it out, thanksWednesday, November 21, 2018 4:46 PM
-
Thanks for the help. I have worked on this for 16 hours, and nothing worked. I had a feeling it was a simple syntax type of problem. Not stupid, just not familiar with this language. I tried all kinds of help resources and googling, and was not able to find anything that worked. Will try this as soon as I can.
Sorry to have bothered you.
Greg
Wednesday, November 21, 2018 4:50 PM