Powershell script to find valid L2TP/IPSec VPN client and do robocopy transfer

383 views Asked by At

Basically related to this but without knowing the IP address...

Further explanation:

Im using a L2TP/IPSec VPN mobile client and a ZyXEL ZyWALL USG50 as the server. I cannot find a way to set a static IP for a VPN mobile client so I cannot be guaranteed that it will be the same IP. In order to successfully transfer make the RoboCopy, I need to search for the VPN mobile client's IP I want to make the transfer to.

Obviously, a lot of time is wasted just searching for it but I really have no other choice.

On a "positive" note, Im sure there isnt going to be much VPN mobile clients so Im pretty sure it is always going to be 192.168.250.1 but at MOST it would be 192.168.250.10.

My idea for the script is:

  1. Check if a IP is running (ping)
  2. Check if a path on that IP actually exists. For example //192.168.250.1/PATH
  3. Just to be sure this is the real location I want to transfer everything, check for a file such as "IAMTHECORRECTVPNBACKUPSERVER.txt"
  4. Once those conditions are met (lets say for example purposes it is the IP of 192.168.250.7):

    $IP="192.168.250.7 & c:\windows\system32\Robocopy.exe "D:\folder" "\192.168.250.7\folder" /E /MT:20 /R:50 /W:10 /V /ETA /LOG:c:\robocopy$(Get-Date -format 'ddMMyyyyHHmmss')$IP.txt

    1. Exit and finish the scheduled task

For now I have this:

$num = 0
$ipvalida=false 

DO
{
    $ip="192.168.250."+$num
    if (test-connection $ip=True) 
        {
            if (test-path \\$ip\copiaseguridad\=True) 
            {
                if (test-path \\$ip\copiaseguridad\correctlocation.txt=True) 
                {
                    $hash=Get-FileHash -a md5 \\$ip\copiaseguridad\correctlocation.txt | Select -Property Hash
                    if ($hash.toString() = "B987A46ABB6302F114385D940CD64CB2")
                    {
                        $ipvalida=true
                    }

                }
            }
        }
        $num=$num+1
} While ($num -le 5) AND ($ipvalida=false)

if ($ipvalida=true)
{
    & c:\windows\system32\Robocopy.exe "D:\folder" "\\$ip\copiaseguridad" /E /MT:20 /R:50 /W:10 /V /ETA /LOG:c:\robocopy$(Get-Date -format 'ddMMyyyHHmmssfff').txt
}

How could I do this?

0

There are 0 answers