Wi-Fi環境切り替えバッチの作成と活用メモ

Windows

企業内のネットワーク環境が多様化する中で、同じ場所にある複数のWi-Fiネットワークに対して効率的に接続させるためのバッチファイルを作成する必要がありました。以下は、そのプロセスを詳細に記したメモです。

スポンサーリンク

1.Wi-Fiプロファイルの準備

まず、対象となるWi-Fiに手動で接続し、その設定をエクスポートします。このプロファイル(XMLファイル)を作成することで、後でスクリプトから手軽にそのネットワークに接続できるようになります。以下のコマンドを管理者権限で実行します。

netsh wlan export profile name="internet-A" folder=c:\test\
name:SSIDを指定します。
folder:プロファイルの出力先フォルダを指定します。

例えば、「internet-A.xml」という名前のファイルがCドライブのtestフォルダに生成されます。生成されたプロファイルファイルは、スクリプトと同じディレクトリにある「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接続のための基本コマンド

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.バッチメニューの作成

上記を基本コマンドをもとにメニュー化します。このメニューから接続したいWi-Fiを選択させることになります。

@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 の動作が完了しました。

このメニューで、ユーザーが数字を入力するだけで指定したWi-Fiに接続できるようになります。

さらに、接続を切り替える際に前回接続していたSSIDへの接続が残ることを防ぐため、以下のコマンドを追加して現在のWi-Fi接続を切断することをおすすめします。

rem 現在の接続を切断 
netsh wlan disconnect

4.コードをもう少しきれいに

使い捨てコードであっても、見栄え良く整理することは重要です。以下に改善されたコードを示します。特に「call」コマンドや「exit /b」を利用してスクリプトを分かりやすくしています。「>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.*;" > 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.*;" > 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

このスクリプトにより、Wi-Fi接続の切り替え作業が簡単に行えるようになります。各Wi-Fiネットワークへの迅速かつ確実な接続を実現するために、工夫されたこのバッチファイルを活用してみてください。

楽天Kobo電子書籍ストア
¥3,149 (2024/06/29 12:24時点 | 楽天市場調べ)
タイトルとURLをコピーしました