返回列表 发帖

[问题求助] 用powershell判断网卡状态来连接wifi

是这样的, 我的电脑上安装有一个USB的无线网卡, 还有一个有线网卡, 我想实现一个功能,
如果有线网卡能正常上网, USB的无线网卡就连接名为AAA的无线网络(密码111),
如果有线网卡不能正常上网, USB的无线网卡就连接名为BBB的无线网络(密码222),
powershell或cmd能实现上述功能吗? 怎么判断有线网卡能不能上网呢? 还有连接wifi用什么命令呢

下面是ai写的, 不行
function Test-WiredConnection {
    $pingResult = Test-Connection -ComputerName 8.8.8.8 -Count 1 -ErrorAction SilentlyContinue
    return $pingResult.StatusCode -eq 0
}
function Connect-Wifi {
    param (
        [string]$SSID,
        [string]$Password
    )
    $profileXml = @"
<XML>
<WLANProfile xmlns="http://www.microsoft.com/networking/WLAN/profile/v1">
    <name>$SSID</name>
    <SSIDConfig>
        <SSID>
            <name>$SSID</name>
        </SSID>
    </SSIDConfig>
    <connectionType>ESS</connectionType>
    <connectionMode>manual</connectionMode>
    <MSM>
        <security>
            <authEncryption>
                <authentication>WPA2PSK</authentication>
                <encryption>AES</encryption>
                <useOneX>false</useOneX>
            </authEncryption>
            <sharedKey>
                <keyType>passPhrase</keyType>
                <protected>false</protected>
                <keyMaterial>$Password</keyMaterial>
            </sharedKey>
        </security>
    </MSM>
</WLANProfile>
</XML>
"@
    $profilePath = "$env:TEMP\$SSID.xml"
    $profileXml | Set-Content -Path $profilePath -Encoding UTF8
    netsh wlan add profile filename="$profilePath" > $null
    netsh wlan connect name=$SSID > $null
}
if (Test-WiredConnection) {
    Write-Host "Wired connection is active. Connecting to AAA..."
    Connect-Wifi -SSID "AAA" -Password "111"
} else {
    Write-Host "Wired connection is inactive. Connecting to BBB..."
    Connect-Wifi -SSID "BBB" -Password "222"
}COPY

TOP

返回列表