Skip to content

Instantly share code, notes, and snippets.

@kagarlickij
Created January 8, 2017 17:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kagarlickij/3d3540b1d9a00b7c4e816d55750e3650 to your computer and use it in GitHub Desktop.
Save kagarlickij/3d3540b1d9a00b7c4e816d55750e3650 to your computer and use it in GitHub Desktop.
This script will restart network adapter
<#
.NOTES
===========================================================================
Created with: SAPIEN Technologies, Inc., PowerShell Studio 2016 v5.2.117
Created on: 17.03.2016
Created by: Dmitriy Kagarlickij
Contact: dmitriy@kagarlickij.com
===========================================================================
.DESCRIPTION
This script should be executing with Administrator-level permissions
#>
#Requires -RunAsAdministrator
# Set variables
$AdapterName = "Ethernet"
$LogFile = "C:\Users\Administrator\RestartNetAdapter$AdapterName.log"
# Check net adapter current status
if ( $((Get-NetAdapter -Name $AdapterName)."Status") -ne 'Up' )
{
Write-Output "Net adapter is disabled or absent. Script will be stopped."
exit
}
# Remove old log file if it's present
if (Test-Path $LogFile)
{
Remove-Item $LogFile
}
# Create new log file
Write-Output "Script has been started at $(Get-Date -Format g)" | Out-File $LogFile
# Get current IPAddr
$firstIP = $((Get-NetIPAddress -InterfaceAlias $AdapterName -AddressFamily "IPv4").IPAddress)
Write-Output "IPAddr before adapter restart = $firstIP" | Out-File $LogFile -Append
# Quick net adapter restart
Write-Output "Quick net adapter restart has been initiated" | Out-File $LogFile -Append
Restart-NetAdapter -Name $AdapterName -Confirm:$false
Start-Sleep -Seconds 5
# Check net adapter current status and start slow restart in case of necessarity
if ($((Get-NetAdapter -Name $AdapterName)."Status") -ne 'Up')
{
Write-Output "Slow net adapter restart has been initiated" | Out-File $LogFile -Append
Disable-NetAdapter -Name $AdapterName -Confirm:$false
Start-Sleep -Seconds 30
Enable-NetAdapter -Name $AdapterName -Confirm:$false
}
# Final check net adapter current status
if ($((Get-NetAdapter -Name $AdapterName)."Status") -ne 'Up')
{
Write-Output "Net adapter has been restarted with error" | Out-File $LogFile -Append
}
else
{
Write-Output "Net adapter has been restarted successfully" | Out-File $LogFile -Append
}
# Get current IPAddr
$secondIP = $((Get-NetIPAddress -InterfaceAlias $AdapterName -AddressFamily "IPv4").IPAddress)
Write-Output "IPAddr after adapter restart = $secondIP" | Out-File $LogFile -Append
# Compare IPAddr
if ($secondIP -eq $firstIP)
{
Write-Output "After net adapter restart IPAddr was not chahged" | Out-File $LogFile -Append
}
else
{
Write-Output "After net adapter restart IPAddr was chahged" | Out-File $LogFile -Append
}
# Variables cleanup
Remove-Variable -Name * -ErrorAction SilentlyContinue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment