Here is the code we are using:
$global:nics = Get-WmiObject Win32_NetworkAdapterConfiguration
$global:wirelessNICS = @()
$global:nonWirelessNICS = @()
$global:OSVersion = [System.Environment]::OSVersion.Version.Major
$global:counter = 0
function Get-NICS {
$global:nics = Get-WmiObject Win32_NetworkAdapterConfiguration
for ($i = 0; $i -lt $global:nics.Count; $i++) {
$DHCPEnabled = $global:nics[$i].DHCPEnabled
$IPAddress = $global:nics[$i].IPAddress
$DefaultIPGateway = $global:nics[$i].DefaultIPGateway
$DNSDomain = $global:nics[$i].DNSDomain
$ServiceName = $global:nics[$i].ServiceName
$Description = $global:nics[$i].Description
$Index = $global:nics[$i].Index
if (($Description -like '*Wireless*') -or ($Description -like '*802.11*')){
#Write-Host "Wireless adapter $Description found at array index $Index "
$global:wirelessNICS += $Index
}
else {
#Write-Host "Didn't find it for $Description at array index $Index"
}
}#end FOR
for ($i = 0; $i -lt $global:nics.Count; $i++) {
$DHCPEnabled = $global:nics[$i].DHCPEnabled
$IPAddress = $global:nics[$i].IPAddress
$DefaultIPGateway = $global:nics[$i].DefaultIPGateway
$DNSDomain = $global:nics[$i].DNSDomain
$ServiceName = $global:nics[$i].ServiceName
$Description = $global:nics[$i].Description
$Index = $global:nics[$i].Index
$IPEnabled = $global:nics[$i].IPEnabled
if (($Description -notlike '*Wireless*') -and ($Description -notlike '*802.11*') -and ($Description -notlike '*Cisco Systems VPN*') -and ($IPEnabled -eq 'True') -and ($Description -notlike '1394*') -and ($Description -notlike
'*Juniper*')) {
#Write-Host "Non wireless adapter $Description found at array index $Index "
$global:nonWirelessNICS += $Index
}
else {
#Write-Host "Didn't find it for $Description at array index $Index"
}
}#end FOR
}#end function Get-NICS
function IsWiredConnected {
for ($i = 0; $i -lt $nonWirelessNICS.Count; $i++) {
$CurrentNIC = $nonWirelessNICS[$i]
$ConnectionStatus = Get-WMIObject Win32_NetworkAdapter | Where-Object {$_.Index -eq $CurrentNIC} | Select-Object Name,NetConnectionStatus
if ($ConnectionStatus.NetConnectionStatus -eq 2) {
return $true
}
else {
return $false
}
}#end FOR
}#end function IsWiredConnected
function IsWirelessConnected {
for ($i = 0; $i -lt $wirelessNICS.Count; $i++) {
$CurrentWirelessNIC = $wirelessNICS[$i]
$ConnectionStatus = Get-WMIObject Win32_NetworkAdapter | Where-Object {$_.Index -eq $CurrentWirelessNIC} | Select-Object Name,NetConnectionStatus
if ($ConnectionStatus.NetConnectionStatus -eq 2) {
return $true
}
else {
return $false
}
}#end FOR
}#end function IsWirelessConnected
function IsWirelessDisabled {
for ($i = 0; $i -lt $wirelessNICS.Count; $i++) {
$CurrentWirelessNIC = $wirelessNICS[$i]
$ConnectionStatus = Get-WMIObject Win32_NetworkAdapter | Where-Object {$_.Index -eq $CurrentWirelessNIC} | Select-Object Name,NetConnectionStatus
if (($ConnectionStatus.NetConnectionStatus -eq 0) -or ($ConnectionStatus.NetConnectionStatus -eq 4)){
return $true
}
else {
return $false
}
}#end FOR
}#end function IsWirelessDisabled
function ChangeNICStatus {
PARAM ($ifname = $(throw "Specify 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
}
}
}#end function ChangeNICStatus
function DisableWirelessNICs {
for ($i = 0; $i -lt $wirelessNICS.Count; $i++) {
$CurrentWirelessNIC = Get-WMIObject Win32_NetworkAdapter | Where-Object {$_.Index -eq $wirelessNICS[$i]}
ChangeNICStatus($CurrentWirelessNIC.NetConnectionID)
}#end FOR
}#end function DisableWirelessNICs
function EnableWirelessNICs {
for ($i = 0; $i -lt $wirelessNICS.Count; $i++) {
$CurrentWirelessNIC = Get-WMIObject Win32_NetworkAdapter | Where-Object {$_.Index -eq $wirelessNICS[$i]}
ChangeNICStatus($CurrentWirelessNIC.NetConnectionID)
}#end FOR
}#end function EnableWirelessNICs
function DisableWirelessNICs {
for ($i = 0; $i -lt $wirelessNICS.Count; $i++) {
$CurrentWirelessNIC = Get-WMIObject Win32_NetworkAdapter | Where-Object {$_.Index -eq $wirelessNICS[$i]}
ChangeNICStatus($CurrentWirelessNIC.NetConnectionID)
}#end FOR
}#end function DisableWirelessNICs
while(1 -eq 1) {
Get-NICS
if ((IsWiredConnected) -and (-Not (IsWirelessDisabled)) -and ($global:WirelessNICs.Count -gt 0)) {
DisableWirelessNICs
}
if ((-Not(IsWiredConnected)) -and (IsWirelessDisabled) -and ($global:WirelessNICs.Count -gt 0)) {
EnableWirelessNICs
}
if ($global:counter -eq 60) {
$hostname = hostname
$getDate = Get-Date
$timeStamp = [string]::Format("{0} {1}",$getDate.ToShortDateString(),$getDate.ToShortTimeString())
$finalString = "$hostname,$timeStamp"
$finalString | Out-File "C:\Program Files\Manage-NICs\Manage-NICs-Log.log"
$global:counter = 0
}
else {
$global:counter++
}
$global:wirelessNICS = @() #assigns a new/empty array for the next loop.
$global:nonWirelessNICS = @() #assigns a new/empty array for the next loop.
Start-Sleep -Seconds 5
}#end WHILE