19 July 2013

Powershell: Reset Network Adaptor with Connectivity Verification

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.

You can download the script from here.

 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  

Related Posts:

  • Adding a User to the Local Administrators Group with Verification This script will add a user to the local administrators group. It will also verify if the user is added and write both a registry key for the add/remove programs and a key for the WMI entry so that it will appear in a WMI q… Read More
  • Get the Software GUID This script will get the proper software name and associated GUID. I use this script when writing uninstaller powershell scripts. You can use the GUID in an msiexec so that you do not need the source files for the uninstall… Read More
  • Autodesk 2014 Uninstaller Here is a script that will uninstall Autodesk 2014. This will uninstall BDS Ultimate, which should cover all Revit 2014 applications. This also uninstalls Civil 3D 2014, but does not cover anything else from the IDSP suite.… Read More
  • Verify SCCM can see Application GUIDs I was deploying an application through SCCM and it continued to fail every time, although the app was completely installed. I knew it had to be with the verification process. I went through and entered all of the GUIDs for … Read More
  • Autodesk 2014 Building Design Suite Ultimate Uninstaller The Autodesk suite is not the easiest to uninstall because of all the components that the built in uninstaller does not uninstall. I went in and extracted all of the GUIDs for each components and created the powershell scr… Read More

0 comments:

Post a Comment