找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 23766|回复: 12

[网络连接] 求助把这2个自动连接对应WIFI的bat合并到一个

[复制链接]
发表于 2022-1-25 11:32:41 | 显示全部楼层 |阅读模式
这是用来封装系统进入桌面时自动连接对应WIFI的,我们这 有2个WIFI,想自动保存到WIFI连接列表,求高手合并,虽然分别运行也是可以的,但还是想用一个做到,谢谢
  1. @echo off
  2. powershell -c "Get-Content -LiteralPath '%~0' | Select-Object -Skip 3 | Out-String | Invoke-Expression"
  3. pause&goto b_bat
  4. $wifiName="WIFI1"; # WIFI NAME
  5. $wifiKey="88888888"; # WIFI PASSWORD
  6. $xml_Template=@"
  7. <?xml version="1.0"?>
  8. <WLANProfile xmlns="http://www.microsoft.com/networking/WLAN/profile/v1">
  9.         <name>WIFI_NAME</name>
  10.         <SSIDConfig>
  11.                 <SSID>
  12.                         <hex>WIFI_NAME_HEX</hex>
  13.                         <name>WIFI_NAME</name>
  14.                 </SSID>
  15.         </SSIDConfig>
  16.         <connectionType>ESS</connectionType>
  17.         <connectionMode>manual</connectionMode>
  18.         <MSM>
  19.                 <security>
  20.                         <authEncryption>
  21.                                 <authentication>WPA2PSK</authentication>
  22.                                 <encryption>AES</encryption>
  23.                                 <useOneX>false</useOneX>
  24.                         </authEncryption>
  25.                         <sharedKey>
  26.                                 <keyType>passPhrase</keyType>
  27.                                 <protected>false</protected>
  28.                                 <keyMaterial>WIFI_KEY</keyMaterial>
  29.                         </sharedKey>
  30.                 </security>
  31.         </MSM>
  32.         <MacRandomization xmlns="http://www.microsoft.com/networking/WLAN/profile/v3">
  33.                 <enableRandomization>false</enableRandomization>
  34.                 <randomizationSeed>634562794</randomizationSeed>
  35.         </MacRandomization>
  36. </WLANProfile>
  37. "@
  38. $wifiNameHex="";
  39. foreach ($each in [System.Text.Encoding]::UTF8.GetBytes($wifiName)) { $wifiNameHex+=("{0:x}" -f $each).ToUpper();}
  40. $xmlFile="WLAN-{0}.xml" -f $wifiName
  41. $xml=$xml_Template -replace "WIFI_NAME_HEX",$wifiNameHex -replace "WIFI_NAME",$wifiName -replace "WIFI_KEY",$wifiKey
  42. $xml | Out-File $xmlFile -Encoding utf8
  43. netsh wlan delete profile $wifiName 2>$null
  44. netsh wlan add profile $xmlFile
  45. netsh wlan connect $wifiName
  46. Remove-Item -LiteralPath $xmlFile
复制代码
  1. @echo off
  2. powershell -c "Get-Content -LiteralPath '%~0' | Select-Object -Skip 3 | Out-String | Invoke-Expression"
  3. pause&exit
  4. $wifiName="WIFI2"; # WIFI NAME
  5. $wifiKey="88888888"; # WIFI PASSWORD
  6. $xml_Template=@"
  7. <?xml version="1.0"?>
  8. <WLANProfile xmlns="http://www.microsoft.com/networking/WLAN/profile/v1">
  9.         <name>WIFI_NAME</name>
  10.         <SSIDConfig>
  11.                 <SSID>
  12.                         <hex>WIFI_NAME_HEX</hex>
  13.                         <name>WIFI_NAME</name>
  14.                 </SSID>
  15.         </SSIDConfig>
  16.         <connectionType>ESS</connectionType>
  17.         <connectionMode>manual</connectionMode>
  18.         <MSM>
  19.                 <security>
  20.                         <authEncryption>
  21.                                 <authentication>WPA2PSK</authentication>
  22.                                 <encryption>AES</encryption>
  23.                                 <useOneX>false</useOneX>
  24.                         </authEncryption>
  25.                         <sharedKey>
  26.                                 <keyType>passPhrase</keyType>
  27.                                 <protected>false</protected>
  28.                                 <keyMaterial>WIFI_KEY</keyMaterial>
  29.                         </sharedKey>
  30.                 </security>
  31.         </MSM>
  32.         <MacRandomization xmlns="http://www.microsoft.com/networking/WLAN/profile/v3">
  33.                 <enableRandomization>false</enableRandomization>
  34.                 <randomizationSeed>634562794</randomizationSeed>
  35.         </MacRandomization>
  36. </WLANProfile>
  37. "@
  38. $wifiNameHex="";
  39. foreach ($each in [System.Text.Encoding]::UTF8.GetBytes($wifiName)) { $wifiNameHex+=("{0:x}" -f $each).ToUpper();}
  40. $xmlFile="WLAN-{0}.xml" -f $wifiName
  41. $xml=$xml_Template -replace "WIFI_NAME_HEX",$wifiNameHex -replace "WIFI_NAME",$wifiName -replace "WIFI_KEY",$wifiKey
  42. $xml | Out-File $xmlFile -Encoding utf8
  43. netsh wlan delete profile $wifiName 2>$null
  44. netsh wlan add profile $xmlFile
  45. netsh wlan connect $wifiName
  46. Remove-Item -LiteralPath $xmlFile
复制代码
发表于 2022-1-25 13:30:25 | 显示全部楼层
本帖最后由 yakeyun 于 2022-1-26 17:08 编辑

回复 1# xiyimood


效果自测:
  1. @echo off
  2. powershell -c "Get-Content -LiteralPath '%~0' | Select-Object -Skip 3 | Out-String | Invoke-Expression"
  3. pause & goto b_bat
  4. $wifiName="WIFI1"; # WIFI NAME
  5. $wifiKey="88888888"; # WIFI PASSWORD
  6. $xml_Template=@"
  7. <?xml version="1.0"?>
  8. <WLANProfile xmlns="http://www.microsoft.com/networking/WLAN/profile/v1">
  9.         <name>WIFI_NAME</name>
  10.         <SSIDConfig>
  11.                 <SSID>
  12.                         <hex>WIFI_NAME_HEX</hex>
  13.                         <name>WIFI_NAME</name>
  14.                 </SSID>
  15.         </SSIDConfig>
  16.         <connectionType>ESS</connectionType>
  17.         <connectionMode>manual</connectionMode>
  18.         <MSM>
  19.                 <security>
  20.                         <authEncryption>
  21.                                 <authentication>WPA2PSK</authentication>
  22.                                 <encryption>AES</encryption>
  23.                                 <useOneX>false</useOneX>
  24.                         </authEncryption>
  25.                         <sharedKey>
  26.                                 <keyType>passPhrase</keyType>
  27.                                 <protected>false</protected>
  28.                                 <keyMaterial>WIFI_KEY</keyMaterial>
  29.                         </sharedKey>
  30.                 </security>
  31.         </MSM>
  32.         <MacRandomization xmlns="http://www.microsoft.com/networking/WLAN/profile/v3">
  33.                 <enableRandomization>false</enableRandomization>
  34.                 <randomizationSeed>634562794</randomizationSeed>
  35.         </MacRandomization>
  36. </WLANProfile>
  37. "@
  38. $wifiNameHex="";
  39. foreach ($each in [System.Text.Encoding]::UTF8.GetBytes($wifiName)) { $wifiNameHex+=("{0:x}" -f $each).ToUpper();}
  40. $xmlFile="WLAN-{0}.xml" -f $wifiName
  41. $xml=$xml_Template -replace "WIFI_NAME_HEX",$wifiNameHex -replace "WIFI_NAME",$wifiName -replace "WIFI_KEY",$wifiKey
  42. $xml | Out-File $xmlFile -Encoding utf8
  43. netsh wlan delete profile $wifiName 2>$null
  44. netsh wlan add profile $xmlFile
  45. netsh wlan connect $wifiName
  46. Remove-Item -LiteralPath $xmlFile

  47. ::Ping网络状态
  48. choice /t 10 /d y /n >nul
  49. ping -n 3 www.baidu.com >nul
  50. IF ERRORLEVEL 1 (goto WIFI2)
  51. IF ERRORLEVEL 0 (goto End)

  52. :WIFI2
  53. powershell -c "Get-Content -LiteralPath '%~0' | Select-Object -Skip 3 | Out-String | Invoke-Expression"
  54. pause & exit /b
  55. $wifiName="WIFI2"; # WIFI NAME
  56. $wifiKey="88888888"; # WIFI PASSWORD
  57. $xml_Template=@"
  58. <?xml version="1.0"?>
  59. <WLANProfile xmlns="http://www.microsoft.com/networking/WLAN/profile/v1">
  60.         <name>WIFI_NAME</name>
  61.         <SSIDConfig>
  62.                 <SSID>
  63.                         <hex>WIFI_NAME_HEX</hex>
  64.                         <name>WIFI_NAME</name>
  65.                 </SSID>
  66.         </SSIDConfig>
  67.         <connectionType>ESS</connectionType>
  68.         <connectionMode>manual</connectionMode>
  69.         <MSM>
  70.                 <security>
  71.                         <authEncryption>
  72.                                 <authentication>WPA2PSK</authentication>
  73.                                 <encryption>AES</encryption>
  74.                                 <useOneX>false</useOneX>
  75.                         </authEncryption>
  76.                         <sharedKey>
  77.                                 <keyType>passPhrase</keyType>
  78.                                 <protected>false</protected>
  79.                                 <keyMaterial>WIFI_KEY</keyMaterial>
  80.                         </sharedKey>
  81.                 </security>
  82.         </MSM>
  83.         <MacRandomization xmlns="http://www.microsoft.com/networking/WLAN/profile/v3">
  84.                 <enableRandomization>false</enableRandomization>
  85.                 <randomizationSeed>634562794</randomizationSeed>
  86.         </MacRandomization>
  87. </WLANProfile>
  88. "@
  89. $wifiNameHex="";
  90. foreach ($each in [System.Text.Encoding]::UTF8.GetBytes($wifiName)) { $wifiNameHex+=("{0:x}" -f $each).ToUpper();}
  91. $xmlFile="WLAN-{0}.xml" -f $wifiName
  92. $xml=$xml_Template -replace "WIFI_NAME_HEX",$wifiNameHex -replace "WIFI_NAME",$wifiName -replace "WIFI_KEY",$wifiKey
  93. $xml | Out-File $xmlFile -Encoding utf8
  94. netsh wlan delete profile $wifiName 2>$null
  95. netsh wlan add profile $xmlFile
  96. netsh wlan connect $wifiName
  97. Remove-Item -LiteralPath $xmlFile

  98. :End
  99. exit /b
复制代码
 楼主| 发表于 2022-1-25 14:40:58 | 显示全部楼层
回复 2# yakeyun


    谢谢大神,没有成功
  1. Invoke-Expression : 所在位置 行:48 字符: 3
  2. + IF ERRORLEVEL 1 goto WIFI2
  3. +   ~
  4. if 语句中的“IF”后面缺少“(”。
  5. 所在位置 行:49 字符: 3
  6. + IF ERRORLEVEL 0 exit
  7. +   ~
  8. if 语句中的“IF”后面缺少“(”。
  9. 所在位置 行:53 字符: 6
  10. + pause&exit
  11. +      ~
  12. 不允许使用与号(&)。& 运算符是为将来使用而保留的;请用双引号将与号引起来("&"),以将其作为字符串的一部分传递。
  13. 所在位置 行:1 字符: 96
  14. + ... 装部署文件\c.bat' | Select-Object -Skip 3 | Out-String | Invoke-Expression
  15. +                                                         ~~~~~~~~~~~~~~~~~
  16.     + CategoryInfo          : ParserError: (:) [Invoke-Expression], ParseException
  17.     + FullyQualifiedErrorId : MissingOpenParenthesisInIfStatement,Microsoft.PowerShell.Commands.InvokeExpressionComman
  18.    d

  19. 请按任意键继续. . .
复制代码
 楼主| 发表于 2022-1-25 14:41:53 | 显示全部楼层
回复  yakeyun


    谢谢大神,没有成功
xiyimood 发表于 2022-1-25 14:40



    c.bat是我保存你的代码文件
发表于 2022-1-25 15:14:44 | 显示全部楼层
本帖最后由 went 于 2022-1-26 20:39 编辑

47,49行自己改下
test.bat, ansi编码
  1. #&cls&@cd /d "%~dp0" & powershell -c "Get-Content '%~0' | Out-String | Invoke-Expression" &pause&exit
  2. $xml_Template=@'
  3. <?xml version="1.0"?>
  4. <WLANProfile xmlns="http://www.microsoft.com/networking/WLAN/profile/v1">
  5.         <name>WIFI_NAME</name>
  6.         <SSIDConfig>
  7.                 <SSID>
  8.                         <hex>WIFI_NAME_HEX</hex>
  9.                         <name>WIFI_NAME</name>
  10.                 </SSID>
  11.         </SSIDConfig>
  12.         <connectionType>ESS</connectionType>
  13.         <connectionMode>manual</connectionMode>
  14.         <MSM>
  15.                 <security>
  16.                         <authEncryption>
  17.                                 <authentication>WPA2PSK</authentication>
  18.                                 <encryption>AES</encryption>
  19.                                 <useOneX>false</useOneX>
  20.                         </authEncryption>
  21.                         <sharedKey>
  22.                                 <keyType>passPhrase</keyType>
  23.                                 <protected>false</protected>
  24.                                 <keyMaterial>WIFI_KEY</keyMaterial>
  25.                         </sharedKey>
  26.                 </security>
  27.         </MSM>
  28.         <MacRandomization xmlns="http://www.microsoft.com/networking/WLAN/profile/v3">
  29.                 <enableRandomization>false</enableRandomization>
  30.                 <randomizationSeed>634562794</randomizationSeed>
  31.         </MacRandomization>
  32. </WLANProfile>
  33. '@
  34. #函数 更新或添加一个wifi配置
  35. function Add-WifiConfig($wifiName,$wifiKey,[switch]$connect){
  36.         $wifiNameHex="";
  37.         foreach ($each in [System.Text.Encoding]::UTF8.GetBytes($wifiName)) { $wifiNameHex+=("{0:x}" -f $each).ToUpper();}
  38.         $xmlFile="WLAN-{0}.xml" -f $wifiName
  39.         $xml=$xml_Template -replace "WIFI_NAME_HEX",$wifiNameHex -replace "WIFI_NAME",$wifiName -replace "WIFI_KEY",$wifiKey
  40.         $xml | Out-File $xmlFile -Encoding utf8
  41.         netsh wlan delete profile $wifiName >$null 2>$null
  42.         netsh wlan add profile $xmlFile
  43.         if($connect){ netsh wlan connect $wifiName }
  44.         Remove-Item -LiteralPath $xmlFile
  45. }

  46. Add-WifiConfig -wifiName 'TP-LINK_0AE2' -wifiKey 'LLLL1234' -connect

  47. Add-WifiConfig -wifiName 'TP-LINK_5G_0AE2' -wifiKey 'LLLL1234'
复制代码
发表于 2022-1-25 16:21:14 | 显示全部楼层
本帖最后由 yakeyun 于 2022-1-25 16:24 编辑

回复 3# xiyimood


已经修改,再试下看看。批处理中只有包含了中文,才能另存为ansi编码。
当检测到第一个WIFI连接,延时10秒,网络畅通就退出,网络不通就尝试连接第二个WIFI。IP分配延时时间,可以根据实际自测速度调整,一般5秒以内就可以正常连接上。
 楼主| 发表于 2022-1-26 14:33:54 | 显示全部楼层
回复 6# yakeyun


   你好,您是在原回复上改的吗,我试了还不行
  1. Invoke-Expression : 所在位置 行:48 字符: 3
  2. + IF ERRORLEVEL 1 goto WIFI2
  3. +   ~
  4. if 语句中的“IF”后面缺少“(”。
  5. 所在位置 行:49 字符: 3
  6. + IF ERRORLEVEL 0 goto End
  7. +   ~
  8. if 语句中的“IF”后面缺少“(”。
  9. 所在位置 行:53 字符: 6
  10. + pause&exit
  11. +      ~
  12. 不允许使用与号(&)。& 运算符是为将来使用而保留的;请用双引号将与号引起来("&"),以将其作为字符串的一部分传递。
  13. 所在位置 行:1 字符: 108
  14. + ... sktop\1.bat' | Select-Object -Skip 3 | Out-String | Invoke-Expression
  15. +                                                         ~~~~~~~~~~~~~~~~~
  16.     + CategoryInfo          : ParserError: (:) [Invoke-Expression], ParseException
  17.     + FullyQualifiedErrorId : MissingOpenParenthesisInIfStatement,Microsoft.PowerShell.Commands.InvokeExpressionComman
  18.    d

  19. 请按任意键继续. . .
复制代码
 楼主| 发表于 2022-1-26 14:35:09 | 显示全部楼层
回复 5# went


    哥,你这确实是可以,但只是加入记忆,可以让它主动连接第一个wifi吗
  1. 已将配置文件 GDPR-office 添加到接口 WLAN。
  2. 已成功完成连接请求。
  3. 已将配置文件 CSGX2 添加到接口 WLAN。
  4. 已成功完成连接请求。
  5. 请按任意键继续. . .
复制代码
发表于 2022-1-26 17:09:18 | 显示全部楼层
回复 7# xiyimood


原代码基础上修改,可以再试下看看。
发表于 2022-1-26 20:43:36 | 显示全部楼层
回复 8# xiyimood
需要连接哪个wifi,就加上-connect选项,详见5楼
 楼主| 发表于 2022-1-27 17:50:43 | 显示全部楼层
回复 10# went


    加了 -connect不知为啥也没自动连接,自动连接没打勾
发表于 2022-1-27 20:39:29 | 显示全部楼层
回复 11# xiyimood


    我这里可以自动连接,你那边不清楚
 楼主| 发表于 2022-1-28 10:46:41 | 显示全部楼层
回复 12# went


    感谢,实在没自动连接手动点一下也行,已经很不错了
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-20 12:47 , Processed in 0.022078 second(s), 8 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表