19 July 2013

Powershell: Reset Network Adaptor with Connectivity Verification

Posted By: Mick Pletcher - 10:27 AM

Share

& Comment

Here is a script that will disable and then re-enable the network interface on your pc. It will also verify if the network connection is re-established by testing the connection to google.com.


 cls  
 #Declare Global Variables  
 Set-Variable -Name Computer -Scope Global -Force  
 Set-Variable -Name Active -Scope Global -Force  
 Set-Variable -Name Network -Scope Global -Force  
 Set-Variable -Name Networks -Scope Global -Force  
 Set-Variable -Name Output -Scope Global -Force  
 Set-Variable -Name RetVal -Scope Global -Force  
 $Computer = gc env:computername  
 $Networks = Get-WmiObject Win32_NetworkAdapter -ComputerName $Computer  
 Foreach ($Network in $Networks) {  
      If ($Network.NetEnabled -eq $true) {  
           $Active = $Network  
           Write-Host "Network Adaptor"  
           Write-host "   "$Network.Description  
           Write-Host  
           $RetVal = $Network.Disable()  
           If ($RetVal.ReturnValue -eq 0) {  
                Write-Host "Network adaptor is disabled"  
           }  
           Start-Sleep -Seconds 5  
           $RetVal = $Network.Enable()  
           If ($RetVal.ReturnValue -eq 0) {  
                Write-Host "Network adaptor is re-enabled"  
           }  
           Start-Sleep -Seconds 30  
           $Output = Test-Connection google.com -Quiet  
           If ($Output -eq $true) {  
                Write-Host "Network adaptor Restored"  
           } else {  
                Write-Host "Network adaptor unavailable"  
           }  
      }  
 }  
 Remove-Variable -Name Active -Scope Global -Force  
 Remove-Variable -Name Computer -Scope Global -Force  
 Remove-Variable -Name Network -Scope Global -Force  
 Remove-Variable -Name Networks -Scope Global -Force  
 Remove-Variable -Name Output -Scope Global -Force  
 Remove-Variable -Name RetVal -Scope Global -Force  

About Mick Pletcher

My blog is here to help solve issues I have encountered and solved, publish scripts I have written, and educate others in understanding areas that are not well covered

0 comments:

Post a Comment

Copyright © 2013 Mick's IT Blogs™ is a registered trademark.