#cs ---------------------------------------------------------------------------- SyncOnSyncOff Version: 1.20 Author: Scriptic Credit: _GetActiveSSID function by guinness Script Function: For Windows laptop. Avoid excessive data usage on Mobile hotspots by shutting off background cloud syncing programs. Check WiFi SSID and determine if it is in the list of Mobile Hotspots. If so, disable cloud sync programs like Google Drive, Box Sync, and OneDrive. Otherwise enable cloud sync programs. Usage: Modify "Configuration" section below to match your syncing programs and mobile hotspots Script can be run manually with a desktop shortcut. A better use is to trigger a Windows Scheduled Task to run the script on network connection. 1. Open Task Scheduler by using WindowsKey->type "Sch"->select Task Scheduler. 2. In left pane right-click "Task Scheduler Library" -> "Create Basic Task" 3. Name it "SyncOnSyncOff" or something snazzier. Click "Next" 4. Trigger->"When a specific event is logged"->Next->Log->"Microsoft-Windows-NetworkProfile/Operational" 5. Source-NetworkProfile 6. "Event ID"->10000->Next->Action->"Start a program"->Next 7. Browse to this script and select it. Click the box to "Open the Properties dialog for this task when I click Finish. 8. Click "Finish." Click "Conditions." Uncheck "Start the task only if the PC is on AC power." Caveats: Script does not do much error checking, so user beware. Log file grows with each execution so after 100 years or so it might get too big. You can delete it to start a new file. Script stopped running and I found the task scheduler defaults to not run tasks when on battery. Had to go back in and uncheck that under "Conditions." Changelog: 1.10 2016-10-17 Origin 1.20 2016-10-19 Added comments, etc for posting #ce ---------------------------------------------------------------------------- ;------------------- Configuration ------------------------- ; array of hotspot SSIDs. Change this to list your mobile hotspot(s) Local $HotSpots[2] = [ _ "BATMOBILE", _ ; My phone hotspot "BATJET" _ ; My Verizon JetPack hotspot ] #cs --------------------------------------------------------------------------------------------------------- Array of syncing processes: [path, image, startup parameters] You need to change this to match the processes you want to start/stop based on SSID: Script uses Taskkill to stop the running process. It just needs the process name which is also the .exe file name. To restart the process we have to invoke the "path\program parameter" command line. You can get the path and image name from the Windows Task Manager if you right-clicking the process and click "Properties." I don't remember where I got the "/background" parameter for OneDrive. #ce --------------------------------------------------------------------------------------------------------- Local $Process[3][3] = [ _ ["C:\Users\jim\AppData\Local\Microsoft\OneDrive\", "OneDrive.exe", "/background" ], _ ["C:\Program Files\Box\Box Sync\", "BoxSync.exe", "" ], _ ["C:\Program Files (x86)\Google\Drive\", "GoogleDriveSync.exe", "" ]] Global Const $windowName = "SyncOnSyncOFf" Global Const $sFilePath = "C:\Users\jim\Documents\AutoIT\NetConnect.log" ; Set to empty string to not use log file ;Global Const $sFilePath = "" ;-------------------- Include -------------------------------------- #include #include #include #include #include ;----------------- Variables and Constants -------------------------- Global $hLog Global $idLog ; handle of text box ;-------------------- Script Main Procedure Start ------------------ OpenLog() Local $MySSID = _GetActiveSSID() LogIt("Connected. SSID = " & $MySSID) $sCmd = "start" $sStatus = "No hotspot detected." For $hsName in $HotSpots If StringCompare($MySSID, $hsName) = 0 Then $sCmd = "stop" $sStatus = "Hotspot detected." ExitLoop EndIf Next Logit($sStatus) ; Start or stop processes as needed: For $i = 0 to UBound($Process, $UBOUND_ROWS) - 1 ; Step through list of sync processes $Proc = $Process[$i][1] Local $iPID = ProcessExists($Proc) ; Check if Process is running. If $iPID Then LogIt($Proc & " is running.") if $sCmd = "stop" Then LogIt("Stopping " & $Proc) ShellExecute("Taskkill", "/f /im " & $Proc) ; Stop the madness! By "madness" I mean syncing over mobile data. Endif Else LogIt($Proc & " is not running.") if $sCmd = "start" Then LogIt("Starting " & $Proc) ShellExecute($Process[$i][0] & $Process[$i][1], $Process[$i][2]) ; Start the sync process. Endif Endif Next EndScript(0) ;---------------- End of main script. Defined functions below --------------------------- ; Parse the netsh command output to determine the current wifi SSID: ; Thanks to AutoIt Forum member "guinness" Func _GetActiveSSID() Local $iPID = Run(@ComSpec & ' /u /c ' & 'netsh wlan show interfaces', @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD), $sOutput = '' While 1 $sOutput &= StdoutRead($iPID) If @error Then ExitLoop EndIf $sOutput = StringStripWS($sOutput, 7) WEnd $sReturn = StringRegExp($sOutput, '(?s)(?i)SSID\s*:\s(.*?)' & @CR, 3) If @error Then Return SetError(1, 0, '') EndIf Return $sReturn[0] EndFunc ;==>_GetActiveSSID ; Create final log message, add it to the logs, close the log window, and exit the script: Func EndScript($cod) ; Clean up any windows left open Local $sTag Switch Int($cod) Case 0 $sTag = "Success" Case Else $sTag = "Error" EndSwitch LogIt($sTag & ": Ending script with exit code " & $cod & ".") CloseLog() Exit $cod EndFunc ; Make TimeStamp string: Func TS() return @YEAR & "-" & @MON & "-" & @MDAY & "_" & @HOUR & "." & @MIN & "." & @SEC EndFunc ; Open a log window and a log file: Func OpenLog() GUICreate($windowName, 600, 370, 700, 10) $idLog = GUICtrlCreateEdit("", 10, 10, 590, 350, $ES_AUTOVSCROLL + $WS_VSCROLL) GUISetState(@SW_SHOW) Logit("Log opened by """ & @ScriptFullPath & """ script.") EndFunc ; OpenLog ; Add a timestamped message to the logs: Func Logit($msg) Local $tsMsg = TS() & " " & $msg & @CRLF GUICtrlSetData($idLog, $tsMsg, 1) local $hFileOpen = FileOpen($sFilePath, $FO_APPEND) FileWrite($hFileOpen, $tsMsg) FileClose($hFileOpen) EndFunc ; Add final message to log and close log window: Func CloseLog() Logit("End of Log.") Sleep(10000) ; milliseconds GUIDelete() EndFunc ; Script End