SonicWallVpn.ps1

function Enable-SonicVpn
{
    Param
    {
        [string]
        $name = "VPN"
    }
    Write-Host "Connecting to VPN..."
    
    $user = Get-User
    $pwd = Get-Password
        
    & "C:\Program Files\Dell SonicWALL\Global VPN Client\swgvc.exe" /E $name /U $user /P $pwd
}

function Disable-SonicVpn
{
    Write-Host "Disonnecting VPN..."
    
    & "C:\Program Files\Dell SonicWALL\Global VPN Client\swgvc.exe" /D $name
}


function Get-Password
{
    $pwdFileName = "$(Get-ModulePath)\vpn.pwd"

    if(Test-Path $pwdFileName)
    {
        $pwdFile = gc $pwdFileName
        $secured = $pwdFile | ConvertTo-SecureString

    }
    else
    {
        $secured = Read-Host -Prompt "Enter password for VPN" -AsSecureString
        $secured | ConvertFrom-SecureString | Out-File $pwdFileName
    }
    
    $user=[System.Security.Principal.WindowsIdentity]::GetCurrent().Name
    $creds = new-object PSCredential $user, $secured
    $pwd = $creds.GetNetworkCredential().Password
    return $pwd
}

function Get-User
{
    $usrFileName = "$(Get-ModulePath)\vpn.usr"

    if(Test-Path $usrFileName)
    {
        $usr = gc $usrFileName     

    }
    else
    {
        $usr = Read-Host -Prompt "Enter user name for VPN"      
        $usr | Out-File $usrFileName  
    }

    return $usr
    
}

function Get-ModulePath
{
    Split-Path (gmo -list Personal).Path
}



New-Alias evpn Enable-SonicVpn
Export-ModuleMember -Function Enable-SonicVpn -Alias evpn

New-Alias dvpn Disable-SonicVpn
Export-ModuleMember -Function Disable-SonicVpn -Alias dvpn