Wi-Fi環境切り替えバッチ
社内にそれぞれ用途が違う複数の種類のWi-Fiがあり、大量のマシンにそれらを設定していく必要が生じたので、それ用のバッチの作成したときのメモ。
1.Wi-Fiプロファイルの準備
まず、該当Wi-Fiに一度手動で繋げた状態で、Wi-Fiプロファイル(xmlファイル)をエクスポートしておく。コマンドは下記。cmdを管理者として実行しておく必要がある。
netsh wlan export profile name="internet-A" folder=c:\test\
nameはSSIDを指定。folderは出力先。この例だと「internet-A.xml」がcドライブ内のtestフォルダへ生成される。
これでxmlファイルを生成したら、バッチファイルと同じ場所に例えば「profile」というフォルダを作成して入れておく。
今回は下記のような構成を想定している。
フォルダ
|-切替.bat
|-profile
|-Wi-Fi-internet-A.xml
|-Wi-Fi-internet-B.xml
|-Wi-Fi-internet-C.xml
|-Wi-Fi-internet-D.xml
|-Wi-Fi-internet-E.xml
2.Wi-Fi接続のための基本コマンド
コマンドは下記。
set wifi_prof="%~dp0profile\internet-A.xml set ssid=internet-A set pw=xxxxxxxxx netsh wlan add profile filename=%wifi_prof% netsh wlan set profileparameter name=%ssid% nonBroadcast=yes keymaterial=%pw% connectionmode=auto netsh wlan connect name=%ssid%
netsh wlan set profileparameter に関する補足
name:SSID名
keymaterial:パスワード
nonBroadcast=yes:「ネットワークがブロードキャストを行っていない場合でも接続する」にチェック
connectionmode=auto:「自動的に接続する」にチェック
上記の connectionmode=auto によって「自動的に接続する」にチェックをつけただけでは接続されない場合があったので、
netsh wlan connect name=%ssid% を入れておく。
3.メニュー化
上記を基本コマンドをもとにメニュー化する。
@echo off :startmenu echo ----------------メニュー---------------- echo [1] Wi-Fi"internet-A"に接続 echo [2] Wi-Fi"internet-B"に接続 echo [3] Wi-Fi"internet-C"に接続 echo [4] 処理を終了 echo ----------------------------------------- set /p NUM="処理番号を入力してください >" if "%NUM%"=="1" goto connect-internet-A if "%NUM%"=="2" goto connect-internet-B if "%NUM%"=="3" goto connect-internet-C if "%NUM%"=="4" goto endMsg goto NoNumber :connect-internet-A rem ここに接続処理を書く pause goto end :connect-internet-B rem ここに接続処理を書く pause goto end :connect-internet-C rem ここに接続処理を書く pause goto end :NoNumber echo その番号はメニューにありません。 echo ... pause goto startmenu :end :endMsg echo %0 の動作が完了しました。
これでだいたいの環境切り替えには対応できそう。
切り替え時に前回接続したSSIDへの接続が残る(切断されない)という挙動があったので、冒頭に下記切断コマンドを入れることで解消。
rem 現在の接続を切断 netsh wlan disconnect
4.コードをもう少しきれいに
使い捨てコードとはいえ、「call」や「exit /b」とかを使って雰囲気だけでも見栄えよくしたほうがよさそう。
ということで最低限コードを見直してみたものが下記。
多様している「>nul」はnulへリダイレクトすることで結果を表示させないテクニック。
@echo off :startmenu echo ----------------メニュー---------------- echo [1] Wi-Fi"internet-A"に接続 echo [2] Wi-Fi"internet-B"に接続 echo [3] Wi-Fi"internet-C"に接続 echo [4] 処理を終了 echo ----------------------------------------- set /p num="処理番号を入力してください >" if "%num%"=="1" call :connect-internet-A if "%num%"=="2" call :connect-internet-B if "%num%"=="3" call :connect-internet-C if "%num%"=="4" call :endMsg echo - 完了 pause exit /b :connect-internet-A call :disconnect-connection call :delete-connection call :get-connectiondata call :get-connection call :set-proxy1 exit /b :connect-internet-B call :disconnect-connection call :delete-connection call :get-connectiondata call :get-connection call :set-proxy2 exit /b :connect-internet-C call :disconnect-connection call :delete-connection call :get-connectiondata call :get-connection call :set-proxy3 exit /b :disconnect-connection echo - 現在の接続を切断しています... netsh wlan disconnect > nul exit /b :delete-connection echo - 誤接続防止のため全てのプロファイルを削除しています... netsh wlan delete profile name=internet-A > nul netsh wlan delete profile name=internet-B > nul netsh wlan delete profile name=internet-C > nul echo - 3秒待ちます... powershell sleep 3 exit /b :set-proxy1 echo - プロキシをタイプ1に設定しています... reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyServer /t REG_SZ /d xxx.xxx.xxx.xxx /f > nul reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyEnable /t REG_DWORD /d 1 /f > nul reg add "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings" /f /v ProxyOverride /t reg_sz /d "xxx.xxx.xxx.*;<local>" > nul reg add "HKCU\Software\Microsoft\Internet Explorer\Main" /v "Start Page" /d "http://www.sample.com" /f > nul echo - winhttp proxyのimport前に一度InternetExploreを起動します... start http://www.sample.com echo. pause echo. echo - winhttpの設定をInternetExploreからインポートしています.... netsh winhttp import proxy source=ie > nul exit /b :set-proxy2 echo - プロキシをタイプ2に設定しています... reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyServer /t REG_SZ /d yyy.yyy.yyy.yyy /f > nul reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyEnable /t REG_DWORD /d 1 /f > nul reg add "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings" /f /v ProxyOverride /t reg_sz /d "yyy.yyy.yyy.*;<local>" > nul reg add "HKCU\Software\Microsoft\Internet Explorer\Main" /v "Start Page" /d "https://www.google.com" /f > nul echo - winhttp proxyのimport前に一度InternetExploreを起動します... start https://www.google.com echo. pause echo. echo - winhttpの設定をInternetExploreからインポートしています.... netsh winhttp import proxy source=ie > nul exit /b :set-proxy3 echo - プロキシをタイプ3に設定しています... reg add "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings" /f /v ProxyEnable /t reg_dword /d 0 > nul reg add "HKCU\Software\Microsoft\Internet Explorer\Main" /v "Start Page" /d "https://www.google.com" /f > nul echo - winhttp proxyのimport前に一度InternetExploreを起動します... start https://www.google.com echo. pause echo. echo - winhttpの設定をInternetExploreからインポートしています.... netsh winhttp reset proxy > nul exit /b :get-connectiondata echo - 接続しています... if "%num%"=="1" ( set wifi_prof="%~dp0profile\Wi-Fi-internetA.xml set ssid=xxxxxxx set pw=xxxxxxxx goto label ) if "%num%"=="2" ( set wifi_prof="%~dp0profile\Wi-Fi-internetB.xml set ssid=xxxxxxx set pw=xxxxxxxx goto label ) if "%num%"=="3" ( set wifi_prof="%~dp0profile\Wi-Fi-internetC.xml set ssid=xxxxxxx set pw=xxxxxxxx goto label ) :get-connection netsh wlan add profile filename=%wifi_prof% netsh wlan set profileparameter name=%ssid% nonBroadcast=yes keymaterial=%pw% connectionmode=auto echo - Wi-Fi設定完了:%ssid% netsh wlan connect name=%ssid% > nul echo - Wi-Fi接続完了:%ssid% echo. echo - 接続を5秒間待ちます powershell sleep 5 exit /b :endMsg echo %0 の動作が完了しました。 pause