PoshCode Archive  Artifact [b6d9bf6173]

Artifact b6d9bf6173c2eeb573afa7462404281a83ba8c72d855679c8fb77f14bf722ff3:

  • File Enable-Disable-NIC-XP.ps1 — part of check-in [9908d48c23] at 2018-06-10 13:01:24 on branch trunk — Enabling/Disabling network adapter. Works on Windows XP and higher. (user: Kris Cieslak size: 2466)

# encoding: ascii
# api: powershell
# title: Enable/Disable NIC, XP+
# description: Enabling/Disabling network adapter. Works on Windows XP and higher.
# version: 0.1
# type: script
# author: Kris Cieslak 
# license: CC0
# x-poshcode-id: 1800
# x-archived: 2011-08-10T11:06:52
# x-published: 2011-04-21T13:00:00
#
#
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#          Name: ifstate
#        Author: Kris Cieslak (defaultset.blogspot.com)
#          Date: 2010-04-21
#   Description: Enabling/Disabling network adapter.
#                Works on Windows XP and higher.
#                If your os is WinXP and its language is not English,
#                you'll have to change values in $UpStateLabel and $DownStateLabel 
#
#    Parameters: network interface name,
#                state [up/down] (optional, if up then down, or if down then up :))
# Usage example: 
#                ./ifstate 'Local network connection' down
#            or
#                ./ifstate 'Local network connection'  
#
# -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
PARAM ($ifname = $(throw "Specifiy interface name"),$state = "")
trap [Exception] {
    Write-Host 'Ups! Something wrong.' -ForegroundColor Red
	continue;
}

# These values depends on your os language
$UpStateLabel = 'En&able';
$DownStateLabel = 'Disa&ble';

$st = 0;
if ($state.length -gt 0) {
  switch ($state.ToLower()) {
       'up' { $st = 1 }
	   'down' {$st = 0 }
  }
} else {
  $c =(gwmi Win32_NetworkAdapter | ? { $_.NetConnectionID -eq $ifname }).ConfigManagerErrorCode;
  if ($c -eq 22) { $st = 1 } else { $st = 0 }
}

if ($st -eq 1) {
    $StateLabel = $UpStateLabel;
} else {
    $StateLabel = $DownStateLabel;
}

if ([int](([regex]('\d{1,3}')).match((gwmi win32_OperatingSystem).Version).ToString()) -le 5) {
    $shell = New-Object -comObject Shell.Application;
    $test=(($shell.NameSpace(3).Items() | 
	    ? { $_.Path -like '*7007ACC7-3202-11D1-AAD2-00805FC1270E*'}).GetFolder.Items() |
		? { $_.Name -eq $ifname }).Verbs() | ? { $_.Name -eq $StateLabel }
	if ($test -ne $null) { 
	   ($test).DoIt() 
	}
} else {
     if ($st -eq 1) {
        (gwmi Win32_NetworkAdapter | ? { $_.NetConnectionID -eq $ifname } ).Enable() | Out-Null
	 } else {
	    (gwmi Win32_NetworkAdapter | ? { $_.NetConnectionID -eq $ifname } ).Disable() | Out-Null
	 }
}