суббота, 22 июля 2023 г.

GUI to set Proxy in Windows

Adjust the Proxy URL as needed.

### START ### 

cls

# Set Proxy

# Define properties

$fontName = "Arial"

$fontSize = 10

$formWidth = 450

$formHeight = 350

$labelWidth = 570

$labelHeight = 20  

$formText = 'Set Proxy AutoConfigURL'

$currentLabelLocation = New-Object System.Drawing.Point(10,20)

$textBoxLocation = New-Object System.Drawing.Point(10,45)  

$futureLabelLocation = New-Object System.Drawing.Point(10,70)

$setButtonLocation = New-Object System.Drawing.Point(40,100)  

$cancelButtonLocation = New-Object System.Drawing.Point(130,100)

$removeButtonLocation = New-Object System.Drawing.Point(220,100)  

$buttonSize = New-Object System.Drawing.Size(75,23)

$autoConfigURL = 'http://pac.domain:8080/pac.pac'

$currentAutoConfigURL = 'Not set' 

$registryPath = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings'


# Load necessary assemblies

Add-Type -AssemblyName System.Windows.Forms


# Create the form

$form = New-Object System.Windows.Forms.Form

$form.Text = $formText

$form.Size = New-Object System.Drawing.Size($formWidth,$formHeight)

$form.StartPosition = 'CenterScreen'


# Define the font

$font = New-Object System.Drawing.Font($fontName, $fontSize)


# Get the current AutoConfigURL value, if it exists

try {

    $currentAutoConfigURL = (Get-ItemProperty -Path $registryPath -Name AutoConfigURL -ErrorAction Stop).AutoConfigURL

} catch {

    # The AutoConfigURL property does not exist

}


# Create label for current AutoConfigURL

$currentLabel = New-Object System.Windows.Forms.Label

$currentLabel.Location = $currentLabelLocation

$currentLabel.Size = New-Object System.Drawing.Size($labelWidth,$labelHeight)

$currentLabel.Text = 'Current AutoConfigURL: ' + $currentAutoConfigURL

$currentLabel.Font = $font

$form.Controls.Add($currentLabel)


# Create TextBox for input

$inputTextBox = New-Object System.Windows.Forms.TextBox

$inputTextBox.Location = $textBoxLocation

$inputTextBox.Size = New-Object System.Drawing.Size($labelWidth,$labelHeight)

$inputTextBox.Text = $autoConfigURL

$form.Controls.Add($inputTextBox)


# Create Set button

$setButton = New-Object System.Windows.Forms.Button

$setButton.Location = $setButtonLocation

$setButton.Size = $buttonSize

$setButton.Text = 'Set'

$setButton.DialogResult = [System.Windows.Forms.DialogResult]::OK

$form.AcceptButton = $setButton

$form.Controls.Add($setButton)


# Create cancel button

$cancelButton = New-Object System.Windows.Forms.Button

$cancelButton.Location = $cancelButtonLocation

$cancelButton.Size = $buttonSize

$cancelButton.Text = 'Cancel'

$cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel

$form.CancelButton = $cancelButton

$form.Controls.Add($cancelButton)


# Create remove button

$removeButton = New-Object System.Windows.Forms.Button

$removeButton.Location = $removeButtonLocation

$removeButton.Size = $buttonSize

$removeButton.Text = 'Remove'

$removeButton.Add_Click({

    # Set the AutoConfigURL to an empty string

    Set-ItemProperty -Path $registryPath -Name AutoConfigURL -Value ''

    # Disable proxy use in Internet Explorer settings

    Set-ItemProperty -Path $registryPath -Name ProxyEnable -Value 0

    $form.Close()

})

$form.Controls.Add($removeButton)


# Create a group box

$groupBox = New-Object System.Windows.Forms.GroupBox

$groupBox.Location = New-Object System.Drawing.Point(10,150)

$groupBox.Size = New-Object System.Drawing.Size(410,105)  # Adjust the size as necessary

$groupBox.Text = 'Instructions'

$groupBox.Font = $font

$form.Controls.Add($groupBox)


# Create instructions

$instructions = @(

    '1. Enter the new AutoConfigURL in the text box.',

    'Click Set to set the new AutoConfigURL and enable the proxy.',

    '2. Click Remove to clear the AutoConfigURL and disable proxy.',

    '3. Clear the textbox and click Set to disable the proxy.'

)


$labelLocation = 20

foreach ($instruction in $instructions) {

    $instructionLabel = New-Object System.Windows.Forms.Label

    $instructionLabel.Location = New-Object System.Drawing.Point(10,$labelLocation)

    $instructionLabel.Size = New-Object System.Drawing.Size(410,20)  # Adjust the size as necessary

    $instructionLabel.Text = $instruction

    $instructionLabel.Font = $font

    $groupBox.Controls.Add($instructionLabel)

    $labelLocation += 20  # Adjust the spacing as necessary

}


# Show the form

$form.Topmost = $true

$result = $form.ShowDialog()


# If user clicks Set, set the AutoConfigURL

if ($result -eq [System.Windows.Forms.DialogResult]::OK) {

    Set-ItemProperty -Path $registryPath -Name AutoConfigURL -Value $inputTextBox.Text

    Set-ItemProperty -Path $registryPath -Name ProxyEnable -Value 1

}

### END ### 


Комментариев нет:

Отправить комментарий