大量のマシンにWi-Fi 環境を設定する必要が生じたので、それ用のバッチの作成したときのメモ。
社内に複数の種類のWi-Fiがあり、それぞれ用途が違うようなシチュエーションを想定したもの。
まずはメニューの枠組みを以下のような感じで作成。
選択すると接続するWi-Fiアクセスポイントを切り替えたうえで、プロキシ設定など必要な変更も全てしてくれるようなものをイメージ。
(初めての接続であっても、ステルスSSIDであっても接続を新規作成して接続してくれるもの)
@echo off :startmenu echo ----------------メニュー---------------- echo [1] Wi-Fi"internet-A"に接続 echo [2] Wi-Fi"internet-B"に接続 echo [3] Wi-Fi"syanai-A"に接続 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-syanai-A if "%NUM%"=="4" goto endMsg goto NoNumber :connect-internet-A rem ここに接続処理を書く(※後述参照) pause goto end :connect-internet-B rem ここに接続処理を書く(※後述参照) pause goto end :connect-syanai-A rem ここに接続処理を書く(※後述参照) pause goto end :NoNumber echo その番号はメニューにありません。 echo ... pause goto startmenu :end :endMsg echo %0 の動作が完了しました。
次に各接続処理の中身を書いていく.....その前に、自動接続(新規)を可能とするためにWi-Fiプロファイル(xmlファイル)を、該当Wi-Fiに一度つなげた状態でエクスポートしておく。
netsh wlan export profile name="Wi-Fi-internet-A" folder=c:\test\
nameは適当でよいので、バッチから呼び出す際にわかりやすいものをつけておく。この例だと「Wi-Fi-internet-A.xml」がcドライブ内のtestフォルダへ生成される。
これでxmlファイルを生成したら、バッチファイルと同じ場所に例えば「profile」というフォルダを作成して入れておく。(※以下のバッチは、バッチファイル本体と同レベル「profile」内にxmlを配置した場合のもの)
接続処理の中身。
:connect-internet-A set wifi_prof="%~dp0profile\Wi-Fi-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 echo - Wi-Fi設定完了:%ssid% netsh wlan connect name=%ssid% echo - Wi-Fi接続完了:%ssid% echo. echo - 接続を5秒間待ちます powershell sleep 5 pause goto end
netsh wlan set profileparameter の補足
name:SSID名
keymaterial:パスワード
nonBroadcast=yes:「ネットワークがブロードキャストを行っていない場合でも接続する」にチェック
connectionmode=auto:「自動的に接続する」にチェック
上記の connectionmode=auto によって「自動的に接続する」にチェックをつけただけでは接続されない場合があったので、
netsh wlan connect name=%ssid% を入れておく。
connect-internet-B 、connect-syanai-A についても、同様にWi-Fiプロファイルを用意し、profileフォルダへ放り込んだうえで似たような処理を書く。
あとは接続処理後に必要に応じて、proxy の設定を下記のような感じで入れておく。
rem プロキシサーバー設定 reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyServer /t REG_SZ /d xxx.xxx.xxx.xxx /f rem プロキシ有効化 reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyEnable /t REG_DWORD /d 1 /f rem プロキシ除外を設定 reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /f /v ProxyOverride /t reg_sz /d "xxx.xxx.xxx.*" rem InternetExplorerの初期ページを設定 reg add "HKCU\Software\Microsoft\Internet Explorer\Main" /v "Start Page" /d "https://www.google.com" /f rem winhttp proxyのimport前に一度InternetExploreを起動 start https://www.google.com pause rem winhttpの設定をInternetExploreからインポート netsh winhttp import proxy source=ie echo 設定完了 pause goto end
これでだいたいの環境切り替えには対応できそう。