Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for restart_ethernet.ps

(view source code of restart_ethernet.ps as plain text)

  1. <#
  2. .SYNOPSIS
  3. Restart Ethernet network adapter
  4.  
  5. .DESCRIPTION
  6. If run with elevated privileges, this script will disable the Ethernet network adapter on the local computer, wait for a specified number of seconds (default: 5), and then reenable the Ethernet network adapter; before and after each step the Ethernet network adapter's status is shown.
  7. If run without elevated privileges, this script will only show the Ethernet network adapter's status.
  8.  
  9. .PARAMETER Delay
  10. The number of seconds between disabling/enabling the network card and checking the new status
  11.  
  12. .PARAMETER Version
  13. Show this script's version number.
  14. If combined with -Verbose show full script path, version number and last/modified/release date.
  15.  
  16. .PARAMETER Verbose
  17. Show verbose version info when -Version switch is used.
  18.  
  19. .EXAMPLE
  20. . './Restart_Ethernet.ps1'
  21.  
  22. If run with elevated privileges, output will look like this:
  23.  
  24. Ethernet adapter status: Up
  25. Ethernet adapter status: Disabled
  26. Ethernet adapter status: Up
  27.  
  28. If run without elevated privileges, output will look like this:
  29.  
  30. Ethernet adapter status: Up
  31.  
  32. .LINK
  33. script written by Rob van der Woude
  34. https://www.robvanderwoude.com/
  35. #>
  36.  
  37. param(
  38. 	[ValidateRange(5,60)]
  39. 	[Int]$Delay = 5,
  40. 	[switch]$Version,
  41. 	[switch]$Verbose
  42. )
  43.  
  44. $progver = '1.01'
  45.  
  46. if ( $Version ) {
  47. 	if ( $Verbose ) {
  48. 		$lastmod = ( [System.IO.File]::GetLastWriteTime( $PSCommandPath ) )
  49. 		if ( $lastmod.ToString( "h.mm" ) -eq $progver ) {
  50. 			"`"{0}`", Version {1}, release date {2}" -f $PSCommandPath, $progver, $lastmod.ToString( "yyyy-MM-dd" )
  51. 		} else {
  52. 			# if last modified time is not equal to program version, the script has been tampered with
  53. 			"`"{0}`", Version {1}, last modified date {2}" -f $PSCommandPath, $progver, $lastmod.ToString( "yyyy-MM-dd" )
  54. 		}
  55. 	} else {
  56. 		$progver
  57. 	}
  58. 	exit 0
  59. }
  60.  
  61. Write-Host ( "Ethernet adapter status: {0}" -f ( Get-NetAdapter -Name 'Ethernet' ).Status )
  62. if ( ( [Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent( ) ).IsInRole( [Security.Principal.WindowsBuiltInRole]::Administrator ) ) {
  63. 	Write-Host "Disabling Ethernet adapter . . ."
  64. 	Disable-NetAdapter -Name 'Ethernet' -AsJob | Out-Null
  65. 	Start-Sleep $Delay
  66. 	Write-Host ( "Ethernet adapter status: {0}" -f ( Get-NetAdapter -Name 'Ethernet' ).Status )
  67. 	Write-Host "Enabling Ethernet adapter . . ."
  68. 	Enable-NetAdapter -Name 'Ethernet' -AsJob | Out-Null
  69. 	Start-Sleep $Delay
  70. 	Write-Host ( "Ethernet adapter status: {0}" -f ( Get-NetAdapter -Name 'Ethernet' ).Status )
  71. }

page last modified: 2024-04-16; loaded in 0.0167 seconds