是否可以通过编程的方式将WiFi配置文件添加到Windows操作系统(最低版本为Windows 7)中?
我尝试使用netsh的add profile和connect,但对我无效。是否有任何PowerShell命令可以完成这个任务?
我想要自动地为多个客户端设置一个特定SSID的WiFi密码。
希望有人能够提供一个命令,包含以下示例信息:
- SSID:WifiNetwork
- 密码:Password123
谢谢!
是否可以通过编程的方式将WiFi配置文件添加到Windows操作系统(最低版本为Windows 7)中?
我尝试使用netsh的add profile和connect,但对我无效。是否有任何PowerShell命令可以完成这个任务?
我想要自动地为多个客户端设置一个特定SSID的WiFi密码。
希望有人能够提供一个命令,包含以下示例信息:
谢谢!
我找到了一种添加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
我希望这些信息能够帮助到某人。
我写了一个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
我使用了这里的代码,但是当我尝试添加配置文件时出现了错误,因为我们的密码包含了一个“无效字符”。(我得到的错误是“尝试加载具有不正确格式的程序”并在谷歌搜索后找到了原因。)所以我添加了一些逻辑来替换那些不可接受的字符并使它们变得可接受。
它还强制将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("""", """)
$key = $key.replace("&", "&")
$key = $key.replace("'", "&pos;")
$key = $key.replace("<", "<")
$key = $key.replace(">", ">")
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
("WifiNetwork".ToCharArray() |foreach-object {'{0:X}' -f ([int]$_)}) -join ''
- Per von Zweigbergk