2026年1月29日木曜日

Windows11 のプロキシ設定のセットアップスクリプトを Powershell で設定する

Windows11 のプロキシ設定のセットアップスクリプトを Powershell で設定する

概要

タイトルの通りです

環境

  • Windows11

Powershell スクリプト

# Windows proxy setup script configuration tool
# Usage: .\change_proxy.ps1 -ScriptURL "http://xxx.xxx.xxx.xxx/proxy.pac"
#        .\change_proxy.ps1 -Reset

param(
    [string]$ScriptURL = "http://xxx.xxx.xxx.xxx/proxy.pac",
    [switch]$Reset = $false
)

# Check admin privileges
function Test-IsAdmin {
    $currentUser = [Security.Principal.WindowsIdentity]::GetCurrent()
    $principal = New-Object Security.Principal.WindowsPrincipal($currentUser)
    return $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
}

# Elevate to admin if needed
if (-not (Test-IsAdmin)) {
    Write-Host "Admin privileges required. Elevating..." -ForegroundColor Yellow
    $scriptPath = $MyInvocation.MyCommand.Path
    $arguments = "-NoProfile -ExecutionPolicy Bypass -File `"$scriptPath`""
    if ($ScriptURL -ne "http://xxx.xxx.xxx.xxx/proxy.pac") {
        $arguments += " -ScriptURL `"$ScriptURL`""
    }
    if ($Reset) {
        $arguments += " -Reset"
    }
    Start-Process PowerShell -ArgumentList $arguments -Verb RunAs -Wait
    exit 0
}

if ($Reset) {
    # Reset proxy settings
    Write-Host "Resetting proxy settings..."
    netsh winhttp reset proxy

    # Reset registry settings
    $regPath = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
    if (Test-Path $regPath) {
        Set-ItemProperty -Path $regPath -Name ProxyEnable -Value 0 -ErrorAction SilentlyContinue
        Set-ItemProperty -Path $regPath -Name AutoConfigURL -Value "" -ErrorAction SilentlyContinue
        Set-ItemProperty -Path $regPath -Name AutoDetect -Value 0 -ErrorAction SilentlyContinue
        Set-ItemProperty -Path $regPath -Name ProxyServer -Value "" -ErrorAction SilentlyContinue
        Write-Host "Proxy settings reset." -ForegroundColor Green
    }
} else {
    # Configure setup script
    Write-Host "Configuring setup script: $ScriptURL"

    $regPath = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
    if (-not (Test-Path $regPath)) {
        New-Item -Path $regPath -Force | Out-Null
    }

    # Disable direct proxy
    Set-ItemProperty -Path $regPath -Name ProxyEnable -Value 0 -ErrorAction SilentlyContinue
    # Set auto-config script
    Set-ItemProperty -Path $regPath -Name AutoConfigURL -Value $ScriptURL -ErrorAction SilentlyContinue
    # Disable auto-detect
    Set-ItemProperty -Path $regPath -Name AutoDetect -Value 0 -ErrorAction SilentlyContinue

    Write-Host "Setup script configured." -ForegroundColor Green
}

# Display current settings
Write-Host "`nCurrent proxy settings:" -ForegroundColor Cyan
$regPath = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
if (Test-Path $regPath) {
    $proxyEnable = (Get-ItemProperty -Path $regPath -Name ProxyEnable -ErrorAction SilentlyContinue).ProxyEnable
    $autoConfigURL = (Get-ItemProperty -Path $regPath -Name AutoConfigURL -ErrorAction SilentlyContinue).AutoConfigURL
    $autoDetect = (Get-ItemProperty -Path $regPath -Name AutoDetect -ErrorAction SilentlyContinue).AutoDetect
    $proxyServer = (Get-ItemProperty -Path $regPath -Name ProxyServer -ErrorAction SilentlyContinue).ProxyServer

    Write-Host "  ProxyEnable: $proxyEnable"
    Write-Host "  AutoConfigURL: $autoConfigURL"
    Write-Host "  AutoDetect: $autoDetect"
    Write-Host "  ProxyServer: $proxyServer"
}
Write-Host ""
netsh winhttp show proxy

最後に

当然ですが管理者権限が必要になります

0 件のコメント:

コメントを投稿