【问题标题】:Powershell script to alternate enabling disabling a network cardPowershell脚本交替启用禁用网卡
【发布时间】:2020-03-08 02:44:01
【问题描述】:

我进行了一些搜索并将以下脚本放在一起,但它没有做任何事情。它将显示禁用互联网,但随后关闭,实际上并没有禁用卡。 任何帮助将不胜感激。

# This Powershell Script will alternate enabling and disabling the Network Card
# Change NAME OF NETWORK CONNECTION with the actual name of your network connection from control panel

$up = "Up"
$disconnected="Disconnected"
$lan = "NAME OF NETWORK CONNECTION"

$landown = Get-NetAdapter | select Name,Status | where { $_.Status -match $disconnected -and $_.Name -match $lan }
$lanUp = Get-NetAdapter | select Name,Status | where { $_.Status -match $up -and $_.Name -match $lan }

if ($lanUp)
{
    Write-Host("Disabling Internet")
    Disable-NetAdapter -Name $lan -Confirm:$false
}
elseif ($landown) 
{
    Write-Host("Enabling Internet")
    Enable-NetAdapter -Name $lan -Confirm:$false
}
Exit

============================ 原帖

我对 powershell 了解不多。我使用了 devcon 和一个脚本,我创建了一个快捷方式,该快捷方式将在多个桌面上交替启用和禁用网卡,以便在不需要时轻松断开与 Internet/网络的连接。自从从win 7升级到10后,一台计算机偶尔无法在不重新启动的情况下重新启用它的网卡(即使进入设备管理器。禁用后它偶尔会停止工作)。 reddit 上的某个人认为这可能是 devcon 以及它如何卸载驱动程序的问题。建议使用 powershell 脚本。

我已经搜索并找到了启用或禁用网络适配器以及如何获取适配器信息的命令。但我正在寻找一个脚本来检查适配器的状态,并根据当前状态启用或禁用它。并希望这能阻止问题的发生。

任何帮助将不胜感激。

谢谢。

【问题讨论】:

  • 使用这个搜索... 'Powershell disable network card' powershellgallery.com,你会找到示例,或者只使用 cmdlet --- Disable-NetAdapter --- docs.microsoft.com/en-us/powershell/module/netadapter/… --- Enable-NetAdapter --- docs.microsoft.com/en-us/powershell/module/netadapter/… --- 否则,您要求为您完成工作。这不是人们在这里做的。你必须展示你的工作、错误等,更多的东西会倾向于提供帮助。
  • 很抱歉。我在上面添加了一些代码并寻求帮助。我希望这足以重新打开我的帖子并允许人们发表评论。我不确定这里的一切是如何运作的。谢谢你的回复。

标签: powershell


【解决方案1】:

您的代码对我来说工作正常,但重新启用网络适配器时除外。当接口未启动时,您为 .Status 使用了错误的字符串。如果您查看已禁用的网络适配器(提示:Disable-NetAdapter),您会看到 Status 属性的值为“已禁用”,而不是“已断开”。这就是为什么它在您的界面“启动”时有效,但在“禁用”时无效。将您的代码替换为以下内容:

$up = "Up"
$disabled = "Disabled"
$lan = "NAME OF NETWORK CONNECTION"

$landown = Get-NetAdapter | select Name,Status | where { $_.Status -match $disabled -and $_.Name -match $lan }
$lanUp = Get-NetAdapter | select Name,Status | where { $_.Status -match $up -and $_.Name -match $lan }

if ($lanUp)
{
    Write-Host("Disabling Internet")
    Disable-NetAdapter -Name $lan -Confirm:$false
}
elseif ($landown) 
{
    Write-Host("Enabling Internet")
    Enable-NetAdapter -Name $lan -Confirm:$false
}
Exit

如果它没有按预期开始工作,我会先解决这个问题,然后进一步解决其他问题(例如您对驱动程序的理论)。

【讨论】:

  • 谢谢我错过了。它仍然无法正常工作,但我意识到它没有以管理员身份运行。所以我创建了一个快捷方式并将其设置为以管理员身份运行。现在,当我运行快捷方式并在 UAC 提示符上单击“确定”时,脚本就可以工作了。我注意到了两件事。在这台计算机上,重新启动后,适配器显示为不存在,而不是禁用。一旦启用并再次禁用,则显示为禁用。此外,powershell 窗口没有关闭,所以我不得不添加另一行(经过研究使其关闭。我的最终脚本如下。谢谢您的帮助。
【解决方案2】:

最终脚本。当我为其创建快捷方式并将快捷方式设置为以管理员身份运行时有效。 谢谢

# This Powershell Script will alternate enabling and disabling the Network Card
# Change NAME OF NETWORK CONNECTION with the actual name of your network connection from control panel
# Example "Local Area Network"

$up = "Up"
$disabled = "Disabled"
$notpresent = "Not Present"
$lan = "NAME OF NETWORK CONNECTION"

$landown = Get-NetAdapter | select Name,Status | where { $_.Status -match $disabled -and $_.Name -match $lan }
$landown2 = Get-NetAdapter | select Name,Status | where { $_.Status -match $notpresent -and $_.Name -match $lan }
$lanUp = Get-NetAdapter | select Name,Status | where { $_.Status -match $up -and $_.Name -match $lan }

if ($lanUp)
{
    Write-Host("Disabling Internet")
    Disable-NetAdapter -Name $lan -Confirm:$false
}
elseif ($landown) 
{
    Write-Host("Enabling Internet")
    Enable-NetAdapter -Name $lan -Confirm:$false
}
elseif ($landown2) 
{
    Write-Host("Enabling Internet")
    Enable-NetAdapter -Name $lan -Confirm:$false
}
stop-process -Id $PID
Exit

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多