IPv6
我的要求是如果已选中则取消选中,或者如果未选中则显示已未选中。
我尝试了下面的代码,但它没有给出任何输出,也没有给出任何错误。
$IPv6 = Get-NetAdapterBinding | Select-Object Name, DisplayName, ComponentID, Enabled | where { $_.Name -match 'Ethernet' -and $_.ComponentID -match 'ms_tcpip6' -and $_.Enabled -match 'False' }
If ($IPv6)
{
If ($IPv6.Name -match 'Wi-Fi' -and $IPv6.ComponentID -match 'ms_tcpip6' -and $IPv6.Enabled -match 'False')
{Write-Host 'IPv6 Already disabled for Ethernet & Wi-Fi Adapter' -ForegroundColor DarkYellow}
}else
{
Disable-NetAdapterBinding -Name * -DisplayName "Internet Protocol Version 6 (TCP/IPv6)"
Write-Host 'IPv6 disabled for Ethernet & Wi-Fi Adapter' -ForegroundColor Green
}
答案1
此 PowerShell 将显示已启用 IPv6 的网络适配器。它将在已启用 IPv6 的适配器上禁用 IPv6,然后显示已禁用 IPv6 的适配器(之前和之后)。
添加额外的条件逻辑来缩小细节是一件简单的事情,但这是用于所有启用的适配器的代码,将帮助您实现最终目标。
电源外壳
$N = Get-NetAdapterBinding | Select-Object Name, DisplayName, ComponentID, Enabled;
Write-Host "Before disabling IPv6 where enabled" -ForegroundColor Magenta;
$N | ForEach-Object {
If ($_.ComponentID -match 'ms_tcpip6' -and $_.Enabled -match 'False' ) { Write-Host "$($_.Name) ==> IPv6 Disabled" -ForegroundColor Yellow; };
If ($_.ComponentID -match 'ms_tcpip6' -and $_.Enabled -match 'True' ) {
Write-Host "$($_.Name) ==> IPv6 Enabled" -ForegroundColor Green;
Disable-NetAdapterBinding -Name "$($_.Name)" -ComponentID 'ms_tcpip6';};
};
$N = Get-NetAdapterBinding | Select-Object Name, DisplayName, ComponentID, Enabled;
Write-Host "";
Write-Host "After disabling IPv6 where enabled" -ForegroundColor Magenta;
$N | ForEach-Object {
If ($_.ComponentID -match 'ms_tcpip6' -and $_.Enabled -match 'False' ) { Write-Host "$($_.Name) ==> IPv6 Disabled" -ForegroundColor Yellow; };
If ($_.ComponentID -match 'ms_tcpip6' -and $_.Enabled -match 'True' ) {
Write-Host "$($_.Name) ==> IPv6 Enabled" -ForegroundColor Green;
};
};