LoginSignup
4
2

More than 3 years have passed since last update.

コマンドでIE プロキシ状態 on/off 変更~デスクトップでイコン作成~One-Click変更~色見で状態知る

Last updated at Posted at 2019-09-19

この間「プロキシ必要」と「プロキシなし」のWIFI環境をよく切り替えるから、
もしコマンドでIEプロキシの状態が変更できたら、
バッチファイルを作ってOne-Clickで切り替えられると思います。

いろんな情報を整理して結局できました、
これは私情報整理して答えた英語の回答です。
https://stackoverflow.com/a/58004248/6070417

日本語の手順:
1、フォルダ作成"C:\Users<YOUR_USERNAME>\Proxy Settings"。
2、下記のコードをコピーして"toggle_proxy_on_off.vbs"に保存する。

'Toggle your Proxy on and off 
Option Explicit 

'Variables & Constants:
Dim ProxySettings_path, VbsScript_filename 

VbsScript_filename = "toggle_proxy_on_off.vbs"
Const MESSAGE_BOX_TIMEOUT = 1 'sec; change this value to set how long the message box displays when you toggle the proxy setting 
Const PROXY_OFF = 0

Dim WSHShell, proxyEnableVal, username 
Set WSHShell = WScript.CreateObject("WScript.Shell")
'get the username string for use in path names, since trying to use the "%USERNAME%" variable directly in path names throws an error
username = WSHShell.ExpandEnvironmentStrings("%USERNAME%") 
ProxySettings_path = "C:\Users\" + username + "\Proxy Settings"

'Determine current proxy setting and toggle to opposite setting
proxyEnableVal = wshshell.regread("HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable")
If proxyEnableVal = PROXY_OFF Then 
  TurnProxyOn
Else
  TurnProxyOff
End If

WSHShell.run("powershell -windowstyle hidden -file """ + ProxySettings_path + "\Refresh-System.ps1""")

'Subroutine to Toggle Proxy Setting to ON
Sub TurnProxyOn 
  'turn proxy on via a registry entry 
  WSHShell.regwrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable", 1, "REG_DWORD"
  'create/update desktop shortcut 
  CreateOrUpdateDesktopShortcut("on")
  'notify user via an auto-timed popup box 
  WSHShell.Popup "Internet proxy is now ON", MESSAGE_BOX_TIMEOUT, "Proxy Settings"
End Sub

'Subroutine to Toggle Proxy Setting to OFF
Sub TurnProxyOff 
  'turn proxy off via a registry entry 
  WSHShell.regwrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable", 0, "REG_DWORD"
  'create/update desktop shortcut 
  CreateOrUpdateDesktopShortcut("off")
  'notify user via an auto-timed popup box 
  WSHShell.Popup "Internet proxy is now OFF", MESSAGE_BOX_TIMEOUT, "Proxy Settings"
End Sub

'Subroutine to create or update a shortcut on the desktop 
Sub CreateOrUpdateDesktopShortcut(onOrOff)
  'create a shortcut 
  Dim shortcut, iconStr
  Set shortcut = WSHShell.CreateShortcut("C:\Users\" + username + "\Desktop\Proxy On-Off.lnk")
  'Set the target path (target file) to run when the shortcut is clicked 
  shortcut.TargetPath = ProxySettings_path + "\" + VbsScript_filename
  'Set the working directory. This is necessary in case you ever make this shortcut call a batch (.bat) file, for instance, which in turn calls a .vbs script. In order to know where the .vbs script file/command is located, the shortcut must be operating in the working directory where the .vbs scripts are located. Otherwise, calls to the .vbs scripts from a .bat file this shortcut points to, for instance, won't work since their directories are not in the Windows %PATH% variable, and you'll get an error which states: "'name_of_vbs_script_file' is not recognized as an internal or external command, operable program or batch file."
  shortcut.WorkingDirectory = ProxySettings_path 
  'Set the icon to associate with this shortcut 
  If onOrOff = "on" Then
    iconStr = "on.ico"
  ElseIf onOrOff = "off" Then
    iconStr = "off.ico"
  End If 
  shortcut.IconLocation = ProxySettings_path + "\Icons\" + iconStr
  'Save the shortcut 
  shortcut.Save
End Sub

3、下記のコードをコピーして"Refresh-System.ps1"に保存する。

function Refresh-System
{
  $signature = @'
[DllImport("wininet.dll", SetLastError = true, CharSet=CharSet.Auto)]
public static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int dwBufferLength);
'@

$INTERNET_OPTION_SETTINGS_CHANGED   = 39
$INTERNET_OPTION_REFRESH            = 37
$type = Add-Type -MemberDefinition $signature -Name wininet -Namespace pinvoke -PassThru
$a = $type::InternetSetOption(0, $INTERNET_OPTION_SETTINGS_CHANGED, 0, 0)
$b = $type::InternetSetOption(0, $INTERNET_OPTION_REFRESH, 0, 0)
return $a -and $b
}
Refresh-System

4、"toggle_proxy_on_off.vbs"と"Refresh-System.ps1"を"C:\Users<YOUR_USERNAME>\Proxy Settings" に移動する。
5、フォルダ作成"C:\Users<YOUR_USERNAME>\Proxy Settings\Icons"。
6、下の画像をダウンロードする。
on.png
off.png
7、このサイト「convertico.com」でダウンロードしたファイルをicoに変換して"C:\Users<YOUR_USERNAME>\Proxy Settings\Icons"へ移動する。
8、"toggle_proxy_on_off.vbs"をダブルクリックする。

OK、デスクトップでアイコンが作成しました、色を見るとすぐIEプロキシの状態が知るでしょう!

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2