Skip to content

Instantly share code, notes, and snippets.

@dennisroche
Last active January 30, 2020 15:46
Show Gist options
  • Save dennisroche/4f498585dc460cdbe9e8cd2328d391ac to your computer and use it in GitHub Desktop.
Save dennisroche/4f498585dc460cdbe9e8cd2328d391ac to your computer and use it in GitHub Desktop.
Powershell script that sets your Internet Proxy settings and npm (if found) and auto-restarts Cntlm Authenication Proxy
#Requires -RunAsAdministrator
[CmdletBinding()]
param()
$ErrorActionPreference = "Stop"
trap
{
Pop-Location
Write-Error "$_"
Exit 1
}
function Set-InternetProxy($proxyServer) {
Write-Host "Setting proxy to be '$proxyServer' in Internet Settings" -ForegroundColor Cyan
$internetSettings = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
Set-ItemProperty -Path $internetSettings -Name ProxyServer -Value "$proxyServer"
Set-ItemProperty -Path $internetSettings -Name ProxyEnable -Value 1
# Set npm proxy
if (Get-Command "npm.cmd" -ErrorAction SilentlyContinue)
{
Write-Host "Setting proxy to be '$proxyServer' for npm" -ForegroundColor Cyan
npm config set proxy "http://$proxyServer"
npm config set https-proxy "http://$proxyServer"
}
}
function Clear-InternetProxy() {
Write-Host "Clearing proxy server in Internet Settings"-ForegroundColor Yellow
$internetSettings = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
Set-ItemProperty -Path $internetSettings -Name ProxyServer -Value ""
Set-ItemProperty -Path $internetSettings -Name ProxyEnable -Value 0
# Clear npm proxy
if (Get-Command "npm.cmd" -ErrorAction SilentlyContinue)
{
Write-Host "Clearing proxy server for npm" -ForegroundColor Yellow
npm config delete proxy
npm config delete https-proxy
}
}
$eventId = "WatchCntlmWmi"
$watchCntlm = @{
Query = "SELECT * FROM __InstanceModificationEvent WITHIN 1 WHERE targetInstance ISA 'win32_Service' AND targetInstance.Name='cntlm'"
Action = {
Write-Host ("Service: '{0}' changed from '{2}' to '{3}'" -f $event.SourceEventArgs.NewEvent.TargetInstance.DisplayName,
$event.SourceEventArgs.NewEvent.TargetInstance.Name,
$event.SourceEventArgs.NewEvent.PreviousInstance.State,
$event.SourceEventArgs.NewEvent.TargetInstance.State) -ForegroundColor Magenta
if ($event.SourceEventArgs.NewEvent.TargetInstance.State -eq 'Stopped Pending') {
try {
$process = Get-Process cntlm -ErrorAction SilentlyContinue
if ($process) {
Write-Host "Killing backing process 'Cntlm.exe'" -ForegroundColor Cyan
$process.Kill()
}
} catch {
Write-Host "Failed to kill backing process 'Cntlm.exe' $_" -ForegroundColor Yellow
}
}
if ($event.SourceEventArgs.NewEvent.TargetInstance.State -eq 'Stopped') {
try {
Write-Host "Restarting 'Cntlm Authenication Proxy'" -ForegroundColor Cyan
Start-Sleep 3
$service = Get-Service -Name cntlm
$service.Start()
} catch {
Write-Host "Failed to restart 'Cntlm Authenication Proxy' $_" -ForegroundColor Yellow
}
}
}
SourceIdentifier = $eventId
}
$service = Get-Service -Name cntlm
try {
Set-InternetProxy "localhost:3128"
Register-WMIEvent @watchCntlm | Out-Null
Write-Host "Monitoring 'Cntlm Authenication Proxy'" -ForegroundColor Cyan
Write-Host "Start 'Cntlm Authenication Proxy' service" -ForegroundColor Green
$service.Start()
Wait-Event -SourceIdentifier $eventId
} catch {
Write-Error $_
} finally {
Clear-InternetProxy
Write-Host "Stop monitoring 'Cntlm Authenication Proxy'" -ForegroundColor Red
Unregister-Event -SourceIdentifier $eventId -Force -ErrorAction "Continue"
Start-Sleep 3
Write-Host "Stop 'Cntlm Authenication Proxy' service" -ForegroundColor Red
$service.Stop()
Write-Host "Bye :)" -ForegroundColor Blue
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment