在Windows编程中以编程方式添加带密码的WiFi配置文件

13

是否可以通过编程的方式将WiFi配置文件添加到Windows操作系统(最低版本为Windows 7)中?

我尝试使用netsh的add profileconnect,但对我无效。是否有任何PowerShell命令可以完成这个任务?

我想要自动地为多个客户端设置一个特定SSID的WiFi密码。

希望有人能够提供一个命令,包含以下示例信息:

  • SSID:WifiNetwork
  • 密码:Password123

谢谢!

3个回答

24

我找到了一种添加Wi-Fi配置文件的方法。

首先,您需要导出现有的Wi-Fi配置文件:

I found a way to add a wifi profile.

At first you export an existing wifi profile:

netsh wlan export profile name="WifiNetwork" folder="C:\path\" key=clear

然后您将获得一个具有以下样式的XML文件:

<?xml version="1.0"?>
<WLANProfile xmlns="http://www.microsoft.com/networking/WLAN/profile/v1">
    <name>WifiNetwork</name>
    <SSIDConfig>
        <SSID>
            <hex>576966694E6574776F726B</hex>
            <name>WifiNetwork</name>
        </SSID>
    </SSIDConfig>
    <connectionType>ESS</connectionType>
    <connectionMode>auto</connectionMode>
    <MSM>
        <security>
            <authEncryption>
                <authentication>WPA2PSK</authentication>
                <encryption>AES</encryption>
                <useOneX>false</useOneX>
            </authEncryption>
            <sharedKey>
                <keyType>passPhrase</keyType>
                <protected>false</protected>
                <keyMaterial>Password123</keyMaterial>
            </sharedKey>
        </security>
    </MSM>
</WLANProfile>

你可以修改这个文件,然后使用以下命令导入以添加这个wifi:

netsh wlan add profile filename="C:\path\WifiNetwork.xml"

使用以下方式检查您的个人资料:

netsh wlan show profile

使用关键字检查您的个人资料:

netsh wlan show profiles WifiNetwork key=clear

我希望这些信息能够帮助到某人。


4
请注意:如果您想手动编辑配置文件,请确保更新<ssidconfig> <ssid>内部的<hex>元素。它需要是SSID的HEX表示形式。可以使用Powershell生成此内容:("WifiNetwork".ToCharArray() |foreach-object {'{0:X}' -f ([int]$_)}) -join '' - Per von Zweigbergk
3
@PervonZweigbergk 十六进制或名称是可选的。“虽然十六进制和名称元素是可选的,但至少一个十六进制或名称元素必须作为SSID元素的子元素出现。” 参考:https://learn.microsoft.com/en-us/windows/win32/nativewifi/wlan-profileschema-hex-ssid-element - Jimmy Westberg

7

我写了一个PowerShell脚本 - 以下代码的前三行尚未经过测试,因为在我的脚本中,我从CSV文件中获取它 - 其余部分是原样的 - 并且可以使用我拥有的两个 SSID。

$profilefile="ACprofile.xml"
$SSID="ACSSID"
$PW="12345678"

$SSIDHEX=($SSID.ToCharArray() |foreach-object {'{0:X}' -f ([int]$_)}) -join''
$xmlfile="<?xml version=""1.0""?>
<WLANProfile xmlns=""http://www.microsoft.com/networking/WLAN/profile/v1"">
    <name>$SSID</name>
    <SSIDConfig>
        <SSID>
            <hex>$SSIDHEX</hex>
            <name>$SSID</name>
        </SSID>
    </SSIDConfig>
    <connectionType>ESS</connectionType>
    <connectionMode>auto</connectionMode>
    <MSM>
        <security>
            <authEncryption>
                <authentication>WPA2PSK</authentication>
                <encryption>AES</encryption>
                <useOneX>false</useOneX>
            </authEncryption>
            <sharedKey>
                <keyType>passPhrase</keyType>
                <protected>false</protected>
                <keyMaterial>$PW</keyMaterial>
            </sharedKey>
        </security>
    </MSM>
</WLANProfile>
"

$XMLFILE > ($profilefile)
netsh wlan add profile filename="$($profilefile)"
netsh wlan show profiles $SSID key=clear
netsh wlan connect name=$SSID

1
不要忘记在脚本中更改变量SSID和密码,它不会要求输入。 - XLR8

0

我使用了这里的代码,但是当我尝试添加配置文件时出现了错误,因为我们的密码包含了一个“无效字符”。(我得到的错误是“尝试加载具有不正确格式的程序”并在谷歌搜索后找到了原因。)所以我添加了一些逻辑来替换那些不可接受的字符并使它们变得可接受。

它还强制将XML文件创建在与脚本相同的文件夹中(由于某种原因,如果没有这个,它会给我带来问题。)

最后,我添加了一个(注释掉的)user=all命令,可以轻松取消注释,以便您可以为所有用户而不仅仅是运行脚本的用户添加此配置文件。(我正在使用。)

$profilefile = "ACprofile.xml"
$SSID = "WifiNetwork"
$PW = "Password1234"

function Get-ScriptPath()
{
    # If using PowerShell ISE
    if ($psISE)
    {
        $ScriptPath = Split-Path -Parent -Path $psISE.CurrentFile.FullPath
    }
    # If using PowerShell 3.0 or greater
    elseif($PSVersionTable.PSVersion.Major -gt 3)
    {
        $ScriptPath = $PSScriptRoot
    }
    # If using PowerShell 2.0 or lower
    else
    {
        $ScriptPath = split-path -parent $MyInvocation.MyCommand.Path
    }

    # If still not found
    # I found this can happen if running an exe created using PS2EXE module
    if(-not $ScriptPath) {
        $ScriptPath = [System.AppDomain]::CurrentDomain.BaseDirectory.TrimEnd('\')
    }

    # Return result
    return $ScriptPath
}

# Replace Unacceptable values in password
function FormatKey($key) {
    $key = $key.replace("""", "&quot;")
    $key = $key.replace("&", "&amp;")
    $key = $key.replace("'", "&pos;")
    $key = $key.replace("<", "&lt;")
    $key = $key.replace(">", "&gt;")
    return $key
}

$PW = FormatKey($PW)
$SSIDHEX = ($SSID.ToCharArray() |foreach-object {'{0:X}' -f ([int]$_)}) -join''
$xmlfile = "<?xml version=""1.0""?>
<WLANProfile xmlns=""http://www.microsoft.com/networking/WLAN/profile/v1"">
    <name>$SSID</name>
    <SSIDConfig>
        <SSID>
            <hex>$SSIDHEX</hex>
            <name>$SSID</name>
        </SSID>
    </SSIDConfig>
    <connectionType>ESS</connectionType>
    <connectionMode>auto</connectionMode>
    <MSM>
        <security>
            <authEncryption>
                <authentication>WPA2PSK</authentication>
                <encryption>AES</encryption>
                <useOneX>false</useOneX>
            </authEncryption>
            <sharedKey>
                <keyType>passPhrase</keyType>
                <protected>false</protected>
                <keyMaterial>$PW</keyMaterial>
            </sharedKey>
        </security>
    </MSM>
</WLANProfile>
"

#Create XML File
$XMLFILE > "$(Get-ScriptPath)\$profilefile"

#Create, display, then connect to the new network
netsh wlan add profile filename="$(Get-ScriptPath)\$profilefile" #user=all
netsh wlan show profiles $SSID key=clear
netsh wlan connect name=$SSID

# Delete the XML file we created
Remove-Item "$(Get-ScriptPath)\$profilefile" -Force

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接