Created
December 23, 2022 20:39
-
-
Save belgotux/1dd9619c444b13f8b68806592ae4182b to your computer and use it in GitHub Desktop.
lunch actions on connection change on windows Event with Task scheduler or via a loop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[void][Windows.Networking.Connectivity.NetworkInformation, Windows, ContentType = WindowsRuntime] | |
#Task Scheduler on a event : | |
# Log : Microsoft-Windows-NetworkProfile/Operationel | |
# Source : actions_connection_event.ps1 | |
# Event ID : 10000 | |
# Action : powershell.exe C:\Scripts\actions_connection_event.ps1 | |
# Condition : unselect !"Start the task only if the computer is on AC power" | |
$rootedConnexion="WIfiX","WifiY" | |
# can be a path | |
# or an array of 3 string : path / -argumentList / -WindowStyle | |
$exeToStart= @( | |
@("ssh","home-socks -p 2222","hidden"), | |
"C:\Program Files (x86)\Proxifier\Proxifier.exe" | |
) | |
$pidFile=$env:TEMP+'\tunnelSocksProcess.pid' | |
$connProfil = [Windows.Networking.Connectivity.NetworkInformation]::GetInternetConnectionProfile() | |
$actuelNetworkNames=$connProfil.GetNetworkNames() | |
# check if pid not already running | |
if (-not(Test-path $pidFile -PathType Leaf)) { | |
if ($actuelNetworkNames -in $rootedConnexion) { | |
foreach ($exe in $exeToStart) { | |
if ($exe -is [array]) { | |
Start-Process -FilePath $exe[0] -argumentList $exe[1] -WindowStyle $exe[2] -PassThru | select-object -ExpandProperty id | Out-File -Append -FilePath $pidFile | |
# -PassThru return a process object # -ExpandProperty get only the value (no header) | |
} | |
else { | |
Start-Process -FilePath $exe -PassThru | select-object -ExpandProperty id | Out-File -Append -FilePath $pidFile | |
} | |
} | |
} | |
} | |
# if pid exist, check if network change | |
else { | |
if (-not($actuelNetworkNames -in $rootedConnexion)) { | |
#write-host "Network change" | |
$currentPid=get-content -Path $pidFile | |
stop-process -id $currentPid | |
remove-item $pidFile | |
} | |
} | |
#write-host "Actual network : $actuelNetworkNames" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[void][Windows.Networking.Connectivity.NetworkInformation, Windows, ContentType = WindowsRuntime] | |
$rootedConnexion="WifiX","WifiY" | |
$loopSeconds=10 | |
# can be a path | |
# or an array of 3 string : path / -argumentList / -WindowStyle (hidden/minimized/maximized/normal) | |
$exeToStart= @( | |
@("ssh","home-socks -p 2222","hidden"), | |
"C:\Program Files (x86)\Proxifier\Proxifier.exe" | |
) | |
$pidFile=$env:TEMP+'\tunnelSocksProcess.pid' | |
$Enable = 1 | |
DO { | |
$connProfil = [Windows.Networking.Connectivity.NetworkInformation]::GetInternetConnectionProfile() | |
$actuelNetworkNames=$connProfil.GetNetworkNames() | |
# check if pid not already running | |
if (-not(Test-path $pidFile -PathType Leaf)) { | |
if ($actuelNetworkNames -in $rootedConnexion) { | |
foreach ($exe in $exeToStart) { | |
#write-host $exe | |
if ($exe -is [array]) { | |
Start-Process -FilePath $exe[0] -argumentList $exe[1] -WindowStyle $exe[2] -PassThru | select-object -ExpandProperty id | Out-File -Append -FilePath $pidFile | |
# -PassThru return a process object # -ExpandProperty get only the value (no header) | |
} | |
else { | |
Start-Process -FilePath $exe -PassThru | select-object -ExpandProperty id | Out-File -Append -FilePath $pidFile | |
} | |
} | |
} | |
} | |
# if pid exist, check if network change | |
else { | |
if (-not($actuelNetworkNames -in $rootedConnexion)) { | |
write-host "Network change" | |
$currentPid=get-content -Path $pidFile | |
stop-process -id $currentPid | |
remove-item $pidFile | |
} | |
} | |
start-sleep -seconds $loopSeconds | |
write-host "Actual network : $actuelNetworkNames" | |
} Until ($Enable -eq 0) | |
# https://learn.microsoft.com/en-us/uwp/api/windows.networking.connectivity.connectionprofile?view=winrt-22621 | |
# https://learn.microsoft.com/en-us/uwp/api/windows.networking.connectivity.networkinformation.getinternetconnectionprofile?view=winrt-22621 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment