Is there a way to turn PIA off on a timer

Hello 

I've currently got it set in task scheduler to shut PIA off at a certain time and start it again later, When It's shut down though it maintains the connection and the VPN is still active. 

Is there anyway to work around that and get it to shut down without me having to manually turn it off ?

I'm on version 1.3.3

Any help would be appreciated 

Thanks :smile:


Comments

  • Hello

    I am not aware of any feature which would allow for timed use. It may be possible to write a script that could perform this, but we would be unable to assist with this scenario.
  • edited August 2019
    You can do it with a powershell script in Windows. Just set up a timer script with a trigger time and at the trigger time disconnect the TAP adapter but this is a very brute/crude force method plus openvpn may try to reconnect and wants to re-enable the adpater, and its a little messy... so instead of that the better way, set up the script to kill the pia-service process at the trigger time - this closely mimics what happens when you right click the tray icon and choose 'Disconnect'.

    Since you seem basically familiar with Task Scheduler -  Use the windows native Task Scheduler and add a task to do this for you with a powershell script. Write a powershell script to kill the pia-service process, save it somewhere as, for example, "pia-service_kill.ps1", then create a task in Task Scheduler to execute the powershell script at the time you choose.

    A powershell script for use with Task Scheduler is simple, its one line
    Get-Process 'pia-service' -ErrorAction SilentlyContinue | Stop-Process -PassThru -Force
    put that one line in a script and save the script somewhere. Then set up a task in Task Scheduler to execute the script at the time you choose. And, a simple modification to add killing the pia-openvpn process too along with the pia-service process so the tray icon does not remain green when you kill the pia-service process (note: even though the icon remains green for a bit after the pia-service process is killed the VPN has actually disconnected when the pia-service process was killed. The pia-openvpn process is just a little confused for its expected state when the pia-service process suddenly disappears and openvpn thinks its still connected when in reality it isn't.)

    Get-Process 'pia-service' -ErrorAction SilentlyContinue | Stop-Process -PassThru -Force;
    Get-Process 'pia-openvpn' -ErrorAction SilentlyContinue | Stop-Process -PassThru -Force;

    or... if you prefer you can combine the powershell lines like this:
    Get-Process 'pia-service', 'pia-openvpn' -ErrorAction SilentlyContinue | Stop-Process -PassThru -Force

    or... if you prefer, you can uses the powershell aliases for Get-Process and Stop-Process
    ps 'pia-service', 'pia-openvpn' -ErrorAction SilentlyContinue | Kill -PassThru -Force
    With the powershell scripts, when they run it will leave a powershell process running in the background. To shut this down when the script actions are complete for shutting down pia-servce and pia-openvpn add this line to the above script examples after the other lines
    Get-Process 'powershell' -ErrorAction SilentlyContinue | Stop-Process -PassThru -Force
    so it looks like this

    Get-Process 'pia-service' -ErrorAction SilentlyContinue | Stop-Process -PassThru -Force;
    Get-Process 'pia-openvpn' -ErrorAction SilentlyContinue | Stop-Process -PassThru -Force;
    Get-Process 'powershell' -ErrorAction SilentlyContinue | Stop-Process -PassThru -Force
    or using the combining thing, like this
    Get-Process 'pia-service', 'pia-openvpn', 'powershell' -ErrorAction SilentlyContinue | Stop-Process -PassThru -Force

    or... if you don't want to use powershell then create a batch file with these lines in it:
    taskkill /IM pia-service.exe /F
    taskkill /IM pia-openvpn.exe /F
    save the batch file in some place other than the root of your hard drive, then create a task in Task Scheduler to run the file at the time you choose or manually run the file.

    In OS X and Linux you can use 'Kill' or 'KillAll' but make the proper adjustments for conversion from windows to those.

    What ever 'script' method you choose always stop the pia-service process first because if you do not then the pia-openvpn process is gonna reconnect. 

    You can also set up a powershell script to connect the VPN for you on a schedule, a little more involved than just disconnecting but still simple.

    a few notes on your OP;

    You could not have disconnected the VPN connection with whatever Task Scheduler method you used if its still connected to VPN after the task runs. Are you sure what you are seeing is just not the icon being green still because the pia-openvpn process is still hanging around in the task list? I mentioned this above. Or were you trying to disconnect/kill the pia-openvpn process because if that's all you did it will reconnect if the pia-service process is still running.



  • Yeah It's defiantly maintaining the connection as when I open it again in the morning I get a message saying that the app was shut down unexpectedly and my VPN connection was preserved,

    Thank you very much for the advice man, I'll give that a go with the information you provided and post back here with updates

    Thanks again :) 
  • edited August 2019
    You shut the whole client down? Don't have to do that.

    Anyway, you're welcome - post back and let me know how it works out for you.
  • edited August 2019
    Task Scheduler is the best way to do this with the script lines I posted in the previous post. Buttt.... In case anyone does not want to mess with Task Scheduler to run the script lines I posted above but wants a timed shut down of the VPN - here is the 'timer' powershell script to do this for you.
    $ErrorActionPreference = 'SilentlyContinue';
    Start-Sleep ((Get-Date '11:38:00 AM') - (Get-Date)).TotalSeconds;
    $piaservicepid = (Get-Process 'pia-service').Id; $piaopenvpnpid = (Get-Process 'pia-openvpn').Id;
    Stop-Process -Id $piaservicepid -PassThru -Force; Wait-Process -Id $piaservicepid;
    Stop-Process -Id $piaopenvpnpid -PassThru -Force; Wait-Process -Id $piaopenvpnpid;
    Get-Process 'powershell' -ErrorAction SilentlyContinue | Stop-Process -PassThru -Force
    You have to run this one manually. Put in the VPN shut down time time, in the same manner and form as I have above. For example, if you wanted the VPN connection to shut down at 1 PM your time local then you would do this in the second line of the script:
    Start-Sleep ((Get-Date '1:00:00 PM') - (Get-Date)).TotalSeconds;
    then start up powershell and run the script. You will need to keep powershell open, although you can minimize it to the task bar if you wish, so the script will keep running until it does its job at the trigger time. When the trigger time is reached the rest of the lines will execute and the VPN connection will be disconnected and then the poweshell session will be terminated so it does not hang around in the background.

    If someone is interested in another variation of a VPN disconnect script that uses the TAP adapter disconnect method, here it is:
    $tapindex = (Get-NetAdapter | ? Virtual -eq $true | ? Status -eq 'Up' | ? DriverName -match 'tap-pia-*').ifIndex;
    $piaservicepid = (Get-Process 'pia-service').Id;
    Get-NetAdapter | ? ifIndex -eq $tapindex | Disable-NetAdapter -Confirm:$false;
    Stop-Process -Id $piaservicepid -PassThru -Force;
    Get-NetAdapter | ? ifIndex -eq $tapindex | Enable-NetAdapter -Confirm:$false
    It first gets the TAP Adapter interface index. Then gets the pia-service.exe process ID. It then disables the TAP adapter, kills the pia-service process using the process ID then re-enables the TAP adapter so it will be ready to connect the next time you want to connect to VPN. Please note that it gets the interface index of the presently connected TAP adapter which uses the driver with the "tap-pia-" driver naming convention, this matches the driver version installed when the version of the new redesigned client is installed. Its written this way in case driver versions of a future TAP adapter for PIA changes, the wild card portion (the *) will take care of that if it maintains the same beginning in the name as 'tap-pia-' plus we get a TAP that's installed by PIA and not another TAP for something else that may be on the system.

    Please note: The above scripts use the naming convention for the TAP 'DriverName' and the 'pia-service' process and the 'pia-openvpn' process as they are named with the install of the new redesigned version of the PIA client. If you are using the old design version of the PIA client you can make the proper adjustments in the naming of these items to match that old design version.

    If anyone wants a powershell one liner application kill switch for VPN use: Here is a generic, very basic, one liner application 'kill switch' for Windows 10 using powershell:
    Register-WMIEvent -Namespace root\wmi -Class MSNdis_StatusMediaDisconnect -Action {Get-Process 'Notepad' -ErrorAction SilentlyContinue | Stop-Process -PassThru -Force}
    This shuts down an application if any of your network adapters disconnect, including the TAP adapter if the VPN connection drops unexpectedly. Where I have 'Notepad' in the script line you put the process name of the application(s) you want to shut down if the VPN disconnects via TAP unexpectedly. You can put in multiple process names by separating them with a comma, like this example
    Register-WMIEvent -Namespace root\wmi -Class 
    MSNdis_StatusMediaDisconnect -Action {Get-Process 'Notepad', 'Firefox', 'bittorrent' -ErrorAction
     SilentlyContinue | Stop-Process -PassThru -Force}
    To use the basic generic kill switch script line: Place the names of the processes you want to shut down, you can get these from Windows Task Manager. Then, with the VPN connected and your shut down app running (the process name), run the script line in a powershell session window and leave it alone. If the VPN connection suddenly dies and the TAP disconnects it will trigger and shut down the processes you have in the script line.

    You can put this shutdown line in a powershell script with your process names in it and save it somewhere (e.g. c:\vpnkillswitch.ps1' - then just run the script (if will have all your shut down process names saved in it). You can use this to, for example, shut down your bit torrent client if you lose the VPN connection. This is a very basic and generic script line, I'll leave it up to you to customize it for your use and specific needs/desires.

    Note: Why trigger on any adapter disconnecting and just not the TAP adapter? It is possible to do that, but the TAP adapter interface has to use another interface in your system to get its connection to the internet and thus the VPN system because it uses that other interface which does have a connection to the internet. In this basic generic version its just monitoring all the connections and assumes one 'hard' interface and the 'soft' ('virtual' software TAP interface) so if the hard interface disconnects so will the TAP. But the TAP can disconnect from the VPN on its own without the hard interface disconnecting from the internet. Thus the 'catch all connections' thing because its the two conditions under which the TAP can disconnect from the VPN.
  • Hey again,

    Sorry for the delay In replying.

    I've managed to get it working by setting Task Scheduler to run the first script you provided, It wouldn't run it by default though and after some fiddling about I found that I had to set the Task to run with highest privileges.

    So currently I've got three tasks scheduled, Two for opening and closing PIA and the other for running the script bellow

     Get-Process 'pia-service' -ErrorAction SilentlyContinue | Stop-Process -PassThru -Force

    I've set one of the tasks to close PIA just after the script above is ran so it doesn't maintain the connection once the app is closed.

    I've also set PIA to reconnect on app launch so I just use Task Scheduler to open it on a timer 


    Thanks you very much for your help, It would of taken me weeks to try and figure all this out by myself.
     
  • I forgot to mention the privilege thing, some will need it and some will not. Glad it worked out for you though, Good luck. :).
  • Posting on this old thread because it was the first to come up in a search for this issue. Simply schedule a windows task to run at the time you want to connect / disconnect. Then have it run the application piactl.exe (in the install directory for PIA) with the argument connect or disconnect, for which action you want.

Sign In or Register to comment.